fixed minibuffer

This commit is contained in:
rabite 2019-02-28 20:05:11 +01:00
parent 06817602a8
commit 9338f5e912
4 changed files with 46 additions and 40 deletions

View File

@ -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 {

View File

@ -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(())
}
}

View File

@ -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(())
}

View File

@ -24,7 +24,6 @@ lazy_static! {
pub enum Events {
InputEvent(Event),
WidgetReady,
ExclusiveInput(bool),
ExclusiveEvent(Option<Sender<Events>>),
}