1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
mod popup;
mod prompt;
mod select_table;
mod sized_gauge;
mod sized_paragraph;
mod sized_table;
mod text_input;
mod with_block;

pub use popup::*;
pub use prompt::*;
use ratatui::widgets::block::Title;
pub use select_table::*;
pub use sized_gauge::*;
pub use sized_paragraph::*;
pub use sized_table::*;
pub use text_input::*;
pub use with_block::*;

use crossterm::event::Event;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
use ratatui::prelude::*;
use ratatui::widgets::*;

pub trait ProcessEvent {
    type Result;
    fn input(&mut self, event: Event) -> Self::Result;
}

pub trait SizedWidget {
    fn height(&self) -> Option<u16> {
        None
    }
    fn width(&self) -> Option<u16> {
        None
    }
}

pub trait Draw {
    fn draw(&mut self, area: Rect, f: &mut Frame<'_>);
}

// the widgets we are using and convenience builders
pub type PopUpInput = PopUp<WithBlock<TextInput>>;
pub fn popup_input(
    title: impl Into<Title<'static>>,
    text: &str,
    initial: &str,
    lines: u16,
) -> PopUpInput {
    PopUp(WithBlock::new(
        TextInput::new(Some(text), initial, lines, true),
        Block::bordered().title(title),
    ))
}

pub fn popup_scrollable_text(
    title: impl Into<Title<'static>>,
    text: &str,
    lines: u16,
) -> PopUpInput {
    PopUp(WithBlock::new(
        TextInput::new(None, text, lines, false),
        Block::bordered().title(title),
    ))
}

pub type PopUpText = PopUp<WithBlock<SizedParagraph>>;
pub fn popup_text(title: impl Into<Title<'static>>, text: Text<'static>) -> PopUpText {
    PopUp(WithBlock::new(
        SizedParagraph::new(text),
        Block::bordered().title(title),
    ))
}

pub type PopUpTable = PopUp<WithBlock<SizedTable>>;
pub fn popup_table(
    title: impl Into<Title<'static>>,
    content: Vec<Vec<Text<'static>>>,
) -> PopUpTable {
    PopUp(WithBlock::new(
        SizedTable::new(content),
        Block::bordered().title(title),
    ))
}

pub type PopUpPrompt = Prompt<PopUpText>;
pub fn popup_prompt(title: &'static str, text: Text<'static>) -> PopUpPrompt {
    Prompt(popup_text(title, text))
}

pub type PopUpGauge = PopUp<WithBlock<SizedGauge>>;
pub fn popup_gauge(
    title: impl Into<Title<'static>>,
    text: Span<'static>,
    ratio: f64,
) -> PopUpGauge {
    PopUp(WithBlock::new(
        SizedGauge::new(text, ratio),
        Block::bordered().title(title),
    ))
}