Skip to content

Commit 0b31b96

Browse files
committed
bug: dynamic alloc of layout constraints in task details screen
Changes i've made is for accomadating loong location names, ive 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 we can accomadate in a single line. This fixes issue tokio-rs#523 i have attached screenshots of the same in the PR, before and after changes
1 parent 59875fe commit 0b31b96

File tree

1 file changed

+73
-23
lines changed

1 file changed

+73
-23
lines changed

tokio-console/src/view/task.rs

+73-23
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl TaskView {
127127
)
128128
};
129129

130-
let stats_area = Layout::default()
130+
let stats_area_check = Layout::default()
131131
.direction(layout::Direction::Horizontal)
132132
.constraints(
133133
[
@@ -138,6 +138,51 @@ impl TaskView {
138138
)
139139
.split(stats_area);
140140

141+
let mut location_lines_vector: Vec<String> = vec![];
142+
let title = "Location: ";
143+
let location_max_width = stats_area_check[0].width as usize - 2 - title.len(); // NOTE: -2 for the border
144+
let stats_area = if task.location().len() > location_max_width {
145+
let max_width_stats_area = area.width - 45;
146+
if max_width_stats_area < task.location().len() as u16 {
147+
location_lines_vector = task
148+
.location()
149+
.to_string()
150+
.chars()
151+
.collect::<Vec<char>>()
152+
.chunks(max_width_stats_area as usize)
153+
.map(|chunk| chunk.iter().collect())
154+
.collect();
155+
156+
let area_needed_to_render_location = task.location().len() as u16;
157+
Layout::default()
158+
.direction(layout::Direction::Horizontal)
159+
.constraints(
160+
[
161+
layout::Constraint::Min(area_needed_to_render_location + 15), //Note: 15 is the length of "| Location: |"
162+
layout::Constraint::Min(32),
163+
]
164+
.as_ref(),
165+
)
166+
.split(stats_area)
167+
} else {
168+
let area_needed_to_render_location = task.location().len() as u16;
169+
location_lines_vector.push(task.location().to_string());
170+
Layout::default()
171+
.direction(layout::Direction::Horizontal)
172+
.constraints(
173+
[
174+
layout::Constraint::Min(area_needed_to_render_location + 15), //Note: 15 is the length of "| Location: |"
175+
layout::Constraint::Min(32),
176+
]
177+
.as_ref(),
178+
)
179+
.split(stats_area)
180+
}
181+
} else {
182+
location_lines_vector.push(task.location().to_string());
183+
stats_area_check
184+
};
185+
141186
// Just preallocate capacity for ID, name, target, total, busy, and idle.
142187
let mut overview = Vec::with_capacity(8);
143188
overview.push(Line::from(vec![
@@ -152,17 +197,13 @@ impl TaskView {
152197

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

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)]));
200+
let first_line = location_lines_vector[0].clone();
201+
location_lines_vector.remove(0);
202+
let location_vector = vec![bold(title), Span::raw(first_line)];
203+
overview.push(Line::from(location_vector));
204+
for line in location_lines_vector {
205+
overview.push(Line::from(Span::raw(format!(" {}", line))));
206+
}
166207

167208
let total = task.total(now);
168209

@@ -185,28 +226,37 @@ impl TaskView {
185226

186227
let mut waker_stats = vec![Line::from(vec![
187228
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())),
229+
Span::from(format!("{} ", task.waker_count())),
193230
])];
231+
let waker_stats_clones = vec![
232+
bold(" Clones: "),
233+
Span::from(format!("{}, ", task.waker_clones())),
234+
];
235+
236+
let waker_stats_drops = vec![
237+
bold(" Drops: "),
238+
Span::from(format!("{}", task.waker_drops())),
239+
];
194240

195-
let mut wakeups = vec![
241+
let wakeups = vec![
196242
bold("Woken: "),
197243
Span::from(format!("{} times", task.wakes())),
198244
];
199245

246+
let mut last_woken_line = vec![];
247+
200248
// If the task has been woken, add the time since wake to its stats as well.
201249
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"));
250+
last_woken_line.reserve(3);
251+
last_woken_line.push(bold("Last woken: "));
252+
last_woken_line.push(styles.time_units(since, view::DUR_LIST_PRECISION, None));
253+
last_woken_line.push(Span::raw(" ago"));
207254
}
208255

256+
waker_stats.push(Line::from(waker_stats_clones));
257+
waker_stats.push(Line::from(waker_stats_drops));
209258
waker_stats.push(Line::from(wakeups));
259+
waker_stats.push(Line::from(last_woken_line));
210260

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

0 commit comments

Comments
 (0)