Skip to content

Commit b04ad50

Browse files
committed
bug: dynamic alloc of layout constraints, requested changes changed
please refer to earlier commits to see what changes have been made
1 parent 0f79e65 commit b04ad50

File tree

1 file changed

+55
-126
lines changed

1 file changed

+55
-126
lines changed

tokio-console/src/view/task.rs

Lines changed: 55 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl TaskView {
3434
pub(crate) fn update_input(&mut self, _event: input::Event) {
3535
// TODO :D
3636
}
37+
3738
pub(crate) fn render(
3839
&mut self,
3940
styles: &view::Styles,
@@ -67,144 +68,74 @@ impl TaskView {
6768
})
6869
.collect();
6970

70-
let stats_area_check = Layout::default()
71-
.direction(layout::Direction::Horizontal)
72-
.constraints(
73-
[
74-
layout::Constraint::Percentage(50),
75-
layout::Constraint::Percentage(50),
76-
]
77-
.as_ref(),
78-
)
79-
.split(area);
80-
8171
let location_heading = "Location: ";
82-
let location_max_width = stats_area_check[0].width as usize - 2 - location_heading.len(); // NOTE: -2 for the border
83-
let max_width_stats_area = area.width - 45;
84-
let mut location_lines_vector: Vec<String> = task
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
8574
.location()
8675
.to_string()
8776
.chars()
8877
.collect::<Vec<char>>()
8978
.chunks(max_width_stats_area as usize)
9079
.map(|chunk| chunk.iter().collect())
9180
.collect();
92-
let no_of_lines_extra_required_to_accomadate_location = location_lines_vector.len() - 1;
81+
// Id, Name, Target, Location (multiple), total, busy, scheduled, and idle times + top/bottom borders
9382
let (
9483
controls_area,
9584
stats_area,
9685
poll_dur_area,
9786
scheduled_dur_area,
9887
fields_area,
9988
warnings_area,
100-
) = if task.location().len() > location_max_width {
101-
if warnings.is_empty() {
102-
let chunks = Layout::default()
103-
.direction(layout::Direction::Vertical)
104-
.constraints(
105-
[
106-
// controls
107-
layout::Constraint::Length(controls.height()),
108-
// task stats
109-
layout::Constraint::Length(
110-
10 + no_of_lines_extra_required_to_accomadate_location as u16,
111-
),
112-
// poll duration
113-
layout::Constraint::Length(9),
114-
// scheduled duration
115-
layout::Constraint::Length(9),
116-
// fields
117-
layout::Constraint::Percentage(60),
118-
]
119-
.as_ref(),
120-
)
121-
.split(area);
122-
(chunks[0], chunks[1], chunks[2], chunks[3], chunks[4], None)
123-
} else {
124-
let chunks = Layout::default()
125-
.direction(layout::Direction::Vertical)
126-
.constraints(
127-
[
128-
// controls
129-
layout::Constraint::Length(controls.height()),
130-
// warnings (add 2 for top and bottom borders)
131-
layout::Constraint::Length(warnings.len() as u16 + 2),
132-
// task stats
133-
layout::Constraint::Length(
134-
10 + no_of_lines_extra_required_to_accomadate_location as u16,
135-
),
136-
// poll duration
137-
layout::Constraint::Length(9),
138-
// scheduled duration
139-
layout::Constraint::Length(9),
140-
// fields
141-
layout::Constraint::Percentage(60),
142-
]
143-
.as_ref(),
144-
)
145-
.split(area);
146-
147-
(
148-
chunks[0],
149-
chunks[2],
150-
chunks[3],
151-
chunks[4],
152-
chunks[5],
153-
Some(chunks[1]),
89+
) = if warnings.is_empty() {
90+
let chunks = Layout::default()
91+
.direction(layout::Direction::Vertical)
92+
.constraints(
93+
[
94+
// controls
95+
layout::Constraint::Length(controls.height()),
96+
// task stats
97+
layout::Constraint::Length((10 + location_lines_vector.len() - 1) as u16),
98+
// poll duration
99+
layout::Constraint::Length(9),
100+
// scheduled duration
101+
layout::Constraint::Length(9),
102+
// fields
103+
layout::Constraint::Percentage(60),
104+
]
105+
.as_ref(),
154106
)
155-
}
107+
.split(area);
108+
(chunks[0], chunks[1], chunks[2], chunks[3], chunks[4], None)
156109
} else {
157-
if warnings.is_empty() {
158-
let chunks = Layout::default()
159-
.direction(layout::Direction::Vertical)
160-
.constraints(
161-
[
162-
// controls
163-
layout::Constraint::Length(controls.height()),
164-
// task stats
165-
layout::Constraint::Length(10),
166-
// poll duration
167-
layout::Constraint::Length(9),
168-
// scheduled duration
169-
layout::Constraint::Length(9),
170-
// fields
171-
layout::Constraint::Percentage(60),
172-
]
173-
.as_ref(),
174-
)
175-
.split(area);
176-
(chunks[0], chunks[1], chunks[2], chunks[3], chunks[4], None)
177-
} else {
178-
let chunks = Layout::default()
179-
.direction(layout::Direction::Vertical)
180-
.constraints(
181-
[
182-
// controls
183-
layout::Constraint::Length(controls.height()),
184-
// warnings (add 2 for top and bottom borders)
185-
layout::Constraint::Length(warnings.len() as u16 + 2),
186-
// task stats
187-
layout::Constraint::Length(10),
188-
// poll duration
189-
layout::Constraint::Length(9),
190-
// scheduled duration
191-
layout::Constraint::Length(9),
192-
// fields
193-
layout::Constraint::Percentage(60),
194-
]
195-
.as_ref(),
196-
)
197-
.split(area);
198-
199-
(
200-
chunks[0],
201-
chunks[2],
202-
chunks[3],
203-
chunks[4],
204-
chunks[5],
205-
Some(chunks[1]),
110+
let chunks = Layout::default()
111+
.direction(layout::Direction::Vertical)
112+
.constraints(
113+
[
114+
// controls
115+
layout::Constraint::Length(controls.height()),
116+
// warnings (add 2 for top and bottom borders)
117+
layout::Constraint::Length(warnings.len() as u16 + 2),
118+
// task stats
119+
layout::Constraint::Length((10 + location_lines_vector.len() - 1) as u16),
120+
// poll duration
121+
layout::Constraint::Length(9),
122+
// scheduled duration
123+
layout::Constraint::Length(9),
124+
// fields
125+
layout::Constraint::Percentage(60),
126+
]
127+
.as_ref(),
206128
)
207-
}
129+
.split(area);
130+
131+
(
132+
chunks[0],
133+
chunks[2],
134+
chunks[3],
135+
chunks[4],
136+
chunks[5],
137+
Some(chunks[1]),
138+
)
208139
};
209140

210141
let stats_area = if location_lines_vector.len() != 1 {
@@ -230,7 +161,6 @@ impl TaskView {
230161
.as_ref(),
231162
)
232163
.split(stats_area)
233-
// stats_area_check
234164
};
235165

236166
// Just preallocate capacity for ID, name, target, total, busy, and idle.
@@ -247,12 +177,11 @@ impl TaskView {
247177

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

250-
let first_line = location_lines_vector[0].clone();
251-
location_lines_vector.remove(0);
252-
let location_vector = vec![bold(location_heading), Span::raw(first_line)];
180+
let location_vector = vec![bold(location_heading), Span::raw(&location_lines_vector[0])];
253181
overview.push(Line::from(location_vector));
254-
for line in location_lines_vector {
255-
overview.push(Line::from(Span::raw(format!(" {}", line))));
182+
for line in &location_lines_vector[1..] {
183+
overview.push(Line::from(Span::raw(format!(" {}", line))));
184+
//10 spaces to be precise due to the Length of "Location: "
256185
}
257186

258187
let total = task.total(now);

0 commit comments

Comments
 (0)