implement command to reload config (#243)

* implement command to reload themes

* refresh keybindings after config reload
This commit is contained in:
Henrik Friedrichsen
2020-08-12 01:12:08 +02:00
committed by GitHub
parent 9e5e3f3089
commit 4f71b2489b
5 changed files with 76 additions and 37 deletions

View File

@@ -12,10 +12,12 @@ use crate::spotify::{Spotify, VOLUME_PERCENT};
use crate::traits::ViewExt;
use crate::ui::help::HelpView;
use crate::ui::layout::Layout;
use crate::UserData;
use cursive::event::{Event, Key};
use cursive::traits::View;
use cursive::views::ViewRef;
use cursive::Cursive;
use std::cell::RefCell;
pub enum CommandResult {
Consumed(Option<String>),
@@ -26,7 +28,7 @@ pub enum CommandResult {
pub struct CommandManager {
aliases: HashMap<String, String>,
bindings: HashMap<String, Command>,
bindings: RefCell<HashMap<String, Command>>,
spotify: Arc<Spotify>,
queue: Arc<Queue>,
library: Arc<Library>,
@@ -39,6 +41,16 @@ impl CommandManager {
library: Arc<Library>,
config: &Config,
) -> CommandManager {
CommandManager {
aliases: HashMap::new(),
bindings: RefCell::new(Self::get_bindings(config)),
spotify,
queue,
library,
}
}
pub fn get_bindings(config: &Config) -> HashMap<String, Command> {
let mut kb = if config.default_keybindings.unwrap_or(true) {
Self::default_keybindings()
} else {
@@ -55,13 +67,7 @@ impl CommandManager {
}
}
CommandManager {
aliases: HashMap::new(),
bindings: kb,
spotify,
queue,
library,
}
kb
}
pub fn register_aliases<S: Into<String>>(&mut self, name: S, aliases: Vec<S>) {
@@ -150,10 +156,23 @@ impl CommandManager {
Ok(None)
}
Command::Help => {
let view = Box::new(HelpView::new(self.keybindings().clone()));
let view = Box::new(HelpView::new(self.bindings.borrow().clone()));
s.call_on_name("main", move |v: &mut Layout| v.push_view(view));
Ok(None)
}
Command::ReloadConfig => {
let cfg = crate::config::load()?;
// update theme
let theme = crate::theme::load(&cfg);
s.set_theme(theme);
// update bindings
self.unregister_keybindings(s);
self.bindings.replace(Self::get_bindings(&cfg));
self.register_keybindings(s);
Ok(None)
}
Command::Search(_)
| Command::Move(_, _)
| Command::Shift(_, _)
@@ -201,30 +220,38 @@ impl CommandManager {
}
pub fn register_keybinding<E: Into<cursive::event::Event>>(
this: Arc<Self>,
&self,
cursive: &mut Cursive,
event: E,
command: Command,
) {
cursive.add_global_callback(event, move |s| {
this.handle(s, command.clone());
if let Some(data) = s.user_data::<UserData>().cloned() {
data.cmd.handle(s, command.clone());
}
});
}
pub fn register_keybindings(this: Arc<Self>, cursive: &mut Cursive) {
let kb = this.keybindings();
pub fn unregister_keybindings(&self, cursive: &mut Cursive) {
let kb = self.bindings.borrow();
for (k, v) in kb {
for (k, _v) in kb.iter() {
if let Some(binding) = Self::parse_keybinding(&k) {
Self::register_keybinding(this.clone(), cursive, binding, v.clone());
} else {
error!("Could not parse keybinding: \"{}\"", &k);
cursive.clear_global_callbacks(binding);
}
}
}
pub fn keybindings(&self) -> &HashMap<String, Command> {
&self.bindings
pub fn register_keybindings(&self, cursive: &mut Cursive) {
let kb = self.bindings.borrow();
for (k, v) in kb.iter() {
if let Some(binding) = Self::parse_keybinding(&k) {
self.register_keybinding(cursive, binding, v.clone());
} else {
error!("Could not parse keybinding: \"{}\"", &k);
}
}
}
fn default_keybindings() -> HashMap<String, Command> {