-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
tasks.rs
282 lines (256 loc) · 10.2 KB
/
tasks.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::{
state::{
tasks::{SortBy, Task, TaskState},
State,
},
view::{
self, bold,
table::{self, TableList, TableListState},
DUR_LEN, DUR_TABLE_PRECISION,
},
};
use tui::{
layout,
style::{self, Color, Style},
text::{Span, Spans, Text},
widgets::{self, Cell, ListItem, Row, Table},
};
#[derive(Debug, Default)]
pub(crate) struct TasksTable {}
impl TableList<11> for TasksTable {
type Row = Task;
type Sort = SortBy;
type Context = ();
const HEADER: &'static [&'static str; 11] = &[
"Warn", "ID", "State", "Name", "Total", "Busy", "Idle", "Polls", "Target", "Location",
"Fields",
];
const WIDTHS: &'static [usize; 11] = &[
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,
Self::HEADER[9].len() + 1,
Self::HEADER[10].len() + 1,
];
fn render<B: tui::backend::Backend>(
table_list_state: &mut TableListState<Self, 11>,
styles: &view::Styles,
frame: &mut tui::terminal::Frame<B>,
area: layout::Rect,
state: &mut State,
_: Self::Context,
) {
let state_len: u16 = Self::WIDTHS[2] as u16;
let now = if let Some(now) = state.last_updated_at() {
now
} else {
// If we have never gotten an update yet, skip...
return;
};
table_list_state
.sorted_items
.extend(state.tasks_state_mut().take_new_tasks());
table_list_state
.sort_by
.sort(now, &mut table_list_state.sorted_items);
let dur_cell = |dur: std::time::Duration| -> Cell<'static> {
Cell::from(styles.time_units(dur, DUR_TABLE_PRECISION, Some(DUR_LEN)))
};
// Start out wide enough to display the column headers...
let mut warn_width = view::Width::new(Self::WIDTHS[0] as u16);
let mut id_width = view::Width::new(Self::WIDTHS[1] as u16);
let mut name_width = view::Width::new(Self::WIDTHS[3] as u16);
let mut polls_width = view::Width::new(Self::WIDTHS[7] as u16);
let mut target_width = view::Width::new(Self::WIDTHS[8] as u16);
let mut location_width = view::Width::new(Self::WIDTHS[9] as u16);
let mut num_idle = 0;
let mut num_running = 0;
let rows = {
let id_width = &mut id_width;
let target_width = &mut target_width;
let location_width = &mut location_width;
let name_width = &mut name_width;
let polls_width = &mut polls_width;
let warn_width = &mut warn_width;
let num_running = &mut num_running;
let num_idle = &mut num_idle;
table_list_state
.sorted_items
.iter()
.filter_map(move |task| {
let task = task.upgrade()?;
let task = task.borrow();
let state = task.state();
// Count task states
match state {
TaskState::Running => *num_running += 1,
TaskState::Idle => *num_idle += 1,
_ => {}
};
let n_warnings = task.warnings().len();
let warnings = if n_warnings > 0 {
let n_warnings = n_warnings.to_string();
warn_width.update_len(n_warnings.len() + 2); // add 2 for the warning icon + whitespace
Cell::from(Spans::from(vec![
styles.warning_narrow(),
Span::from(n_warnings),
]))
} else {
Cell::from("")
};
let mut row = Row::new(vec![
warnings,
Cell::from(id_width.update_str(format!(
"{:>width$}",
task.id_str(),
width = id_width.chars() as usize
))),
Cell::from(task.state().render(styles)),
Cell::from(name_width.update_str(task.name().unwrap_or("")).to_string()),
dur_cell(task.total(now)),
dur_cell(task.busy(now)),
dur_cell(task.idle(now)),
Cell::from(polls_width.update_str(task.total_polls().to_string())),
Cell::from(target_width.update_str(task.target()).to_owned()),
Cell::from(location_width.update_str(task.location()).to_owned()),
Cell::from(Spans::from(
task.formatted_fields()
.iter()
.flatten()
.cloned()
.collect::<Vec<_>>(),
)),
]);
if state == TaskState::Completed {
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!("Tasks ({}) ", table_list_state.len())),
TaskState::Running.render(styles),
Span::from(format!(" Running ({}) ", num_running)),
TaskState::Idle.render(styles),
Span::from(format!(" Idle ({})", num_idle)),
]);
/* TODO: use this to adjust the max size of name and target columns...
// How many characters wide are the fixed-length non-field columns?
let fixed_col_width = id_width.chars()
+ STATE_LEN
+ name_width.chars()
+ DUR_LEN as u16
+ DUR_LEN as u16
+ DUR_LEN as u16
+ POLLS_LEN as u16
+ target_width.chars();
*/
let warnings = state
.tasks_state()
.warnings()
.map(|warning| {
ListItem::new(Text::from(Spans::from(vec![
styles.warning_wide(),
// TODO(eliza): it would be nice to handle singular vs plural...
Span::from(format!("{} {}", warning.count(), warning.summary())),
])))
})
.collect::<Vec<_>>();
let layout = layout::Layout::default()
.direction(layout::Direction::Vertical)
.margin(0);
let controls = table::Controls::for_area(&area, styles);
let (controls_area, tasks_area, warnings_area) = if warnings.is_empty() {
let chunks = layout
.constraints(
[
layout::Constraint::Length(controls.height),
layout::Constraint::Max(area.height),
]
.as_ref(),
)
.split(area);
(chunks[0], chunks[1], None)
} else {
let warnings_height = warnings.len() as u16 + 2;
let chunks = layout
.constraints(
[
layout::Constraint::Length(controls.height),
layout::Constraint::Length(warnings_height),
layout::Constraint::Max(area.height),
]
.as_ref(),
)
.split(area);
(chunks[0], chunks[2], Some(chunks[1]))
};
// Fill all remaining characters in the frame with the task's fields.
//
// Ideally we'd use Min(0), and it would fill the rest of the space. But that is broken
// in tui 0.16. We can use Percentage to fill the space for now.
//
// See https://github.com/fdehau/tui-rs/issues/525
let fields_width = layout::Constraint::Percentage(100);
let widths = &[
warn_width.constraint(),
id_width.constraint(),
layout::Constraint::Length(state_len),
name_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(),
target_width.constraint(),
location_width.constraint(),
fields_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, tasks_area, &mut table_list_state.table_state);
frame.render_widget(controls.paragraph, controls_area);
if let Some(area) = warnings_area {
let block = styles
.border_block()
.title(Spans::from(vec![bold("Warnings")]));
frame.render_widget(widgets::List::new(warnings).block(block), area);
}
table_list_state
.sorted_items
.retain(|t| t.upgrade().is_some());
}
}