diff options
author | Piotr Wach <pwach@bloomberg.net> | 2023-12-20 20:29:07 +0100 |
---|---|---|
committer | Piotr Wach <pwach@bloomberg.net> | 2023-12-20 20:30:43 +0100 |
commit | aaa27e860508e564d82b43295baa4290b53eb87f (patch) | |
tree | 4cd326dafdb751667decb4643fe4d9a7026fc8f7 /src | |
parent | 49aecb9245054446ac1b338ea1cc29831e72d5e0 (diff) |
Fix cursor rendering
Diffstat (limited to 'src')
-rw-r--r-- | src/interactive/app/eventloop.rs | 24 | ||||
-rw-r--r-- | src/interactive/widgets/glob.rs | 22 | ||||
-rw-r--r-- | src/interactive/widgets/main.rs | 5 |
3 files changed, 35 insertions, 16 deletions
diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 1379811..45d4441 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -27,6 +27,13 @@ pub enum FocussedPane { } #[derive(Default)] +pub struct Cursor { + pub show: bool, + pub x: u16, + pub y: u16, +} + +#[derive(Default)] pub struct AppState { pub normal_mode: NavigationState, pub glob_mode: Option<NavigationState>, @@ -70,7 +77,18 @@ impl AppState { display, state: self, }; - draw_window(window, props, terminal) + let mut cursor = Cursor::default(); + + let result = draw_window(window, props, terminal, &mut cursor); + + if cursor.show { + _ = terminal.show_cursor(); + } else { + _ = terminal.hide_cursor(); + } + _ = terminal.set_cursor(cursor.x, cursor.y); + + result } pub fn process_events<B>( @@ -316,13 +334,15 @@ pub fn draw_window<B>( window: &mut MainWindow, props: MainWindowProps, terminal: &mut Terminal<B>, + cursor: &mut Cursor, ) -> Result<()> where B: Backend, { let area = terminal.pre_render()?; - window.render(props, area, terminal); + window.render(props, area, terminal, cursor); terminal.post_render()?; + Ok(()) } diff --git a/src/interactive/widgets/glob.rs b/src/interactive/widgets/glob.rs index 5084c04..51fff3d 100644 --- a/src/interactive/widgets/glob.rs +++ b/src/interactive/widgets/glob.rs @@ -14,6 +14,8 @@ use tui::{ }; use tui_react::Terminal; +use crate::interactive::Cursor; + pub struct GlobPaneProps { pub border_style: Style, pub has_focus: bool, @@ -73,13 +75,13 @@ impl GlobPane { let from_left_to_current_index = current_index - 1; // // Getting all characters before the selected character. - // let before_char_to_delete = self.input.chars().take(from_left_to_current_index); + let before_char_to_delete = self.input.chars().take(from_left_to_current_index); // // Getting all characters after selected character. - // let after_char_to_delete = self.input.chars().skip(current_index); + let after_char_to_delete = self.input.chars().skip(current_index); // Put all characters together except the selected one. // By leaving the selected one out, it is forgotten and therefore deleted. - self.input = self.input[0..from_left_to_current_index].to_string(); + self.input = before_char_to_delete.chain(after_char_to_delete).collect(); self.move_cursor_left(); } } @@ -93,6 +95,7 @@ impl GlobPane { props: impl Borrow<GlobPaneProps>, area: Rect, terminal: &mut Terminal<B>, + cursor: &mut Cursor, ) where B: Backend, { @@ -118,16 +121,11 @@ impl GlobPane { ); if *has_focus { - _ = terminal.show_cursor(); - _ = terminal.set_cursor( - // Draw the cursor at the current position in the input field. - // This position is can be controlled via the left and right arrow key - inner_block_area.x + self.cursor_position as u16 + 1, - // Move one line down, from the border to the input line - inner_block_area.y, - ); + cursor.show = true; + cursor.x = inner_block_area.x + self.cursor_position as u16 + 1; + cursor.y = inner_block_area.y; } else { - _ = terminal.hide_cursor(); + cursor.show = false; } } } diff --git a/src/interactive/widgets/main.rs b/src/interactive/widgets/main.rs index 83383eb..46e6724 100644 --- a/src/interactive/widgets/main.rs +++ b/src/interactive/widgets/main.rs @@ -3,7 +3,7 @@ use crate::interactive::{ Entries, EntriesProps, Footer, FooterProps, GlobPane, GlobPaneProps, Header, HelpPane, HelpPaneProps, MarkPane, MarkPaneProps, COLOR_MARKED, }, - AppState, DisplayOptions, FocussedPane, + AppState, Cursor, DisplayOptions, FocussedPane, }; use std::borrow::Borrow; use tui::backend::Backend; @@ -40,6 +40,7 @@ impl MainWindow { props: impl Borrow<MainWindowProps<'a>>, area: Rect, terminal: &mut Terminal<B>, + cursor: &mut Cursor, ) where B: Backend, { @@ -118,7 +119,7 @@ impl MainWindow { border_style: glob_style, has_focus: matches!(state.focussed, Glob), }; - pane.render(props, glob_area, terminal); + pane.render(props, glob_area, terminal, cursor); } Footer.render( |