Load configuration on nvim startup/ small ui updates

This commit is contained in:
daa 2017-10-29 13:55:36 +03:00
parent db9bbc03f3
commit 512b63a37a
8 changed files with 107 additions and 87 deletions

36
Cargo.lock generated
View File

@ -22,7 +22,6 @@ dependencies = [
"phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -278,15 +277,6 @@ name = "htmlescape"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.8"
@ -447,11 +437,6 @@ dependencies = [
"magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "0.2.2"
@ -534,18 +519,6 @@ dependencies = [
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tempfile"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.4"
@ -600,11 +573,6 @@ name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
"checksum atk-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c69658a4e18d5c9575f716e24559645d08a4044d6946c30c2e0025952c84d842"
@ -630,7 +598,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gtk 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce17f98e7dcdc9d06b3a5f7621d796a24937c04953481205b1be267c5a02697a"
"checksum gtk-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "854b56ce6d6b05945f7735651482835c5ac1f8582142ce67306726259a3dafb0"
"checksum htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"
"checksum libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "8a014d9226c2cc402676fbe9ea2e15dd5222cd1dd57f576b5b283178c944a264"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
@ -650,7 +617,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "eb250fd207a4729c976794d03db689c9be1d634ab5a1c9da9492a13d8fecbcdf"
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
"checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b"
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
"checksum rmp 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7ce560a5728f4eec697f07f8d7fa20608893d44b4f5b8f9f5f51a2987f3cffe2"
@ -661,7 +627,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "11ce2fe9db64b842314052e2421ac61a73ce41b898dc8e3750398b219c5fc1e0"
"checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14"
"checksum toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7540f4ffc193e0d3c94121edb19b055670d369f77d5804db11ae053a45b6e7e"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
@ -670,4 +635,3 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

View File

@ -35,8 +35,6 @@ serde = "1.0"
serde_derive = "1.0"
toml = "0.4"
tempfile = "2.2.0"
#[dependencies.neovim-lib]
#git = "https://github.com/daa84/neovim-lib"

View File

@ -23,8 +23,6 @@ extern crate serde;
extern crate serde_derive;
extern crate toml;
extern crate tempfile;
mod sys;
mod nvim_config;

View File

@ -260,8 +260,8 @@ pub fn start(
}
if let Some(nvim_config) = nvim_config.generate_config() {
if let Some(path) = nvim_config.path().to_str() {
cmd.arg("--cmd").arg(format!("source '{}'", path));
if let Some(path) = nvim_config.to_str() {
cmd.arg("--cmd").arg(format!("source {}", path));
}
}

View File

@ -1,8 +1,8 @@
use std;
use std::fs::File;
use std::path::PathBuf;
use std::fs::OpenOptions;
use std::io::Write;
use tempfile;
use dirs;
use plug_manager;
#[derive(Clone)]
@ -15,7 +15,7 @@ impl NvimConfig {
NvimConfig { plug_config }
}
pub fn generate_config(&self) -> Option<tempfile::NamedTempFile> {
pub fn generate_config(&self) -> Option<PathBuf> {
if self.plug_config.is_some() {
match self.write_file() {
Err(err) => {
@ -29,17 +29,26 @@ impl NvimConfig {
}
}
fn write_file(&self) -> std::io::Result<tempfile::NamedTempFile> {
let temp_file = tempfile::NamedTempFile::new()?;
{
let mut file: &File = &temp_file;
let content = &self.plug_config.as_ref().unwrap().source;
if !content.is_empty() {
file.write_all(content.as_bytes())?;
}
fn write_file(&self) -> Result<PathBuf, String> {
let mut config_dir = dirs::get_app_config_dir_create()?;
config_dir.push("plugins.vim");
file.sync_data()?;
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&config_dir)
.map_err(|e| format!("{}", e))?;
let content = &self.plug_config.as_ref().unwrap().source;
if !content.is_empty() {
debug!("{}", content);
file.write_all(content.as_bytes()).map_err(
|e| format!("{}", e),
)?;
}
Ok(temp_file)
file.sync_all().map_err(|e| format!("{}", e))?;
Ok(config_dir)
}
}

View File

@ -13,7 +13,7 @@ pub struct Manager {
impl Manager {
pub fn new() -> Self {
Manager {
Manager {
vim_plug: vim_plug::Manager::new(),
plug_manage_state: PlugManageState::Unknown,
}
@ -22,9 +22,14 @@ impl Manager {
pub fn load_config(&mut self) -> Option<PlugManagerConfigSource> {
if Store::is_config_exists() {
let store = Store::load();
let config = PlugManagerConfigSource::new(&store);
self.plug_manage_state = PlugManageState::NvimGtk(store);
Some(config)
if store.is_enabled() {
let config = PlugManagerConfigSource::new(&store);
self.plug_manage_state = PlugManageState::NvimGtk(store);
Some(config)
} else {
self.plug_manage_state = PlugManageState::NvimGtk(store);
None
}
} else {
None
}
@ -37,15 +42,36 @@ impl Manager {
pub fn update_state(&mut self) {
if self.vim_plug.is_loaded() {
if let PlugManageState::Unknown = self.plug_manage_state {
self.plug_manage_state = PlugManageState::Configuration(Store::load_from_plug(&self.vim_plug));
self.plug_manage_state =
PlugManageState::VimPlug(Store::load_from_plug(&self.vim_plug));
}
}
}
pub fn store_mut(&mut self) -> Option<&mut Store> {
match self.plug_manage_state {
PlugManageState::NvimGtk(ref mut store) => Some(store),
PlugManageState::VimPlug(ref mut store) => Some(store),
PlugManageState::Unknown => None,
}
}
pub fn store(&self) -> Option<&Store> {
match self.plug_manage_state {
PlugManageState::NvimGtk(ref store) => Some(store),
PlugManageState::VimPlug(ref store) => Some(store),
PlugManageState::Unknown => None,
}
}
pub fn save(&self) {
self.store().map(|s| s.save());
}
}
pub enum PlugManageState {
NvimGtk(Store),
Configuration(Store),
VimPlug(Store),
Unknown,
}
@ -56,15 +82,14 @@ pub struct PlugManagerConfigSource {
impl PlugManagerConfigSource {
pub fn new(store: &Store) -> Self {
let mut builder = "call plug#begin()".to_owned();
let mut builder = "call plug#begin()\n".to_owned();
for plug in store.get_plugs() {
builder += &format!("Plug '{}'", plug.get_plug_path());
builder += &format!("Plug '{}'\n", plug.get_plug_path());
}
builder += "call plug#end()";
builder += "call plug#end()\n";
PlugManagerConfigSource { source: builder }
}
}

View File

@ -12,6 +12,10 @@ impl Store {
Settings::is_file_exists()
}
pub fn is_enabled(&self) -> bool {
self.settings.enabled
}
pub fn load() -> Self {
Store { settings: Settings::load() }
}
@ -37,12 +41,20 @@ impl Store {
pub fn get_plugs(&self) -> &[PlugInfo] {
&self.settings.plugs
}
pub fn set_enabled(&mut self, enabled: bool) {
self.settings.enabled = enabled;
}
pub fn save(&self) {
self.settings.save();
}
}
#[derive(Serialize, Deserialize)]
struct Settings {
plugs: Vec<PlugInfo>,
enabled: bool,
plugs: Vec<PlugInfo>,
}
impl Settings {

View File

@ -39,6 +39,8 @@ impl<'a> Ui<'a> {
let enable_swc = gtk::Switch::new();
enable_swc.set_valign(gtk::Align::Center);
let manager_ref = self.manager.clone();
header_bar.pack_end(&enable_swc);
header_bar.set_title("Plug");
@ -51,27 +53,23 @@ impl<'a> Ui<'a> {
match self.manager.borrow_mut().plug_manage_state {
manager::PlugManageState::Unknown => {
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
let warn_lbl = gtk::Label::new(None);
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!");
help.pack_start(&warn_lbl, true, false, 0);
let help_lbl = gtk::Label::new("Help");
pages.add_page(&help_lbl, &help, "help");
add_help_tab(
&pages,
"<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!",
);
}
manager::PlugManageState::Configuration(ref store) => {
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
let warn_lbl = gtk::Label::new(None);
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!\n\
NeovimGtk manages plugins use vim-plug as backend, so enable it disables vim-plug configuration.");
help.pack_start(&warn_lbl, true, false, 0);
let help_lbl = gtk::Label::new("Help");
pages.add_page(&help_lbl, &help, "help");
manager::PlugManageState::VimPlug(ref store) => {
enable_swc.set_state(store.is_enabled());
add_help_tab(
&pages,
"<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!\n\
NeovimGtk manages plugins use vim-plug as backend, so enable it disables vim-plug configuration.\n\
Current configuration taken from your vim-plug",
);
self.add_plugin_list_tab(&pages, store);
}
manager::PlugManageState::NvimGtk(ref store) => {
enable_swc.set_state(store.is_enabled());
let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
// TODO:
let get_plugins_lbl = gtk::Label::new("Get Plugins");
@ -82,13 +80,20 @@ impl<'a> Ui<'a> {
}
enable_swc.connect_state_set(move |_, state| {
manager_ref.borrow_mut().store_mut().map(|s| {
s.set_enabled(state)
});
Inhibit(false)
});
content.pack_start(&*pages, true, true, 0);
content.show_all();
match dlg.run() {
OK_ID => {
println!("TODO:");
self.manager.borrow().save();
}
_ => (),
}
@ -97,7 +102,6 @@ impl<'a> Ui<'a> {
}
fn add_plugin_list_tab(&self, pages: &SettingsPages, store: &Store) {
// Plugins
let plugins = gtk::Box::new(gtk::Orientation::Vertical, 3);
self.fill_plugin_list(&plugins, store);
@ -126,7 +130,7 @@ impl<'a> Ui<'a> {
let remove_btn = gtk::Button::new_with_label("Remove");
remove_btn.set_halign(gtk::Align::End);
let store_ref = store.clone();
//let store_ref = store.clone();
let panel_ref = panel.clone();
let row_ref = row.clone();
remove_btn.connect_clicked(move |_| {
@ -154,6 +158,16 @@ impl<'a> Ui<'a> {
}
}
fn add_help_tab(pages: &SettingsPages, markup: &str) {
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
let label = gtk::Label::new(None);
label.set_markup(markup);
help.pack_start(&label, true, false, 0);
let help_lbl = gtk::Label::new("Help");
pages.add_page(&help_lbl, &help, "help");
}
struct SettingsPages {
categories: gtk::ListBox,
stack: gtk::Stack,