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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
pub mod help;
pub mod state;
use crate::{ui::help::draw_help, App, Args};
use tui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style},
widgets::{Block, BorderType, Borders, Paragraph, StatefulWidget, Widget},
Frame,
};
use tui_logger::{TuiLoggerLevelOutput, TuiLoggerWidget};
pub trait StateRenderer {
fn rect(&self) -> Rect;
fn render_child<W: Widget>(&mut self, w: W, rect: Rect);
fn render_child_stateful<W: StatefulWidget>(&mut self, w: W, state: &mut W::State, rect: Rect);
fn render<W: Widget>(&mut self, w: W) {
self.render_child(w, self.rect());
}
fn render_stateful<W: StatefulWidget>(&mut self, w: W, state: &mut W::State) {
self.render_child_stateful(w, state, self.rect());
}
}
impl<'c, 'f, B> StateRenderer for RenderContext<'c, 'f, B>
where
B: Backend,
{
#[inline]
fn rect(&self) -> Rect {
self.rect
}
fn render_child<W: Widget>(&mut self, w: W, rect: Rect) {
self.frame.render_widget(w, rect);
}
fn render_child_stateful<W: StatefulWidget>(&mut self, w: W, state: &mut W::State, rect: Rect) {
self.frame.render_stateful_widget(w, rect, state);
}
}
struct RenderContext<'c, 'f, B>
where
B: Backend + 'f,
{
frame: &'c mut Frame<'f, B>,
rect: Rect,
}
pub fn draw<B>(rect: &mut Frame<B>, app: &App)
where
B: Backend,
{
if app.global.help {
draw_help(rect)
} else {
draw_default(rect, app)
}
}
pub fn draw_default<B>(rect: &mut Frame<B>, app: &App)
where
B: Backend,
{
let size = rect.size();
let logs = app.global().logs;
let mut constraints = vec![Constraint::Length(3), Constraint::Percentage(60)];
if logs {
constraints.push(Constraint::Percentage(40));
}
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(constraints)
.split(size);
let title = draw_title(&app.args);
rect.render_widget(title, chunks[0]);
app.state().render(RenderContext {
frame: rect,
rect: chunks[1],
});
if logs {
let logs = draw_logs();
rect.render_widget(logs, chunks[2]);
}
}
fn draw_title<'a>(args: &Args) -> Paragraph<'a> {
Paragraph::new(format!(
"Poddy ({})",
args.namespace.as_deref().unwrap_or("<current>")
))
.style(Style::default().fg(Color::White))
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(Color::White))
.border_type(BorderType::Plain),
)
}
fn draw_logs<'a>() -> TuiLoggerWidget<'a> {
TuiLoggerWidget::default()
.output_timestamp(Some("%H:%M:%S%.3f".into()))
.output_level(Some(TuiLoggerLevelOutput::Abbreviated))
.style_error(Style::default().fg(Color::Red))
.style_debug(Style::default().fg(Color::Green))
.style_warn(Style::default().fg(Color::Yellow))
.style_trace(Style::default().fg(Color::Gray))
.style_info(Style::default().fg(Color::Blue))
.block(
Block::default()
.title("Logs")
.border_style(Style::default().fg(Color::White).bg(Color::Black))
.borders(Borders::ALL),
)
.style(Style::default().fg(Color::White).bg(Color::Black))
}