diff options
author | rabite <rabite@posteo.de> | 2019-03-03 00:24:21 +0100 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-03-03 00:24:21 +0100 |
commit | 86250206c32dc55ca5be44b528028323d2227e9c (patch) | |
tree | 88bb274153e67aa2e830b217430d8eb714d1df36 | |
parent | eb5a86b7cd37dc39d20f6ce122f671f94f51b75a (diff) |
replaced manual write!s
-rw-r--r-- | src/fail.rs | 8 | ||||
-rw-r--r-- | src/file_browser.rs | 32 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/minibuffer.rs | 19 | ||||
-rw-r--r-- | src/term.rs | 14 | ||||
-rw-r--r-- | src/widget.rs | 7 |
6 files changed, 62 insertions, 19 deletions
diff --git a/src/fail.rs b/src/fail.rs index 1c0575b..a6f8abe 100644 --- a/src/fail.rs +++ b/src/fail.rs @@ -44,6 +44,14 @@ pub enum HError { NoWidgetCoreError(Backtrace), #[fail(display = "No header for widget")] NoHeaderError, + #[fail(display = "You wanted this!")] + Quit +} + +impl HError { + pub fn quit() -> HResult<()> { + Err(HError::Quit) + } } pub trait ErrorLog where Self: Sized { diff --git a/src/file_browser.rs b/src/file_browser.rs index 13533c1..37a836c 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -106,23 +106,41 @@ impl FileBrowser { pub fn new_cored(core: &WidgetCore) -> HResult<FileBrowser> { let cwd = std::env::current_dir().unwrap(); let coords = core.coordinates.clone(); - let core_ = core.clone(); + let core_m = core.clone(); + let core_l = core.clone(); let mut miller = MillerColumns::new(core); miller.set_coordinates(&coords)?; - let (_, main_coords, _) = miller.calculate_coordinates(); + let (left_coords, main_coords, _) = miller.calculate_coordinates(); + + let main_path = cwd.ancestors() + .take(1) + .map(|path| { + std::path::PathBuf::from(path) + }).last()?; + let left_path = main_path.parent().map(|p| p.to_path_buf()); - let main_path: std::path::PathBuf = cwd.ancestors().take(1).map(|path| std::path::PathBuf::from(path)).collect(); let main_widget = WillBeWidget::new(&core, Box::new(move |_| { - let mut list = ListView::new(&core_, - Files::new_from_path(&main_path).unwrap()); + let mut list = ListView::new(&core_m, + Files::new_from_path(&main_path)?); list.set_coordinates(&main_coords).log(); list.animate_slide_up().log(); Ok(list) })); + if let Some(left_path) = left_path { + let left_widget = WillBeWidget::new(&core, Box::new(move |_| { + let mut list = ListView::new(&core_l, + Files::new_from_path(&left_path)?); + list.set_coordinates(&left_coords).log(); + list.animate_slide_up().log(); + Ok(list) + })); + miller.push_widget(left_widget); + } + miller.push_widget(main_widget); @@ -331,8 +349,7 @@ impl FileBrowser { let mut file = std::fs::File::create(filepath)?; file.write(output.as_bytes())?; - panic!("Quitting!"); - Ok(()) + HError::quit() } pub fn turbo_cd(&mut self) -> HResult<()> { @@ -493,4 +510,3 @@ impl PartialEq for FileBrowser { } } } - diff --git a/src/main.rs b/src/main.rs index b8ecd00..92e4e54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,6 +69,7 @@ fn run() -> HResult<()> { let mut screen = AlternateScreen::from(bufout); let mut _stdout = MouseTerminal::from(stdout().into_raw_mode()?); screen.cursor_hide()?; + screen.clear()?; screen.flush()?; let core = WidgetCore::new()?; diff --git a/src/minibuffer.rs b/src/minibuffer.rs index f9a0f2f..690127f 100644 --- a/src/minibuffer.rs +++ b/src/minibuffer.rs @@ -1,10 +1,11 @@ +use std::io::Write; + use termion::event::Key; -use std::io::{stdout, Write}; use crate::coordinates::{Coordinates}; use crate::widget::{Widget, WidgetCore}; -use crate::fail::{HResult, HError}; -use crate::term; +use crate::fail::{HResult, HError, ErrorLog}; +use crate::term::ScreenExt; #[derive(Debug)] pub struct MiniBuffer { @@ -45,7 +46,7 @@ impl MiniBuffer { self.completions.clear(); self.last_completion = None; - write!(stdout(), "{}", termion::cursor::Show)?; + self.get_core()?.screen.lock()?.cursor_hide(); self.popup()?; @@ -360,14 +361,14 @@ impl Widget for MiniBuffer { fn after_draw(&self) -> HResult<()> { let cursor_pos = self.query.len() + ": ".len() + - self.position + - 1; + self.position; - let ysize = term::ysize(); + let mut screen = self.get_core()?.screen.lock()?; + let ysize = screen.ysize()?; + screen.goto_xy(cursor_pos, ysize).log(); + screen.cursor_show().log(); - write!(stdout(), "{}", term::goto_xy(cursor_pos as u16, ysize))?; - stdout().flush()?; Ok(()) } } diff --git a/src/term.rs b/src/term.rs index 317f5d5..d92baa3 100644 --- a/src/term.rs +++ b/src/term.rs @@ -21,11 +21,25 @@ pub trait ScreenExt: Write { self.flush()?; Ok(()) } + fn clear(&mut self) -> HResult<()> { + write!(self, "{}", termion::clear::All)?; + Ok(()) + } fn write_str(&mut self, str: &str) -> HResult<()> { write!(self, "{}", str)?; self.flush()?; Ok(()) } + fn goto_xy(&mut self, x: usize, y: usize) -> HResult<()> { + let x = x as u16; + let y = y as u16; + write!(self, "{}", goto_xy(x + 1, y + 1))?; + Ok(()) + } + fn ysize(&self) -> HResult<usize> { + let (_, ysize) = termion::terminal_size()?; + Ok((ysize - 1) as usize) + } } impl ScreenExt for AlternateScreen<Box<Stdout>> {} diff --git a/src/widget.rs b/src/widget.rs index f2be282..ba409cd 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -112,7 +112,7 @@ pub trait Widget { fn on_event(&mut self, event: Event) -> HResult<()> { match event { - Event::Key(Key::Char('q')) => panic!("It's your fault!"), + Event::Key(Key::Char('q')) => HError::quit(), Event::Key(key) => self.on_key(key), Event::Mouse(button) => self.on_mouse(button), Event::Unsupported(wtf) => self.on_wtf(wtf), @@ -287,7 +287,10 @@ pub trait Widget { for event in rx_internal_event.iter() { match event { Events::InputEvent(event) => { - self.on_event(event).ok(); + match self.on_event(event) { + Err(HError::Quit) => { HError::quit()?; }, + _ => {} + } self.draw().ok(); }, _ => { |