diff options
-rw-r--r-- | src/fail.rs | 2 | ||||
-rw-r--r-- | src/minibuffer.rs | 72 | ||||
-rw-r--r-- | src/widget.rs | 11 | ||||
-rw-r--r-- | src/window.rs | 1 |
4 files changed, 46 insertions, 40 deletions
diff --git a/src/fail.rs b/src/fail.rs index 02287f9..605a4ef 100644 --- a/src/fail.rs +++ b/src/fail.rs @@ -35,6 +35,8 @@ pub enum HError { WrongDirectoryError{ path: PathBuf, dir: PathBuf }, #[fail(display = "Widget finnished")] PopupFinnished, + #[fail(display = "Input finnished")] + InputFinnished, } impl From<std::io::Error> for HError { diff --git a/src/minibuffer.rs b/src/minibuffer.rs index f48d84c..2533014 100644 --- a/src/minibuffer.rs +++ b/src/minibuffer.rs @@ -1,12 +1,10 @@ use termion::event::Key; -use termion::input::TermRead; - -use std::io::{stdin, stdout, Write}; +use std::io::{stdout, Write}; use crate::coordinates::{Coordinates}; use crate::widget::Widget; -use crate::window::{send_event, Events}; -use crate::fail::HResult; +use crate::fail::{HResult, HError}; +use crate::term; pub struct MiniBuffer { coordinates: Coordinates, @@ -38,38 +36,34 @@ impl MiniBuffer { self.done = false; self.position = 0; - send_event(Events::ExclusiveInput(true))?; + write!(stdout(), "{}", termion::cursor::Show)?; - self.draw()?; - write!(stdout(), "{}{}", - termion::cursor::Show, - termion::cursor::Save)?; - stdout().flush()?; + self.popup()?; - for event in stdin().events() { - let event = event?; - self.on_event(event); - if self.done { - break - } - self.draw()?; + // for event in stdin().events() { + // let event = event?; + // self.on_event(event); + // if self.done { + // break + // } + // self.draw()?; - write!(stdout(), "{}", termion::cursor::Restore)?; - if self.position != 0 { - write!(stdout(), - "{}", - termion::cursor::Right(self.position as u16))?; - } - stdout().flush()?; - } - - self.done = false; - - send_event(Events::ExclusiveInput(false))?; + // write!(stdout(), "{}", termion::cursor::Restore)?; + // if self.position != 0 { + // write!(stdout(), + // "{}", + // termion::cursor::Right(self.position as u16))?; + // } + // stdout().flush()?; + // } Ok(self.input.clone()) } + + pub fn input_finnished(&mut self) -> HResult<()> { + return Err(HError::PopupFinnished) + } } pub fn find_bins(comp_name: &str) -> Vec<String> { @@ -147,12 +141,12 @@ impl Widget for MiniBuffer { fn on_key(&mut self, key: Key) -> HResult<()> { match key { - Key::Esc | Key::Ctrl('c') => { self.input.clear(); self.done = true; }, + Key::Esc | Key::Ctrl('c') => { self.input_finnished()?; }, Key::Char('\n') => { if self.input != "" { self.history.push(self.input.clone()); } - self.done = true; + self.input_finnished()?; } Key::Char('\t') => { if !self.input.ends_with(" ") { @@ -209,4 +203,18 @@ impl Widget for MiniBuffer { } Ok(()) } + + fn after_draw(&self) -> HResult<()> { + let cursor_pos = self.query.len() + + ": ".len() + + self.position + + 1; + + let ysize = term::ysize(); + + + write!(stdout(), "{}", term::goto_xy(cursor_pos as u16, ysize))?; + stdout().flush()?; + Ok(()) + } } diff --git a/src/widget.rs b/src/widget.rs index 353d198..2e47b28 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -20,6 +20,7 @@ pub trait Widget { fn render_footer(&self) -> String { "".into() } fn refresh(&mut self); fn get_drawlist(&self) -> String; + fn after_draw(&self) -> HResult<()> { Ok(()) } fn on_event(&mut self, event: Event) -> HResult<()> { @@ -134,23 +135,19 @@ pub trait Widget { fn popup(&mut self) -> HResult<()> { self.run_widget(); - send_event(Events::ExclusiveEvent(None)); + send_event(Events::ExclusiveEvent(None))?; Ok(()) } fn run_widget(&mut self) -> HResult<()> { let (tx_event, rx_event) = channel(); send_event(Events::ExclusiveEvent(Some(tx_event)))?; - dbg!("sent exclusive request"); self.clear()?; self.refresh(); self.draw()?; - dbg!("entering loop"); - for event in rx_event.iter() { - dbg!(&event); match event { Events::InputEvent(input) => { if let Err(HError::PopupFinnished) = self.on_event(input) { @@ -162,8 +159,8 @@ pub trait Widget { } _ => {} } - - self.draw()?; + self.draw(); + self.after_draw(); } Ok(()) } diff --git a/src/window.rs b/src/window.rs index 90c7812..6152ed3 100644 --- a/src/window.rs +++ b/src/window.rs @@ -24,7 +24,6 @@ lazy_static! { pub enum Events { InputEvent(Event), WidgetReady, - ExclusiveInput(bool), ExclusiveEvent(Option<Sender<Events>>), } |