diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-24 21:27:13 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-24 21:39:40 -0400 |
commit | 77685885d5f8a8dfd1889245cf1bd6d783503620 (patch) | |
tree | 031ee7b504be4e49ddedc57c2eac352079a83066 /src | |
parent | c894688fe20ad9f886ffaccb833a1f74c28db125 (diff) |
textfield cursor working correctly
- this fixes boundary issues and cursor not staying in place
- also fix issue with quit not making sure all io workers are done
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/quit.rs | 2 | ||||
-rw-r--r-- | src/context.rs | 2 | ||||
-rw-r--r-- | src/run.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_textfield.rs | 54 |
4 files changed, 36 insertions, 24 deletions
diff --git a/src/commands/quit.rs b/src/commands/quit.rs index bf1132f..45283c1 100644 --- a/src/commands/quit.rs +++ b/src/commands/quit.rs @@ -15,7 +15,7 @@ impl Quit { } pub fn quit(context: &mut JoshutoContext) -> JoshutoResult<()> { - if !context.worker_queue.is_empty() { + if context.worker_busy { Err(JoshutoError::new( JoshutoErrorKind::IOOther, String::from("operations running in background, use force_quit to quit"), diff --git a/src/context.rs b/src/context.rs index 9f357b5..6801e69 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,6 +10,7 @@ pub struct JoshutoContext { pub curr_tab_index: usize, pub tabs: Vec<JoshutoTab>, pub worker_queue: VecDeque<IOWorkerThread>, + pub worker_busy: bool, pub worker_msg: Option<String>, pub message_queue: VecDeque<String>, @@ -25,6 +26,7 @@ impl JoshutoContext { curr_tab_index: 0, tabs: Vec::new(), worker_queue: VecDeque::with_capacity(10), + worker_busy: false, worker_msg: None, message_queue: VecDeque::with_capacity(4), events: Events::new(), @@ -40,6 +40,7 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: let observer = IOWorkerObserver::new(worker, event_tx); Some(observer) }; + context.worker_busy = true; } } @@ -80,6 +81,7 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: } io_observer = None; context.worker_msg = None; + context.worker_busy = false; } Event::Input(key) => { /* Message handling */ diff --git a/src/ui/widgets/tui_textfield.rs b/src/ui/widgets/tui_textfield.rs index c72bc9b..b98a045 100644 --- a/src/ui/widgets/tui_textfield.rs +++ b/src/ui/widgets/tui_textfield.rs @@ -1,9 +1,11 @@ +use std::io::Write; + use rustyline::completion::{Candidate, Completer, FilenameCompleter, Pair}; use rustyline::line_buffer; use termion::event::Key; use tui::layout::Rect; -use tui::style::{Color, Style}; +use tui::style::{Color, Modifier, Style}; use tui::widgets::{Paragraph, Text, Widget}; use unicode_width::UnicodeWidthChar; @@ -73,25 +75,19 @@ impl<'a> TuiTextField<'a> { let char_idx = self ._prefix - .char_indices() - .last() - .map(|(i, c)| i + c.width().unwrap_or(0)) - .unwrap_or(0); + .chars() + .map(|c| c.len_utf8()) + .sum(); + line_buffer.insert_str(0, self._suffix); line_buffer.insert_str(0, self._prefix); - line_buffer.insert_str(char_idx, self._suffix); line_buffer.set_pos(char_idx); let terminal = backend.terminal_mut(); - terminal.show_cursor(); - let mut cursor_xpos = self._prefix.len() + 1; - { - let frame = terminal.get_frame(); - let f_size = frame.size(); - terminal.set_cursor(cursor_xpos as u16, f_size.height - 1); - } + let mut cursor_xpos = line_buffer.pos(); loop { + cursor_xpos = line_buffer.pos(); terminal .draw(|mut frame| { let f_size = frame.size(); @@ -124,9 +120,28 @@ impl<'a> TuiTextField<'a> { let cmd_prompt_style = Style::default().fg(Color::LightGreen); + let cursor_style = Style::default().modifier(Modifier::REVERSED); + + let prefix = &line_buffer.as_str()[..cursor_xpos]; + + let curr = line_buffer.as_str()[cursor_xpos..] + .chars() + .nth(0); + let (suffix, curr) = match curr { + Some(c) => { + let curr_len = c.len_utf8(); + (&line_buffer.as_str()[(cursor_xpos + curr_len)..], c) + } + None => ("", ' '), + }; + + let curr_string = curr.to_string(); + let text = [ Text::styled(self._prompt, cmd_prompt_style), - Text::raw(line_buffer.as_str()), + Text::raw(prefix), + Text::styled(curr_string, cursor_style), + Text::raw(suffix), ]; let textfield_rect = Rect { @@ -226,19 +241,12 @@ impl<'a> TuiTextField<'a> { _ => {} }; } - cursor_xpos = line_buffer.pos() + 1; - { - let frame = terminal.get_frame(); - let f_size = frame.size(); - terminal.set_cursor(cursor_xpos as u16, f_size.height - 1); - } } - terminal.hide_cursor(); if line_buffer.as_str().is_empty() { None } else { - let strin = line_buffer.to_string(); - Some(strin) + let input_string = line_buffer.to_string(); + Some(input_string) } } } |