From 2560ae486605b38a78ddc17bfabaeaa7953b616a Mon Sep 17 00:00:00 2001 From: Jan Vanbuel Date: Mon, 9 Dec 2024 10:28:28 +0100 Subject: [PATCH 1/2] refactor filter into widget --- src/app/model/config.rs | 17 +++-------------- src/app/model/dagruns.rs | 16 ++-------------- src/app/model/dags.rs | 17 +++-------------- src/app/model/filter.rs | 25 +++++++++++++++++++++++++ src/app/model/taskinstances.rs | 18 +++--------------- 5 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/app/model/config.rs b/src/app/model/config.rs index f6f3544..8a0a770 100644 --- a/src/app/model/config.rs +++ b/src/app/model/config.rs @@ -2,9 +2,9 @@ use crossterm::event::KeyCode; use log::debug; use ratatui::buffer::Buffer; use ratatui::layout::{Constraint, Layout, Rect}; -use ratatui::style::{Modifier, Styled, Stylize}; +use ratatui::style::{Modifier, Stylize}; use ratatui::text::Line; -use ratatui::widgets::{Block, BorderType, Borders, Paragraph, Row, StatefulWidget, Table, Widget}; +use ratatui::widgets::{Block, BorderType, Borders, Row, StatefulWidget, Table, Widget}; use crate::airflow::config::AirflowConfig; use crate::app::events::custom::FlowrsEvent; @@ -122,18 +122,7 @@ impl Widget for &mut ConfigModel { .margin(0) .split(area); - let filter = self.filter.prefix().clone(); - - let paragraph = Paragraph::new(filter.unwrap_or("".to_string())) - .block( - Block::default() - .border_type(BorderType::Rounded) - .borders(Borders::ALL) - .title("filter"), - ) - .set_style(DEFAULT_STYLE); - - Widget::render(paragraph, rects[1], buf); + self.filter.render(rects[1], buf); rects } else { diff --git a/src/app/model/dagruns.rs b/src/app/model/dagruns.rs index e6bfb74..ef47ef4 100644 --- a/src/app/model/dagruns.rs +++ b/src/app/model/dagruns.rs @@ -1,7 +1,7 @@ use crossterm::event::KeyCode; use log::debug; use ratatui::layout::{Constraint, Layout, Rect}; -use ratatui::style::{Modifier, Style, Styled, Stylize}; +use ratatui::style::{Modifier, Style, Stylize}; use ratatui::text::{Line, Span}; use ratatui::widgets::{ Block, BorderType, Borders, Clear, Paragraph, Row, Scrollbar, ScrollbarOrientation, @@ -308,19 +308,7 @@ impl Widget for &mut DagRunModel { .margin(0) .split(area); - let filter = self.filter.prefix().clone(); - - let paragraph = Paragraph::new(filter.unwrap_or("".to_string())) - .block( - Block::default() - .border_type(BorderType::Rounded) - .borders(Borders::ALL) - .title("filter"), - ) - .set_style(DEFAULT_STYLE); - - Widget::render(paragraph, rects[1], buf); - + self.filter.render(rects[1], buf); rects } else { Layout::default() diff --git a/src/app/model/dags.rs b/src/app/model/dags.rs index c7ff6d5..ae94fd4 100644 --- a/src/app/model/dags.rs +++ b/src/app/model/dags.rs @@ -4,9 +4,9 @@ use crossterm::event::KeyCode; use log::debug; use ratatui::buffer::Buffer; use ratatui::layout::{Constraint, Flex, Layout, Rect}; -use ratatui::style::{Color, Modifier, Style, Styled, Stylize}; +use ratatui::style::{Color, Modifier, Style, Stylize}; use ratatui::text::{Line, Span}; -use ratatui::widgets::{Block, BorderType, Borders, Paragraph, Row, StatefulWidget, Table, Widget}; +use ratatui::widgets::{Block, BorderType, Borders, Row, StatefulWidget, Table, Widget}; use time::OffsetDateTime; use crate::airflow::model::dag::Dag; @@ -186,18 +186,7 @@ impl Widget for &mut DagModel { .margin(0) .split(area); - let filter = self.filter.prefix().clone(); - - let paragraph = Paragraph::new(filter.unwrap_or("".to_string())) - .block( - Block::default() - .border_type(BorderType::Rounded) - .borders(Borders::ALL) - .title("filter"), - ) - .set_style(DEFAULT_STYLE); - - Widget::render(paragraph, rects[1], buf); + self.filter.render(rects[1], buf); rects } else { diff --git a/src/app/model/filter.rs b/src/app/model/filter.rs index 72760d0..2bb0ac7 100644 --- a/src/app/model/filter.rs +++ b/src/app/model/filter.rs @@ -1,4 +1,12 @@ use crossterm::event::{KeyCode, KeyEvent}; +use ratatui::{ + buffer::Buffer, + layout::Rect, + style::Styled, + widgets::{Block, BorderType, Borders, Paragraph, Widget}, +}; + +use crate::ui::constants::DEFAULT_STYLE; pub struct Filter { pub enabled: bool, @@ -58,3 +66,20 @@ impl Default for Filter { Self::new() } } + +impl Widget for &Filter { + fn render(self, area: Rect, buf: &mut Buffer) { + let filter = self.prefix().clone(); + + let paragraph = Paragraph::new(filter.unwrap_or("".to_string())) + .block( + Block::default() + .border_type(BorderType::Rounded) + .borders(Borders::ALL) + .title("filter"), + ) + .set_style(DEFAULT_STYLE); + + Widget::render(paragraph, area, buf); + } +} diff --git a/src/app/model/taskinstances.rs b/src/app/model/taskinstances.rs index 6f56a8d..ef24866 100644 --- a/src/app/model/taskinstances.rs +++ b/src/app/model/taskinstances.rs @@ -6,9 +6,9 @@ use crossterm::event::KeyCode; use log::debug; use ratatui::buffer::Buffer; use ratatui::layout::{Constraint, Layout, Rect}; -use ratatui::style::{Modifier, Style, Styled, Stylize}; +use ratatui::style::{Modifier, Style, Stylize}; use ratatui::text::Line; -use ratatui::widgets::{Block, BorderType, Borders, Paragraph, Row, StatefulWidget, Table, Widget}; +use ratatui::widgets::{Block, BorderType, Borders, Row, StatefulWidget, Table, Widget}; use time::format_description; use crate::airflow::model::taskinstance::TaskInstance; @@ -251,19 +251,7 @@ impl Widget for &mut TaskInstanceModel { .margin(0) .split(area); - let filter = self.filter.prefix().clone(); - - let paragraph = Paragraph::new(filter.unwrap_or("".to_string())) - .block( - Block::default() - .border_type(BorderType::Rounded) - .borders(Borders::ALL) - .title("filter"), - ) - .set_style(DEFAULT_STYLE); - - Widget::render(paragraph, rects[1], buffer); - + self.filter.render(rects[1], buffer); rects } else { Layout::default() From 3b1380bda7c23d779f3a7ef09a88b0d10a5006ca Mon Sep 17 00:00:00 2001 From: Jan Vanbuel Date: Mon, 9 Dec 2024 11:14:49 +0100 Subject: [PATCH 2/2] show cursor position --- src/app/model/filter.rs | 24 +++++++++++++++++++++--- src/ui.rs | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/app/model/filter.rs b/src/app/model/filter.rs index 2bb0ac7..26f65f6 100644 --- a/src/app/model/filter.rs +++ b/src/app/model/filter.rs @@ -1,16 +1,22 @@ use crossterm::event::{KeyCode, KeyEvent}; use ratatui::{ buffer::Buffer, - layout::Rect, + layout::{Position, Rect}, style::Styled, widgets::{Block, BorderType, Borders, Paragraph, Widget}, }; use crate::ui::constants::DEFAULT_STYLE; +#[derive(Clone)] +pub struct CursorState { + pub position: Position, +} + pub struct Filter { pub enabled: bool, pub prefix: Option, + pub cursor: CursorState, } impl Filter { @@ -18,6 +24,9 @@ impl Filter { Filter { enabled: false, prefix: None, + cursor: CursorState { + position: Position::default(), + }, } } @@ -59,6 +68,9 @@ impl Filter { _ => {} } } + pub fn cursor_position(&self) -> &Position { + &self.cursor.position + } } impl Default for Filter { @@ -67,11 +79,17 @@ impl Default for Filter { } } -impl Widget for &Filter { +impl Widget for &mut Filter { fn render(self, area: Rect, buf: &mut Buffer) { let filter = self.prefix().clone(); + let filter_text = filter.unwrap_or_default(); + + self.cursor.position = Position { + x: area.x + 1 + filter_text.len() as u16, + y: area.y + 1, + }; - let paragraph = Paragraph::new(filter.unwrap_or("".to_string())) + let paragraph = Paragraph::new(filter_text) .block( Block::default() .border_type(BorderType::Rounded) diff --git a/src/ui.rs b/src/ui.rs index 5679b06..b5f73dc 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -16,18 +16,32 @@ pub fn draw_ui(f: &mut Frame, app: &Arc>) { render_init_screen(f, app.ticks); return; } + // Only frame has the ability to set the cursor position, so we need to control the cursor filter from here + // Not very elegant, and quite some duplication... Should be refactored match app.active_panel { Panel::Config => { app.configs.render(f.area(), f.buffer_mut()); + if app.configs.filter.is_enabled() { + f.set_cursor_position(app.configs.filter.cursor.position); + } } Panel::Dag => { app.dags.render(f.area(), f.buffer_mut()); + if app.dags.filter.is_enabled() { + f.set_cursor_position(app.dags.filter.cursor.position); + } } Panel::DAGRun => { app.dagruns.render(f.area(), f.buffer_mut()); + if app.dagruns.filter.is_enabled() { + f.set_cursor_position(app.dagruns.filter.cursor.position); + } } Panel::TaskInstance => { app.task_instances.render(f.area(), f.buffer_mut()); + if app.task_instances.filter.is_enabled() { + f.set_cursor_position(app.task_instances.filter.cursor.position); + } } Panel::Logs => app.logs.render(f.area(), f.buffer_mut()), }