-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
async_ops.rs
241 lines (214 loc) · 7.86 KB
/
async_ops.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::{
state::{
async_ops::{AsyncOp, SortBy},
resources::Resource,
Id, State,
},
view::{
self, bold,
table::{self, TableList, TableListState},
DUR_LEN, DUR_TABLE_PRECISION,
},
};
use tui::{
layout,
style::{self, Color, Style},
text::Spans,
widgets::{Cell, Row, Table},
};
#[derive(Debug, Default)]
pub(crate) struct AsyncOpsTable {}
pub(crate) struct AsyncOpsTableCtx {
pub(crate) initial_render: bool,
pub(crate) resource_id: Id<Resource>,
}
impl TableList<9> for AsyncOpsTable {
type Row = AsyncOp;
type Sort = SortBy;
type Context = AsyncOpsTableCtx;
const HEADER: &'static [&'static str; 9] = &[
"ID",
"Parent",
"Task",
"Source",
"Total",
"Busy",
"Idle",
"Polls",
"Attributes",
];
const WIDTHS: &'static [usize; 9] = &[
Self::HEADER[0].len() + 1,
Self::HEADER[1].len() + 1,
Self::HEADER[2].len() + 1,
Self::HEADER[3].len() + 1,
Self::HEADER[4].len() + 1,
Self::HEADER[5].len() + 1,
Self::HEADER[6].len() + 1,
Self::HEADER[7].len() + 1,
Self::HEADER[8].len() + 1,
];
fn render<B: tui::backend::Backend>(
table_list_state: &mut TableListState<Self, 9>,
styles: &view::Styles,
frame: &mut tui::terminal::Frame<B>,
area: layout::Rect,
state: &mut State,
ctx: Self::Context,
) {
let now = if let Some(now) = state.last_updated_at() {
now
} else {
// If we have never gotten an update yet, skip...
return;
};
let AsyncOpsTableCtx {
initial_render,
resource_id,
} = ctx;
if initial_render {
table_list_state
.sorted_items
.extend(state.async_ops_state().async_ops().filter(|op| {
op.upgrade()
.map(|op| resource_id == op.borrow().resource_id())
.unwrap_or(false)
}))
} else {
table_list_state.sorted_items.extend(
state
.async_ops_state_mut()
.take_new_async_ops()
.filter(|op| {
op.upgrade()
.map(|op| resource_id == op.borrow().resource_id())
.unwrap_or(false)
}),
)
};
table_list_state
.sort_by
.sort(now, &mut table_list_state.sorted_items);
let mut id_width = view::Width::new(Self::WIDTHS[0] as u16);
let mut parent_width = view::Width::new(Self::WIDTHS[1] as u16);
let mut task_width = view::Width::new(Self::WIDTHS[2] as u16);
let mut source_width = view::Width::new(Self::WIDTHS[3] as u16);
let mut polls_width = view::Width::new(Self::WIDTHS[7] as u16);
let dur_cell = |dur: std::time::Duration| -> Cell<'static> {
Cell::from(styles.time_units(dur, DUR_TABLE_PRECISION, Some(DUR_LEN)))
};
let rows = {
let id_width = &mut id_width;
let parent_width = &mut parent_width;
let task_width = &mut task_width;
let source_width = &mut source_width;
let polls_width = &mut polls_width;
table_list_state
.sorted_items
.iter()
.filter_map(move |async_op| {
let async_op = async_op.upgrade()?;
let async_op = async_op.borrow();
let task_id = async_op.task_id()?;
let task = state
.tasks_state()
.task(task_id)
.and_then(|t| t.upgrade())
.map(|t| t.borrow().short_desc().to_owned());
let task_str = task.unwrap_or_else(|| async_op.task_id_str().to_owned());
let mut row = Row::new(vec![
Cell::from(id_width.update_str(format!(
"{:>width$}",
async_op.id(),
width = id_width.chars() as usize
))),
Cell::from(parent_width.update_str(async_op.parent_id()).to_owned()),
Cell::from(task_width.update_str(task_str)),
Cell::from(source_width.update_str(async_op.source()).to_owned()),
dur_cell(async_op.total(now)),
dur_cell(async_op.busy(now)),
dur_cell(async_op.idle(now)),
Cell::from(polls_width.update_str(async_op.total_polls().to_string())),
Cell::from(Spans::from(
async_op
.formatted_attributes()
.iter()
.flatten()
.cloned()
.collect::<Vec<_>>(),
)),
]);
if async_op.dropped() {
row = row.style(styles.terminated());
}
Some(row)
})
};
let header_style = if styles.color(Color::Cyan).is_some() {
Style::default()
} else {
Style::default().add_modifier(style::Modifier::REVERSED)
};
let header_style = header_style.add_modifier(style::Modifier::BOLD);
let header = Row::new(Self::HEADER.iter().enumerate().map(|(idx, &value)| {
if idx == table_list_state.selected_column {
if table_list_state.sort_descending {
Cell::from(styles.ascending(value))
} else {
Cell::from(styles.descending(value))
}
} else {
Cell::from(value)
}
}))
.height(1)
.style(header_style);
let table = if table_list_state.sort_descending {
Table::new(rows)
} else {
Table::new(rows.rev())
};
let block = styles.border_block().title(vec![bold(format!(
"Async Ops ({}) ",
table_list_state.len()
))]);
let layout = layout::Layout::default()
.direction(layout::Direction::Vertical)
.margin(0);
let controls = table::Controls::for_area(&area, styles);
let chunks = layout
.constraints(
[
layout::Constraint::Length(controls.height),
layout::Constraint::Max(area.height),
]
.as_ref(),
)
.split(area);
let controls_area = chunks[0];
let async_ops_area = chunks[1];
let attributes_width = layout::Constraint::Percentage(100);
let widths = &[
id_width.constraint(),
parent_width.constraint(),
task_width.constraint(),
source_width.constraint(),
layout::Constraint::Length(DUR_LEN as u16),
layout::Constraint::Length(DUR_LEN as u16),
layout::Constraint::Length(DUR_LEN as u16),
polls_width.constraint(),
attributes_width,
];
let table = table
.header(header)
.block(block)
.widths(widths)
.highlight_symbol(view::TABLE_HIGHLIGHT_SYMBOL)
.highlight_style(Style::default().add_modifier(style::Modifier::BOLD));
frame.render_stateful_widget(table, async_ops_area, &mut table_list_state.table_state);
frame.render_widget(controls.paragraph, controls_area);
table_list_state
.sorted_items
.retain(|t| t.upgrade().is_some());
}
}