summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-01-30 12:05:19 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-01-30 13:01:35 +0100
commit37b555faa39c0bee96ce40f0cff7e76dd5ef783b (patch)
tree045d2d881f7865591b16a624a1bc4aa80450eab3
parent99446027a16ae77ac22745767637ac28ba659bde (diff)
Remove bindings code
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/bindings.rs146
-rw-r--r--src/main.rs1
-rw-r--r--src/views/main.rs40
3 files changed, 2 insertions, 185 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
deleted file mode 100644
index e21243f..0000000
--- a/src/bindings.rs
+++ /dev/null
@@ -1,146 +0,0 @@
-use cursive::Cursive;
-use cursive::Printer;
-use cursive::View;
-use cursive::XY;
-use cursive::event::Callback;
-use cursive::event::Event;
-use cursive::event::EventResult;
-use cursive::event::Key;
-
-use crate::views::main::MainView;
-
-pub fn get_bindings() -> Bindings {
- Bindings(vec![
- Binding {
- chars: ["quit", "q"].iter().map(ToString::to_string).collect(),
- callback: Callback::from_fn(|siv: &mut Cursive| {
- trace!("Callback called: q");
- let continue_running = siv.call_on_name(crate::views::main::MAIN_VIEW_NAME, |mv: &mut MainView| {
- if mv.tabs().tab_order().len() == 1 {
- false
- } else {
- if let Some(key) = mv.tabs().active_tab().cloned() {
- debug!("Removing tab: {}", key);
- if let Err(e) = mv.tabs_mut().remove_tab(&key) {
- error!("{:?}", e); // TODO do more than just logging
- }
- debug!("Remove tab");
- } else {
- debug!("No tab to remove found.");
- }
- true
- }
- })
- .unwrap_or(true);
-
- if !continue_running {
- debug!("Byebye");
- siv.quit();
- }
- })
- },
-
- // Binding {
- // chars: ["open", "o"].iter().map(ToString::to_string).collect(),
- // callback: Callback::from_fn(MainView::add_notmuch_query_layer),
- // }
- ])
-}
-
-pub const BINDINGS_CALLER: &str = "BINDINGS_CALLER";
-
-#[derive(Clone)]
-pub struct Bindings(Vec<Binding>);
-
-impl Bindings {
- pub fn new(bs: Vec<Binding>) -> Bindings {
- Bindings(bs)
- }
-
- pub fn caller(&self) -> BindingCaller {
- BindingCaller::new(self.clone())
- }
-}
-
-#[derive(Clone)]
-pub struct Binding {
- chars: Vec<String>,
- callback: Callback,
-}
-
-impl Binding {
- pub fn for_events(chars: Vec<String>, callback: Callback) -> Binding {
- Binding { chars, callback }
- }
-}
-
-pub struct BindingCaller {
- configuration: Bindings,
- state: Vec<char>
-}
-
-impl BindingCaller {
- pub fn new(configuration: Bindings) -> Self {
- BindingCaller { configuration, state: Vec::new() }
- }
-
- pub fn process(&mut self, chr: char) {
- debug!("Char = {}", chr);
- self.state.push(chr);
- }
-
- pub fn finalize(&self) -> Option<Callback> {
- self.configuration
- .0
- .iter()
- .find(|binding| {
- trace!("chars {:?} == state {:?}", binding.chars, self.state);
- let state_str = self.state.iter().collect::<String>();
- binding.chars.iter().any(|state| *state == state_str)
- })
- .map(|binding| {
- trace!("Binding found");
- binding.callback.clone()
- })
- }
-
-}
-
-impl View for BindingCaller {
- fn draw(&self, printer: &Printer) {
- trace!("Drawing with offset = {:?}", printer.offset);
- trace!("Drawing with output_size = {:?}", printer.output_size);
- trace!("Drawing with size = {:?}", printer.size);
-
- {
- let line = "-".repeat(printer.output_size.x);
- let position = XY {
- x: printer.offset.x,
- y: printer.output_size.y - 3,
- };
- printer.print(position, &line)
- }
-
- {
- let line = format!(":{}", self.state.iter().collect::<String>());
- let position = XY {
- x: printer.offset.x,
- y: printer.output_size.y - 2,
- };
- printer.print(position, &line)
- }
- }
-
- fn on_event(&mut self, e: Event) -> EventResult {
- match e {
- Event::Key(Key::Enter) => EventResult::Consumed(self.finalize()),
- Event::Char(chr) => {
- self.process(chr);
- EventResult::Consumed(None)
- },
- _ => unimplemented!()
- }
- }
-
-}
-
diff --git a/src/main.rs b/src/main.rs
index 2f9cfae..5d847d5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,7 +10,6 @@ use cursive::event::{Event, EventTrigger};
use cursive_flexi_logger_view::FlexiLoggerView;
use flexi_logger::{Logger, LogTarget};
-mod bindings;
mod configuration;
mod runtime;
mod tabs;
diff --git a/src/views/main.rs b/src/views/main.rs
index 2cf54c3..c42e417 100644
--- a/src/views/main.rs
+++ b/src/views/main.rs
@@ -18,8 +18,6 @@ use cursive::views::NamedView;
use cursive::views::ResizedView;
use getset::{Getters, MutGetters};
-use crate::bindings::BindingCaller;
-use crate::bindings::Bindings;
use crate::tabs::*;
use crate::runtime::Runtime;
@@ -31,18 +29,14 @@ pub struct MainView {
#[getset(get = "pub", get_mut = "pub")]
tabs: crate::tabs::Tabs,
-
- bindings: Bindings,
- bindings_caller: Option<ResizedView<NamedView<BindingCaller>>>,
}
impl MainView {
pub fn new(rt: Rc<Runtime>) -> Result<NamedView<Self>> {
let default_query = rt.config().notmuch_default_query();
let tabs = Tabs::new(default_query, rt.clone())?;
- let bindings = crate::bindings::get_bindings();
- Ok(MainView { rt, tabs, bindings, bindings_caller: None }.with_name(MAIN_VIEW_NAME))
+ Ok(MainView { rt, tabs }.with_name(MAIN_VIEW_NAME))
}
pub fn get_current_tab(&self) -> Option<&cursive_multiplex::Mux> {
@@ -82,9 +76,6 @@ impl MainView {
impl View for MainView {
fn draw(&self, printer: &Printer) {
self.tabs.draw(printer);
- if let Some(caller) = self.bindings_caller.as_ref() {
- caller.draw(printer);
- }
}
fn layout(&mut self, xy: XY<usize>) {
@@ -101,34 +92,7 @@ impl View for MainView {
fn on_event(&mut self, e: Event) -> EventResult {
debug!("Received event: {:?}", e);
- match self.bindings_caller.as_mut() {
- Some(caller) => match e {
- Event::Key(Key::Esc) => {
- self.bindings_caller = None;
- debug!("Escape. Resetting bindings caller.");
- EventResult::Consumed(None)
- },
- other => {
- debug!("Forwarding event to bindings caller");
- caller.on_event(other)
- },
- },
- None => match e {
- Event::Char(':') => {
- debug!(": -> Constructing bindings caller.");
- self.bindings_caller = Some({
- self.bindings.caller()
- .with_name(crate::bindings::BINDINGS_CALLER)
- .resized(SizeConstraint::Full, SizeConstraint::AtLeast(5))
- });
- EventResult::Consumed(None)
- },
- other => {
- debug!("Forwarding event to tabs");
- self.tabs.on_event(other)
- },
- }
- }
+ self.tabs.on_event(e)
}
fn call_on_any<'a>(&mut self, s: &Selector, tpl: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)) {