mirror of https://github.com/bobwen-dev/hunter
replaced manual write!s
This commit is contained in:
parent
eb5a86b7cd
commit
86250206c3
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()?;
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
14
src/term.rs
14
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>> {}
|
||||
|
|
|
@ -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();
|
||||
},
|
||||
_ => {
|
||||
|
|
Loading…
Reference in New Issue