Skip to content

Commit

Permalink
Merge pull request #282 from jvanbuel/feature/show-cursor-in-filter
Browse files Browse the repository at this point in the history
Feature/show cursor in filter
  • Loading branch information
jvanbuel authored Dec 9, 2024
2 parents 6420cb0 + 3b1380b commit 9fc9ce8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 57 deletions.
17 changes: 3 additions & 14 deletions src/app/model/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 2 additions & 14 deletions src/app/model/dagruns.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 3 additions & 14 deletions src/app/model/dags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions src/app/model/filter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
buffer::Buffer,
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 @@ -51,10 +68,36 @@ impl Filter {
_ => {}
}
}
pub fn cursor_position(&self) -> &Position {
&self.cursor.position
}
}

impl Default for Filter {
fn default() -> Self {
Self::new()
}
}

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_text)
.block(
Block::default()
.border_type(BorderType::Rounded)
.borders(Borders::ALL)
.title("filter"),
)
.set_style(DEFAULT_STYLE);

Widget::render(paragraph, area, buf);
}
}
18 changes: 3 additions & 15 deletions src/app/model/taskinstances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
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 9fc9ce8

Please sign in to comment.