-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource.rs
139 lines (126 loc) · 4.47 KB
/
resource.rs
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
135
136
137
138
139
use crate::{
input,
state::resources::Resource,
state::State,
view::{
self,
async_ops::{self, AsyncOpsTable, AsyncOpsTableCtx},
bold,
controls::{controls_paragraph, ControlDisplay, Controls, KeyDisplay},
help::HelpText,
TableListState,
},
};
use once_cell::sync::OnceCell;
use ratatui::{
layout::{self, Layout},
text::{Span, Spans, Text},
widgets::Paragraph,
};
use std::{cell::RefCell, rc::Rc};
pub(crate) struct ResourceView {
resource: Rc<RefCell<Resource>>,
pub(crate) async_ops_table: TableListState<AsyncOpsTable, 9>,
initial_render: bool,
}
impl ResourceView {
pub(super) fn new(resource: Rc<RefCell<Resource>>) -> Self {
ResourceView {
resource,
async_ops_table: TableListState::<AsyncOpsTable, 9>::default(),
initial_render: true,
}
}
pub(crate) fn update_input(&mut self, event: input::Event) {
self.async_ops_table.update_input(event)
}
pub(crate) fn render<B: ratatui::backend::Backend>(
&mut self,
styles: &view::Styles,
frame: &mut ratatui::terminal::Frame<B>,
area: layout::Rect,
state: &mut State,
) {
let resource = &*self.resource.borrow();
let controls = Controls::new(view_controls(), &area, styles);
let (controls_area, stats_area, async_ops_area) = {
let chunks = Layout::default()
.direction(layout::Direction::Vertical)
.constraints(
[
// controls
layout::Constraint::Length(controls.height()),
// resource stats
layout::Constraint::Length(8),
// async ops
layout::Constraint::Percentage(60),
]
.as_ref(),
)
.split(area);
(chunks[0], chunks[1], chunks[2])
};
let stats_area = Layout::default()
.direction(layout::Direction::Horizontal)
.constraints(
[
layout::Constraint::Percentage(50),
layout::Constraint::Percentage(50),
]
.as_ref(),
)
.split(stats_area);
let overview = vec![
Spans::from(vec![bold("ID: "), Span::raw(resource.id_str())]),
Spans::from(vec![bold("Parent ID: "), Span::raw(resource.parent())]),
Spans::from(vec![bold("Kind: "), Span::raw(resource.kind())]),
Spans::from(vec![bold("Target: "), Span::raw(resource.target())]),
Spans::from(vec![
bold("Type: "),
Span::raw(resource.concrete_type()),
Span::raw(" "),
resource.type_visibility().render(styles),
]),
Spans::from(vec![bold("Location: "), Span::raw(resource.location())]),
];
let mut fields = Text::default();
fields.extend(
resource
.formatted_attributes()
.iter()
.cloned()
.map(Spans::from),
);
let resource_widget =
Paragraph::new(overview).block(styles.border_block().title("Resource"));
let fields_widget = Paragraph::new(fields).block(styles.border_block().title("Attributes"));
frame.render_widget(controls.into_widget(), controls_area);
frame.render_widget(resource_widget, stats_area[0]);
frame.render_widget(fields_widget, stats_area[1]);
let ctx = AsyncOpsTableCtx {
initial_render: self.initial_render,
resource_id: resource.id(),
};
self.async_ops_table
.render(styles, frame, async_ops_area, state, ctx);
self.initial_render = false;
}
}
impl HelpText for ResourceView {
fn render_help_content(&self, styles: &view::Styles) -> Paragraph<'static> {
controls_paragraph(view_controls(), styles)
}
}
fn view_controls() -> &'static [ControlDisplay] {
static VIEW_CONTROLS: OnceCell<Vec<ControlDisplay>> = OnceCell::new();
VIEW_CONTROLS.get_or_init(|| {
let resource_controls = &[ControlDisplay {
action: "return to task list",
keys: &[KeyDisplay {
base: "esc",
utf8: Some("\u{238B} esc"),
}],
}];
[resource_controls, async_ops::view_controls()].concat()
})
}