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