Skip to content

Commit e2a1774

Browse files
authored
feat(console): add name to task details (console-rs#115)
Currently, the task details view doesn't actually show the task's name. This commit changes it to add the name if the task has one. If there's no name, the name is not displayed. I also changed `Task::name` to return an `Option` so that we can more easily detect whether or not the task has a name (rather than checking if the string is empty). Screenshot: ![image](https://user-images.githubusercontent.com/2796466/132244984-783d247c-45d6-4202-bae7-f768f7740d5f.png)
1 parent 4023ad3 commit e2a1774

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

console/src/tasks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ impl Task {
283283
&self.target
284284
}
285285

286-
pub(crate) fn name(&self) -> &str {
287-
self.name.as_ref().map(AsRef::as_ref).unwrap_or_default()
286+
pub(crate) fn name(&self) -> Option<&str> {
287+
self.name.as_ref().map(AsRef::as_ref)
288288
}
289289

290290
pub(crate) fn formatted_fields(&self) -> &[Vec<Span<'static>>] {

console/src/view/task.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl TaskView {
5555
.constraints(
5656
[
5757
layout::Constraint::Length(1),
58-
layout::Constraint::Length(7),
58+
layout::Constraint::Length(8),
5959
layout::Constraint::Length(9),
6060
layout::Constraint::Percentage(60),
6161
]
@@ -105,17 +105,33 @@ impl TaskView {
105105
Span::raw(" = quit"),
106106
]);
107107

108-
let attrs = Spans::from(vec![
108+
// Just preallocate capacity for ID, name, target, total, busy, and idle.
109+
let mut metrics = Vec::with_capacity(6);
110+
metrics.push(Spans::from(vec![
109111
bold("ID: "),
110112
Span::raw(format!("{} ", task.id())),
111113
task.state().render(styles),
112-
]);
113-
let target = Spans::from(vec![bold("Target: "), Span::raw(task.target())]);
114-
let total = Spans::from(vec![bold("Total Time: "), dur(styles, task.total(now))]);
115-
let busy = Spans::from(vec![bold("Busy: "), dur(styles, task.busy(now))]);
116-
let idle = Spans::from(vec![bold("Idle: "), dur(styles, task.idle(now))]);
114+
]));
117115

118-
let metrics = vec![attrs, target, total, busy, idle];
116+
if let Some(name) = task.name() {
117+
metrics.push(Spans::from(vec![bold("Name: "), Span::raw(name)]));
118+
}
119+
metrics.push(Spans::from(vec![
120+
bold("Target: "),
121+
Span::raw(task.target()),
122+
]));
123+
metrics.push(Spans::from(vec![
124+
bold("Total Time: "),
125+
dur(styles, task.total(now)),
126+
]));
127+
metrics.push(Spans::from(vec![
128+
bold("Busy: "),
129+
dur(styles, task.busy(now)),
130+
]));
131+
metrics.push(Spans::from(vec![
132+
bold("Idle: "),
133+
dur(styles, task.idle(now)),
134+
]));
119135

120136
let wakers = Spans::from(vec![
121137
bold("Current wakers: "),

console/src/view/tasks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl List {
148148
width = id_width.chars() as usize
149149
))),
150150
Cell::from(task.state().render(styles)),
151-
Cell::from(name_width.update_str(task.name().to_string())),
151+
Cell::from(name_width.update_str(task.name().unwrap_or("").to_string())),
152152
dur_cell(task.total(now)),
153153
dur_cell(task.busy(now)),
154154
dur_cell(task.idle(now)),

0 commit comments

Comments
 (0)