Skip to content

Commit ada7dab

Browse files
vrn21hds
andauthored
fix(console): add dynamic constraints layout in task details screen (#614)
The changes I've made is for accommodating long location names. I've changed the .. to the long name; it widens the task rectangle if its longer than the default (50%) and goes to the next line if its longer than what we can accommodate in a single line. This fixes issue #523, I have attached screenshots of the same in the PR, before and after changes. Fixes #523 Co-authored-by: Hayden Stainsby <[email protected]>
1 parent 59875fe commit ada7dab

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

tokio-console/src/view/task.rs

+47-31
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ impl TaskView {
6868
})
6969
.collect();
7070

71+
let location_heading = "Location: ";
72+
let max_width_stats_area = area.width - 45; //NOTE: 45 is min width needed, this is a calculated number according to the string: 'Last woken: 75.831727ms ago' but with some more extra pixels.
73+
let location_lines_vector: Vec<String> = task
74+
.location()
75+
.to_string()
76+
.chars()
77+
.collect::<Vec<char>>()
78+
.chunks(max_width_stats_area as usize)
79+
.map(|chunk| chunk.iter().collect())
80+
.collect();
81+
let task_stats_height = 9 + location_lines_vector.len() as u16;
82+
// Id, Name, Target, Location (multiple), total, busy, scheduled, and idle times + top/bottom borders
7183
let (
7284
controls_area,
7385
stats_area,
@@ -83,7 +95,7 @@ impl TaskView {
8395
// controls
8496
layout::Constraint::Length(controls.height()),
8597
// task stats
86-
layout::Constraint::Length(10),
98+
layout::Constraint::Length(task_stats_height),
8799
// poll duration
88100
layout::Constraint::Length(9),
89101
// scheduled duration
@@ -105,7 +117,7 @@ impl TaskView {
105117
// warnings (add 2 for top and bottom borders)
106118
layout::Constraint::Length(warnings.len() as u16 + 2),
107119
// task stats
108-
layout::Constraint::Length(10),
120+
layout::Constraint::Length(task_stats_height),
109121
// poll duration
110122
layout::Constraint::Length(9),
111123
// scheduled duration
@@ -127,15 +139,15 @@ impl TaskView {
127139
)
128140
};
129141

142+
let stats_constraints = [
143+
// 15 is the length of "| Location: |"
144+
layout::Constraint::Min(task.location().len() as u16 + 15),
145+
layout::Constraint::Min(32),
146+
];
147+
130148
let stats_area = Layout::default()
131149
.direction(layout::Direction::Horizontal)
132-
.constraints(
133-
[
134-
layout::Constraint::Percentage(50),
135-
layout::Constraint::Percentage(50),
136-
]
137-
.as_ref(),
138-
)
150+
.constraints(stats_constraints.as_ref())
139151
.split(stats_area);
140152

141153
// Just preallocate capacity for ID, name, target, total, busy, and idle.
@@ -152,17 +164,12 @@ impl TaskView {
152164

153165
overview.push(Line::from(vec![bold("Target: "), Span::raw(task.target())]));
154166

155-
let title = "Location: ";
156-
let location_max_width = stats_area[0].width as usize - 2 - title.len(); // NOTE: -2 for the border
157-
let location = if task.location().len() > location_max_width {
158-
let ellipsis = styles.if_utf8("\u{2026}", "...");
159-
let start = task.location().len() - location_max_width + ellipsis.chars().count();
160-
format!("{}{}", ellipsis, &task.location()[start..])
161-
} else {
162-
task.location().to_string()
163-
};
164-
165-
overview.push(Line::from(vec![bold(title), Span::raw(location)]));
167+
let location_vector = vec![bold(location_heading), Span::raw(&location_lines_vector[0])];
168+
overview.push(Line::from(location_vector));
169+
for line in &location_lines_vector[1..] {
170+
overview.push(Line::from(Span::raw(format!(" {}", line))));
171+
//10 spaces to be precise due to the Length of "Location: "
172+
}
166173

167174
let total = task.total(now);
168175

@@ -185,28 +192,37 @@ impl TaskView {
185192

186193
let mut waker_stats = vec![Line::from(vec![
187194
bold("Current wakers: "),
188-
Span::from(format!("{} (", task.waker_count())),
189-
bold("clones: "),
190-
Span::from(format!("{}, ", task.waker_clones())),
191-
bold("drops: "),
192-
Span::from(format!("{})", task.waker_drops())),
195+
Span::from(format!("{} ", task.waker_count())),
193196
])];
197+
let waker_stats_clones = vec![
198+
bold(" Clones: "),
199+
Span::from(format!("{}, ", task.waker_clones())),
200+
];
194201

195-
let mut wakeups = vec![
202+
let waker_stats_drops = vec![
203+
bold(" Drops: "),
204+
Span::from(format!("{}", task.waker_drops())),
205+
];
206+
207+
let wakeups = vec![
196208
bold("Woken: "),
197209
Span::from(format!("{} times", task.wakes())),
198210
];
199211

212+
let mut last_woken_line = vec![];
213+
200214
// If the task has been woken, add the time since wake to its stats as well.
201215
if let Some(since) = task.since_wake(now) {
202-
wakeups.reserve(3);
203-
wakeups.push(Span::raw(", "));
204-
wakeups.push(bold("last woken: "));
205-
wakeups.push(styles.time_units(since, view::DUR_LIST_PRECISION, None));
206-
wakeups.push(Span::raw(" ago"));
216+
last_woken_line.reserve(3);
217+
last_woken_line.push(bold("Last woken: "));
218+
last_woken_line.push(styles.time_units(since, view::DUR_LIST_PRECISION, None));
219+
last_woken_line.push(Span::raw(" ago"));
207220
}
208221

222+
waker_stats.push(Line::from(waker_stats_clones));
223+
waker_stats.push(Line::from(waker_stats_drops));
209224
waker_stats.push(Line::from(wakeups));
225+
waker_stats.push(Line::from(last_woken_line));
210226

211227
if task.self_wakes() > 0 {
212228
waker_stats.push(Line::from(vec![

0 commit comments

Comments
 (0)