Skip to content

Commit

Permalink
collapse immediate children on '='
Browse files Browse the repository at this point in the history
Summary:
It's often useful to collapse all children of the currently selected cgroup.
Currently, the only way to achieve that is to go manually through all of them and collapse them with `<Enter>`
Add a new hotkey '=' to do the same.

Reviewed By: dschatzberg

Differential Revision: D63396627

fbshipit-source-id: b022d83e52ab4359ca6c90e06e25cefa61b63178
  • Loading branch information
Andrew Onyshchuk authored and facebook-github-bot committed Sep 26, 2024
1 parent b3f62f8 commit 636bb2a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
18 changes: 13 additions & 5 deletions below/model/src/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ impl QueriableContainer for CgroupModel {
Some((&s[..idx_end + 1], &s[idx_end + 2..]))
}
fn get_item(&self, idx: &Self::Idx) -> Option<&SingleCgroupModel> {
let mut model = self;
for part in idx.path.iter() {
model = model.children.get(part.as_str())?;
}
Some(&model.data)
self.get_by_path_iter(idx.path.iter())
.map(|model| &model.data)
}
}

Expand Down Expand Up @@ -274,6 +271,17 @@ impl CgroupModel {
});
self
}

fn get_by_path_iter(
&self,
mut path: impl Iterator<Item = impl AsRef<str>>,
) -> Option<&CgroupModel> {
path.try_fold(self, |cur, p| cur.children.get(p.as_ref()))
}

pub fn get_by_path_str(&self, path: &str) -> Option<&CgroupModel> {
self.get_by_path_iter(path.split('/').filter(|x| !x.is_empty()))
}
}

impl Nameable for CgroupModel {
Expand Down
28 changes: 20 additions & 8 deletions below/view/src/cgroup_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ impl CgroupState {
self.uncollapse_cgroup(cgroup.as_str());
self.cgroup_to_focus = Some(cgroup);
}

pub fn collapse_selected_cgroup_children(&mut self) {
if let Some(cur) = self
.get_model()
.get_by_path_str(&self.current_selected_cgroup)
{
self.collapsed_cgroups
.borrow_mut()
.extend(cur.children.iter().map(|c| &c.data.full_path).cloned());
}
}
}

// TODO: Make CgroupView a collection of CgroupTab
Expand Down Expand Up @@ -336,6 +347,11 @@ impl CgroupView {
view.state.borrow_mut().set_reverse(true);
view.refresh(c)
})
.on_event('=', |c| {
let mut view = Self::get_cgroup_view(c);
view.state.borrow_mut().collapse_selected_cgroup_children();
view.refresh(c);
})
.with_name(Self::get_view_name())
}

Expand Down Expand Up @@ -394,14 +410,10 @@ impl ViewBridge for CgroupView {
} else {
view.get_tag_from_tab_idx(current_tab, selected_column)
};
let field_str = selected_key
.split('/')
// Ignore leading slash
.skip(1)
// Traverse cgroup model tree to find matching model, or None
.try_fold(view.model.borrow(), |model, cgroup_name| {
Ref::filter_map(model, |model| model.children.get(cgroup_name)).ok()
})
let field_str = view
.model
.borrow()
.get_by_path_str(selected_key)
.and_then(|model| model.data.query(&tag))
.map_or("?".to_string(), |field| field.to_string());
format!(" {} : {} ", tag, field_str)
Expand Down
1 change: 1 addition & 0 deletions below/view/src/help_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn fill_reserved(v: &mut LinearLayout) {
" <Home> - scroll to top of primary display\n",
" <End> - scroll to end of primary display\n",
" <Enter> - collapse/expand cgroup tree, submit command if command palette activated\n",
" '=' - collapse immediate children of selected cgroup\n",
" <Ctrl>-r - refresh the screen",
" 'P' - sort by pid (process view only)\n",
" 'N' - sort by name (process view only)\n",
Expand Down

0 comments on commit 636bb2a

Please sign in to comment.