diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 18:09:23 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 18:09:23 +0200 |
| commit | de9ad07b2a4737713f1473641fe195d7e3023928 (patch) | |
| tree | 38f3bdb122b6e94fe45dbfa2264314c7a7f26be8 /src/app.rs | |
| parent | 29ab8b40dc6976687ffb8bfbf663314b0ec3c46e (diff) | |
add tui-cursor handling
Diffstat (limited to 'src/app.rs')
| -rw-r--r-- | src/app.rs | 41 |
1 files changed, 30 insertions, 11 deletions
@@ -5,11 +5,15 @@ use crate::{ sheet::register::Register, state::{window::Window, GlobalState}, tui, + tuicursor::TuiCursor, widgets::{logview::LogView, luaeditor::LuaEditor, sheetview::SheetView}, }; use ratatui::{ - crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, + crossterm::{ + event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, + ExecutableCommand, + }, prelude::*, }; @@ -19,6 +23,7 @@ pub struct App { view: SheetView, editor: LuaEditor, logview: LogView, + cursor: Option<TuiCursor>, } impl App { @@ -33,10 +38,11 @@ impl App { view: SheetView::new(), editor: LuaEditor::new(), logview: LogView::new(), + cursor: None, } } - pub fn run(&mut self, terminal: &mut tui::Tui) -> io::Result<()> { + pub fn run(mut self, terminal: &mut tui::Tui) -> io::Result<()> { if let Err(e) = lua::source(&config::constants::USER_RC_PATH) { self.exit = true; tui::restore()?; @@ -44,7 +50,18 @@ impl App { } while !self.exit { - terminal.draw(|frame| self.render_frame(frame))?; + terminal.draw(|frame| { + self.render_frame(frame); + + if let Some(cursor) = &self.cursor { + frame.set_cursor(cursor.position.1, cursor.position.0); + } + })?; + + if let Some(cursor) = &self.cursor { + terminal.backend_mut().execute(cursor.style)?; + } + self.handle_events()?; } @@ -75,13 +92,11 @@ impl App { let mut state = GlobalState::instance_mut(); state.log.visible = !state.log.visible; } - _ => { - match focus { - Window::View => self.view.handle_key_event(key_event), - Window::Editor => self.editor.handle_key_event(key_event), - Window::Log => self.logview.handle_key_event(key_event), - Window::Error => {}, - } + _ => match focus { + Window::View => self.view.handle_key_event(key_event), + Window::Editor => self.editor.handle_key_event(key_event), + Window::Log => self.logview.handle_key_event(key_event), + Window::Error => {} }, } } @@ -103,7 +118,8 @@ impl App { } if state.editor.visible { - let layout = Layout::horizontal([Constraint::Min(1), Constraint::Length(80)]).split(view); + let layout = + Layout::horizontal([Constraint::Min(1), Constraint::Length(80)]).split(view); view = layout[0]; editor = Some(layout[1]); } @@ -121,8 +137,11 @@ impl Widget for &mut App { self.view.render(view, buf); + self.cursor = None; + if let Some(editor) = editor { self.editor.render(editor, buf); + self.cursor = self.editor.render_cursor().map(|c| c.relative_to(editor)); } if let Some(log) = log { |