Skip to content

Commit f5f9fea

Browse files
feat: add getter for selected task (#7614)
### Description Add public method to return which task is currently selected ### Testing Instructions Expanded existing unit tests Closes TURBO-2521
1 parent cb3ca89 commit f5f9fea

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

crates/turborepo-ui/src/tui/table.rs

+28
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ impl TaskTable {
153153
self.scroll.select(Some(i));
154154
}
155155

156+
pub fn selected(&self) -> Option<&str> {
157+
let i = self.scroll.selected()?;
158+
if i < self.finished.len() {
159+
let task = self.finished.get(i)?;
160+
Some(task.name())
161+
} else if i < self.finished.len() + self.running.len() {
162+
let task = self.running.get(i - self.finished.len())?;
163+
Some(task.name())
164+
} else if i < self.finished.len() + self.running.len() + self.planned.len() {
165+
let task = self
166+
.planned
167+
.get(i - (self.finished.len() + self.running.len()))?;
168+
Some(task.name())
169+
} else {
170+
None
171+
}
172+
}
173+
156174
fn finished_rows(&self, duration_width: u16) -> impl Iterator<Item = Row> + '_ {
157175
self.finished.iter().map(move |task| {
158176
Row::new(vec![
@@ -288,12 +306,16 @@ mod test {
288306
table.next();
289307
table.next();
290308
assert_eq!(table.scroll.selected(), Some(1), "selected b");
309+
assert_eq!(table.selected(), Some("b"), "selected b");
291310
table.start_task("b").unwrap();
292311
assert_eq!(table.scroll.selected(), Some(0), "b stays selected");
312+
assert_eq!(table.selected(), Some("b"), "selected b");
293313
table.start_task("a").unwrap();
294314
assert_eq!(table.scroll.selected(), Some(0), "b stays selected");
315+
assert_eq!(table.selected(), Some("b"), "selected b");
295316
table.finish_task("a").unwrap();
296317
assert_eq!(table.scroll.selected(), Some(1), "b stays selected");
318+
assert_eq!(table.selected(), Some("b"), "selected b");
297319
}
298320

299321
#[test]
@@ -302,22 +324,28 @@ mod test {
302324
table.next();
303325
table.next();
304326
assert_eq!(table.scroll.selected(), Some(1), "selected b");
327+
assert_eq!(table.selected(), Some("b"), "selected b");
305328
// start c which moves it to "running" which is before "planned"
306329
table.start_task("c").unwrap();
307330
assert_eq!(table.scroll.selected(), Some(2), "selection stays on b");
331+
assert_eq!(table.selected(), Some("b"), "selected b");
308332
table.start_task("a").unwrap();
309333
assert_eq!(table.scroll.selected(), Some(2), "selection stays on b");
334+
assert_eq!(table.selected(), Some("b"), "selected b");
310335
// c
311336
// a
312337
// b <-
313338
table.previous();
314339
table.previous();
315340
assert_eq!(table.scroll.selected(), Some(0), "selected c");
341+
assert_eq!(table.selected(), Some("c"), "selected c");
316342
table.finish_task("a").unwrap();
317343
assert_eq!(table.scroll.selected(), Some(1), "c stays selected");
344+
assert_eq!(table.selected(), Some("c"), "selected c");
318345
table.previous();
319346
table.finish_task("c").unwrap();
320347
assert_eq!(table.scroll.selected(), Some(0), "a stays selected");
348+
assert_eq!(table.selected(), Some("a"), "selected a");
321349
}
322350

323351
#[test]

0 commit comments

Comments
 (0)