Skip to content

Commit

Permalink
fix: avoid crash when accessing selected item
Browse files Browse the repository at this point in the history
We should check the length before using the index to
access it.
  • Loading branch information
Rustin170506 committed Jul 8, 2024
1 parent a6cf14b commit ab8f322
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
54 changes: 31 additions & 23 deletions tokio-console/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ impl View {
// mutate the currently selected view.
match event {
key!(Enter) => {
if let Some(task) = self.tasks_list.selected_item().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_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(),
));
}
}
}
_ => {
Expand All @@ -149,9 +151,12 @@ impl View {
ResourcesList => {
match event {
key!(Enter) => {
if let Some(res) = self.resources_list.selected_item().upgrade() {
update_kind = UpdateKind::SelectResource(res.borrow().span_id());
self.state = ResourceInstance(self::resource::ResourceView::new(res));
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));
}
}
}
_ => {
Expand All @@ -169,21 +174,24 @@ impl View {
update_kind = UpdateKind::Other;
}
key!(Enter) => {
if let Some(op) = view.async_ops_table.selected_item().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_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(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(),
));
}
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions tokio-console/src/view/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,20 @@ impl<T: TableList<N>, const N: usize> TableListState<T, N> {
self.scroll_with(|_, _| 0)
}

pub(in crate::view) fn selected_item(&self) -> Weak<RefCell<T::Row>> {
self.table_state
.selected()
.map(|i| {
let selected = if self.sort_descending {
i
} else {
self.sorted_items.len() - i - 1
};
self.sorted_items[selected].clone()
})
.unwrap_or_default()
}
pub(in crate::view) fn selected_item(&self) -> Option<Weak<RefCell<T::Row>>> {
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 {
let adjusted_index = self.sorted_items.len().checked_sub(i + 1)?;
self.sorted_items.get(adjusted_index).cloned()
}
})
}

pub(in crate::view) fn render(
&mut self,
Expand Down

0 comments on commit ab8f322

Please sign in to comment.