Skip to main content

rustic_rs/commands/tui/
widgets.rs

1mod popup;
2mod prompt;
3mod select_table;
4mod sized_gauge;
5mod sized_paragraph;
6mod sized_table;
7mod text_input;
8mod with_block;
9
10pub use popup::*;
11pub use prompt::*;
12pub use select_table::*;
13pub use sized_gauge::*;
14pub use sized_paragraph::*;
15pub use sized_table::*;
16pub use text_input::*;
17pub use with_block::*;
18
19use crossterm::event::Event;
20use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
21use ratatui::prelude::*;
22use ratatui::widgets::{
23    Block, Clear, Gauge, Paragraph, Row, Scrollbar, ScrollbarOrientation, ScrollbarState, Table,
24    TableState,
25};
26
27pub trait ProcessEvent {
28    type Result;
29    fn input(&mut self, event: Event) -> Self::Result;
30}
31
32pub trait SizedWidget {
33    fn height(&self) -> Option<u16> {
34        None
35    }
36    fn width(&self) -> Option<u16> {
37        None
38    }
39}
40
41pub trait Draw {
42    fn draw(&mut self, area: Rect, f: &mut Frame<'_>);
43}
44
45// the widgets we are using and convenience builders
46pub type PopUpInput = PopUp<WithBlock<TextInput>>;
47pub fn popup_input(
48    title: impl Into<Line<'static>>,
49    text: &str,
50    initial: &str,
51    lines: u16,
52) -> PopUpInput {
53    PopUp(WithBlock::new(
54        TextInput::new(Some(text), initial, lines, true),
55        Block::bordered().title(title),
56    ))
57}
58
59pub fn popup_scrollable_text(
60    title: impl Into<Line<'static>>,
61    text: &str,
62    lines: u16,
63) -> PopUpInput {
64    PopUp(WithBlock::new(
65        TextInput::new(None, text, lines, false),
66        Block::bordered().title(title),
67    ))
68}
69
70pub type PopUpText = PopUp<WithBlock<SizedParagraph>>;
71pub fn popup_text(title: impl Into<Line<'static>>, text: Text<'static>) -> PopUpText {
72    PopUp(WithBlock::new(
73        SizedParagraph::new(text),
74        Block::bordered().title(title),
75    ))
76}
77
78pub type PopUpTable = PopUp<WithBlock<SizedTable>>;
79pub fn popup_table(
80    title: impl Into<Line<'static>>,
81    content: Vec<Vec<Text<'static>>>,
82) -> PopUpTable {
83    PopUp(WithBlock::new(
84        SizedTable::new(content),
85        Block::bordered().title(title),
86    ))
87}
88
89pub type PopUpPrompt = Prompt<PopUpText>;
90pub fn popup_prompt(title: &'static str, text: Text<'static>) -> PopUpPrompt {
91    Prompt(popup_text(title, text))
92}
93
94pub type PopUpGauge = PopUp<WithBlock<SizedGauge>>;
95pub fn popup_gauge(title: impl Into<Line<'static>>, text: Span<'static>, ratio: f64) -> PopUpGauge {
96    PopUp(WithBlock::new(
97        SizedGauge::new(text, ratio),
98        Block::bordered().title(title),
99    ))
100}