Skip to content

Commit

Permalink
show cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbuel committed Dec 9, 2024
1 parent 2560ae4 commit 3b1380b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/app/model/filter.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
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<String>,
pub cursor: CursorState,
}

impl Filter {
pub fn new() -> Filter {
Filter {
enabled: false,
prefix: None,
cursor: CursorState {
position: Position::default(),
},
}
}

Expand Down Expand Up @@ -59,6 +68,9 @@ impl Filter {
_ => {}
}
}
pub fn cursor_position(&self) -> &Position {
&self.cursor.position
}
}

impl Default for Filter {
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,32 @@ pub fn draw_ui(f: &mut Frame, app: &Arc<Mutex<App>>) {
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()),
}
Expand Down

0 comments on commit 3b1380b

Please sign in to comment.