Load vimawesome plugins using curl to reduce application dependencies
Dispaly vimawesome plugin information
This commit is contained in:
@@ -22,13 +22,7 @@ extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate toml;
|
||||
|
||||
// http request library
|
||||
extern crate futures;
|
||||
extern crate hyper;
|
||||
extern crate tokio_core;
|
||||
extern crate serde_json;
|
||||
extern crate hyper_tls;
|
||||
|
||||
mod sys;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use gtk::prelude::*;
|
||||
use super::manager;
|
||||
use super::store::{Store, PlugInfo};
|
||||
use super::plugin_settings_dlg;
|
||||
use super::vimawesome::Vimawesome;
|
||||
use super::vimawesome;
|
||||
|
||||
pub struct Ui<'a> {
|
||||
manager: &'a Arc<UiMutex<manager::Manager>>,
|
||||
@@ -63,6 +63,8 @@ impl<'a> Ui<'a> {
|
||||
|
||||
enable_swc.set_state(self.manager.borrow().store.is_enabled());
|
||||
|
||||
let get_plugins = add_get_plugins_tab(&pages);
|
||||
|
||||
match self.manager.borrow().plug_manage_state {
|
||||
manager::PlugManageState::Unknown => {
|
||||
add_help_tab(
|
||||
@@ -78,15 +80,7 @@ impl<'a> Ui<'a> {
|
||||
Current configuration taken from your vim-plug",
|
||||
);
|
||||
}
|
||||
manager::PlugManageState::NvimGtk => {
|
||||
let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
|
||||
Vimawesome::new().log();
|
||||
|
||||
let get_plugins_lbl = gtk::Label::new("Get Plugins");
|
||||
pages.add_page(&get_plugins_lbl, &get_plugins, "get_plugins");
|
||||
|
||||
}
|
||||
manager::PlugManageState::NvimGtk => {}
|
||||
}
|
||||
|
||||
let plugs_panel = self.add_plugin_list_tab(&pages, &self.manager.borrow().store);
|
||||
@@ -105,9 +99,26 @@ impl<'a> Ui<'a> {
|
||||
content.pack_start(&*pages, true, true, 0);
|
||||
content.show_all();
|
||||
|
||||
let get_plugins = UiMutex::new(get_plugins);
|
||||
vimawesome::call(move |res| {
|
||||
let panel = get_plugins.borrow();
|
||||
for child in panel.get_children() {
|
||||
panel.remove(&child);
|
||||
}
|
||||
match res {
|
||||
Ok(list) => {
|
||||
let result = vimawesome::build_result_panel(&list);
|
||||
panel.pack_start(&result, true, true, 0);
|
||||
}
|
||||
Err(e) => {
|
||||
panel.pack_start(>k::Label::new(format!("{}", e).as_str()), false, true, 0);
|
||||
error!("{}", e)
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
let ok: i32 = gtk::ResponseType::Ok.into();
|
||||
if dlg.run() == ok {
|
||||
if dlg.run() == ok {
|
||||
let mut manager = self.manager.borrow_mut();
|
||||
manager.clear_removed();
|
||||
manager.save();
|
||||
@@ -226,6 +237,23 @@ fn create_plug_label(plug_info: &PlugInfo) -> gtk::Box {
|
||||
label_box
|
||||
}
|
||||
|
||||
fn add_get_plugins_tab(pages: &SettingsPages) -> gtk::Box {
|
||||
let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
let spinner = gtk::Spinner::new();
|
||||
let get_plugins_lbl = gtk::Label::new("Get Plugins");
|
||||
pages.add_page(&get_plugins_lbl, &get_plugins, "get_plugins");
|
||||
|
||||
let list_panel = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
let link_button = gtk::Label::new(None);
|
||||
link_button.set_markup("Plugins source: <a href=\"https://vimawesome.com\">https://vimawesome.com</a>");
|
||||
get_plugins.pack_start(&link_button, false, true, 15);
|
||||
get_plugins.pack_start(&list_panel, true, true, 0);
|
||||
list_panel.pack_start(&spinner, true, true, 0);
|
||||
spinner.start();
|
||||
|
||||
list_panel
|
||||
}
|
||||
|
||||
fn add_help_tab(pages: &SettingsPages, markup: &str) {
|
||||
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
|
||||
let label = gtk::Label::new(None);
|
||||
|
||||
@@ -1,56 +1,130 @@
|
||||
use std::io;
|
||||
use futures::{Future, Stream};
|
||||
use hyper::{self, Client};
|
||||
use tokio_core::reactor::Core;
|
||||
use std::thread;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use serde_json;
|
||||
use hyper_tls::HttpsConnector;
|
||||
|
||||
pub struct Vimawesome {}
|
||||
use gtk;
|
||||
use gtk::prelude::*;
|
||||
use glib;
|
||||
|
||||
impl Vimawesome {
|
||||
pub fn new() -> Self {
|
||||
Vimawesome {}
|
||||
pub fn call<F>(cb: F)
|
||||
where
|
||||
F: FnOnce(io::Result<DescriptionList>) + Send + 'static,
|
||||
{
|
||||
thread::spawn(move || {
|
||||
let mut cb = Some(cb);
|
||||
glib::idle_add(move || {
|
||||
cb.take().unwrap()(request());
|
||||
glib::Continue(false)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn request() -> io::Result<DescriptionList> {
|
||||
let child = Command::new("curl")
|
||||
.arg("-s")
|
||||
.arg("https://vimawesome.com/api/plugins?query=&page=1")
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let out = child.wait_with_output()?;
|
||||
|
||||
if out.status.success() {
|
||||
let description_list: DescriptionList = serde_json::from_slice(&out.stdout).map_err(|e| {
|
||||
io::Error::new(io::ErrorKind::Other, e)
|
||||
})?;
|
||||
Ok(description_list)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"curl exit with error:\n{}",
|
||||
match out.status.code() {
|
||||
Some(code) => format!("Exited with status code: {}", code),
|
||||
None => "Process terminated by signal".to_owned(),
|
||||
}
|
||||
),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_result_panel(list: &DescriptionList) -> gtk::ScrolledWindow {
|
||||
let scroll = gtk::ScrolledWindow::new(None, None);
|
||||
scroll.get_style_context().map(|c| c.add_class("view"));
|
||||
let panel = gtk::ListBox::new();
|
||||
|
||||
for plug in list.plugins.iter() {
|
||||
let row = create_plug_row(plug);
|
||||
|
||||
panel.add(&row);
|
||||
}
|
||||
|
||||
pub fn log(&self) {
|
||||
match self.request() {
|
||||
Ok(list) => println!("list: {:?}", list),
|
||||
Err(e) => error!("{}", e),
|
||||
}
|
||||
}
|
||||
scroll.add(&panel);
|
||||
scroll.show_all();
|
||||
scroll
|
||||
}
|
||||
|
||||
fn request(&self) -> Result<DescriptionList, hyper::error::Error> {
|
||||
let mut core = Core::new()?;
|
||||
let handle = core.handle();
|
||||
let client = Client::configure()
|
||||
.connector(HttpsConnector::new(4, &handle).map_err(|e| {
|
||||
io::Error::new(io::ErrorKind::Other, e)
|
||||
})?)
|
||||
.build(&handle);
|
||||
let uri = "https://vimawesome.com/api/plugins?query=&page=1".parse()?;
|
||||
fn create_plug_row(plug: &Description) -> gtk::ListBoxRow {
|
||||
let row = gtk::ListBoxRow::new();
|
||||
let row_container = gtk::Box::new(gtk::Orientation::Vertical, 5);
|
||||
row_container.set_border_width(5);
|
||||
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);
|
||||
let label_box = create_plug_label(plug);
|
||||
|
||||
let work = client.get(uri).and_then(|res| {
|
||||
res.body().concat2().and_then(move |body| {
|
||||
let description_list: DescriptionList =
|
||||
serde_json::from_slice(&body).map_err(|e| {
|
||||
io::Error::new(io::ErrorKind::Other, e)
|
||||
})?;
|
||||
Ok(description_list)
|
||||
})
|
||||
});
|
||||
core.run(work)
|
||||
|
||||
let button_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
|
||||
button_box.set_halign(gtk::Align::End);
|
||||
|
||||
let add_btn = gtk::Button::new_with_label("Install");
|
||||
button_box.pack_start(&add_btn, false, true, 0);
|
||||
|
||||
row_container.pack_start(&hbox, true, true, 0);
|
||||
hbox.pack_start(&label_box, true, true, 0);
|
||||
hbox.pack_start(&button_box, false, true, 0);
|
||||
|
||||
row.add(&row_container);
|
||||
|
||||
|
||||
add_btn.connect_clicked(clone!(button_box => move |_| { }));
|
||||
|
||||
row
|
||||
}
|
||||
|
||||
|
||||
fn create_plug_label(plug: &Description) -> gtk::Box {
|
||||
let label_box = gtk::Box::new(gtk::Orientation::Vertical, 5);
|
||||
|
||||
let name_lbl = gtk::Label::new(None);
|
||||
name_lbl.set_markup(&format!(
|
||||
"<b>{}</b> by {}",
|
||||
plug.name,
|
||||
plug.author.as_ref().map(|s| s.as_ref()).unwrap_or(
|
||||
"unknown",
|
||||
)
|
||||
));
|
||||
name_lbl.set_halign(gtk::Align::Start);
|
||||
let url_lbl = gtk::Label::new(None);
|
||||
if let Some(url) = plug.github_url.as_ref() {
|
||||
url_lbl.set_markup(&format!("<a href=\"{}\">{}</a>", url, url));
|
||||
}
|
||||
url_lbl.set_halign(gtk::Align::Start);
|
||||
|
||||
|
||||
label_box.pack_start(&name_lbl, true, true, 0);
|
||||
label_box.pack_start(&url_lbl, true, true, 0);
|
||||
label_box
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct DescriptionList {
|
||||
plugins: Box<[Description]>,
|
||||
pub struct DescriptionList {
|
||||
pub plugins: Box<[Description]>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Description {
|
||||
name: String,
|
||||
github_url: String,
|
||||
author: String,
|
||||
github_stars: i64,
|
||||
pub struct Description {
|
||||
pub name: String,
|
||||
pub github_url: Option<String>,
|
||||
pub author: Option<String>,
|
||||
pub github_stars: Option<i64>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user