Move to enum-based commands

This commit is contained in:
Rasmus Larsen
2019-05-20 22:15:12 +02:00
parent 4b4a027c3c
commit 1671db14c1
15 changed files with 343 additions and 317 deletions

View File

@@ -12,6 +12,8 @@ use cursive::views::EditView;
use cursive::{Cursive, Printer};
use unicode_width::UnicodeWidthStr;
use command::Command;
use command::Command::Focus;
use commands::CommandResult;
use events;
use traits::{IntoBoxedViewExt, ViewExt};
@@ -283,29 +285,28 @@ impl View for Layout {
}
impl ViewExt for Layout {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "focus" {
if let Some(view) = args.get(0) {
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Focus(view) => {
if self.views.keys().any(|k| k == view) {
self.set_view(view.clone());
let screen = self.views.get_mut(view).unwrap();
screen.view.on_command(s, cmd, args)?;
screen.view.on_command(s, cmd)?;
}
Ok(CommandResult::Consumed(None))
}
Command::Back => {
self.pop_view();
Ok(CommandResult::Consumed(None))
}
_ => {
if let Some(screen) = self.get_current_screen_mut() {
screen.view.on_command(s, cmd)
} else {
Ok(CommandResult::Ignored)
}
}
Ok(CommandResult::Consumed(None))
} else if cmd == "back" {
self.pop_view();
Ok(CommandResult::Consumed(None))
} else if let Some(screen) = self.get_current_screen_mut() {
screen.view.on_command(s, cmd, args)
} else {
Ok(CommandResult::Ignored)
}
}
}