diff --git a/tokio-console/src/view/mod.rs b/tokio-console/src/view/mod.rs index 0c46471e2..e8fff249e 100644 --- a/tokio-console/src/view/mod.rs +++ b/tokio-console/src/view/mod.rs @@ -132,14 +132,12 @@ impl View { // mutate the currently selected view. match event { key!(Enter) => { - if let Some(task_weak) = self.tasks_list.selected_item() { - if let Some(task) = task_weak.upgrade() { - update_kind = UpdateKind::SelectTask(task.borrow().span_id()); - self.state = TaskInstance(self::task::TaskView::new( - task, - state.task_details_ref(), - )); - } + if let Some(task) = self.tasks_list.selected_item() { + update_kind = UpdateKind::SelectTask(task.borrow().span_id()); + self.state = TaskInstance(self::task::TaskView::new( + task, + state.task_details_ref(), + )); } } _ => { @@ -151,12 +149,9 @@ impl View { ResourcesList => { match event { key!(Enter) => { - if let Some(res_weak) = self.resources_list.selected_item() { - if let Some(res) = res_weak.upgrade() { - update_kind = UpdateKind::SelectResource(res.borrow().span_id()); - self.state = - ResourceInstance(self::resource::ResourceView::new(res)); - } + if let Some(res) = self.resources_list.selected_item() { + update_kind = UpdateKind::SelectResource(res.borrow().span_id()); + self.state = ResourceInstance(self::resource::ResourceView::new(res)); } } _ => { @@ -174,24 +169,21 @@ impl View { update_kind = UpdateKind::Other; } key!(Enter) => { - if let Some(op_weak) = view.async_ops_table.selected_item() { - if let Some(op) = op_weak.upgrade() { - if let Some(task_id) = op.borrow().task_id() { - let task = self - .tasks_list - .sorted_items - .iter() - .filter_map(|i| i.upgrade()) - .find(|t| task_id == t.borrow().id()); + if let Some(op) = view.async_ops_table.selected_item() { + if let Some(task_id) = op.borrow().task_id() { + let task = self + .tasks_list + .sorted_items + .iter() + .filter_map(|i| i.upgrade()) + .find(|t| task_id == t.borrow().id()); - if let Some(task) = task { - update_kind = - UpdateKind::SelectTask(task.borrow().span_id()); - self.state = TaskInstance(self::task::TaskView::new( - task, - state.task_details_ref(), - )); - } + if let Some(task) = task { + update_kind = UpdateKind::SelectTask(task.borrow().span_id()); + self.state = TaskInstance(self::task::TaskView::new( + task, + state.task_details_ref(), + )); } } } diff --git a/tokio-console/src/view/table.rs b/tokio-console/src/view/table.rs index 8fc0c5fe9..c3d03959b 100644 --- a/tokio-console/src/view/table.rs +++ b/tokio-console/src/view/table.rs @@ -13,7 +13,7 @@ use ratatui::{ use std::convert::TryFrom; use std::cell::RefCell; -use std::rc::Weak; +use std::rc::{Rc, Weak}; pub(crate) trait TableList { type Row; @@ -154,19 +154,22 @@ impl, const N: usize> TableListState { self.scroll_with(|_, _| 0) } - pub(in crate::view) fn selected_item(&self) -> Option>> { - self.table_state.selected().and_then(|i| { - if self.sort_descending { - if i < self.sorted_items.len() { - Some(self.sorted_items[i].clone()) + pub(in crate::view) fn selected_item(&self) -> Option>> { + self.table_state + .selected() + .and_then(|i| { + if self.sort_descending { + if i < self.sorted_items.len() { + Some(self.sorted_items[i].clone()) + } else { + None + } } else { - None + let adjusted_index = self.sorted_items.len().checked_sub(i + 1)?; + self.sorted_items.get(adjusted_index).cloned() } - } else { - let adjusted_index = self.sorted_items.len().checked_sub(i + 1)?; - self.sorted_items.get(adjusted_index).cloned() - } - }) + }) + .and_then(|weak| weak.upgrade()) } pub(in crate::view) fn render(