From ddb7564809dd068deabf48513e29de40bba0df3f Mon Sep 17 00:00:00 2001 From: wongjiahau Date: Mon, 13 Feb 2023 17:58:25 +0800 Subject: [PATCH] feat(explore): add help --- helix-term/src/ui/explore.rs | 78 +++++++++++++++++++++++++++--------- helix-term/src/ui/tree.rs | 19 +++++++++ 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/helix-term/src/ui/explore.rs b/helix-term/src/ui/explore.rs index 1f2d31bb..179ca418 100644 --- a/helix-term/src/ui/explore.rs +++ b/helix-term/src/ui/explore.rs @@ -254,6 +254,7 @@ impl State { pub struct Explorer { tree: TreeView, + show_help: bool, state: State, prompt: Option<(PromptAction, Prompt)>, #[allow(clippy::type_complexity)] @@ -267,6 +268,7 @@ impl Explorer { let current_root = std::env::current_dir().unwrap_or_else(|_| "./".into()); Ok(Self { tree: Self::new_tree(current_root.clone())?, + show_help: false, state: State::new(true, current_root), repeat_motion: None, prompt: None, @@ -365,27 +367,47 @@ impl Explorer { surface.set_stringn( head_area.x, head_area.y, - path_str, + if self.show_help { + "[HELP]".to_string() + } else { + path_str + }, head_area.width as usize, get_theme!(editor.theme, "ui.explorer.dir", "ui.text"), ); let body_area = area.clip_top(2); let style = editor.theme.get("ui.text"); - if let Ok(preview_content) = get_preview(&item.path, body_area.height as usize) { - preview_content - .into_iter() - .enumerate() - .for_each(|(row, line)| { - surface.set_stringn( - body_area.x, - body_area.y + row as u16, - line, - body_area.width as usize, - style, - ); - }) - } + let content = if self.show_help { + vec![ + "? Toggle help", + "a Add file", + "A Add folder", + "r Rename file/folder", + "d Delete file", + "/ Search", + "f Filter", + "[ Change root to parent", + "] Change root to current", + "R Refresh tree", + ] + .into_iter() + .map(|s| s.to_string()) + .chain(ui::tree::tree_view_help()) + .collect() + } else { + get_preview(&item.path, body_area.height as usize) + .unwrap_or_else(|err| vec![err.to_string()]) + }; + content.into_iter().enumerate().for_each(|(row, line)| { + surface.set_stringn( + body_area.x, + body_area.y + row as u16, + line, + body_area.width as usize, + style, + ); + }) } fn new_search_prompt(&mut self, search_next: bool) { @@ -596,7 +618,15 @@ impl Explorer { self.render_preview(preview_area, surface, cx.editor); let list_area = render_block(area.clip_right(preview_area.width), surface, Borders::RIGHT); - self.tree.render(list_area, surface, cx, &mut self.state); + surface.set_stringn( + list_area.x, + list_area.y, + " Explorer: press ? for help", + list_area.width.into(), + cx.editor.theme.get("ui.text"), + ); + self.tree + .render(list_area.clip_top(1), surface, cx, &mut self.state); } pub fn render_embed( @@ -632,7 +662,15 @@ impl Explorer { render_block(side_area.clip_right(1), surface, Borders::LEFT).clip_bottom(1) } }; - self.tree.render(list_area, surface, cx, &mut self.state); + surface.set_stringn( + list_area.x, + list_area.y, + " Explorer: press ? for help", + list_area.width.into(), + cx.editor.theme.get("ui.text"), + ); + self.tree + .render(list_area.clip_top(1), surface, cx, &mut self.state); { let statusline = if self.is_focus() { @@ -871,6 +909,10 @@ impl Explorer { } Ok(()) } + + fn toggle_help(&mut self) { + self.show_help = !self.show_help + } } impl Component for Explorer { @@ -915,7 +957,7 @@ impl Component for Explorer { } key!('f') => self.new_filter_prompt(), key!('/') => self.new_search_prompt(true), - key!('?') => self.new_search_prompt(false), + key!('?') => self.toggle_help(), key!('a') => { if let Err(error) = self.new_create_file_prompt() { cx.editor.set_error(error.to_string()) diff --git a/helix-term/src/ui/tree.rs b/helix-term/src/ui/tree.rs index 4a42c63d..bd5eff7a 100644 --- a/helix-term/src/ui/tree.rs +++ b/helix-term/src/ui/tree.rs @@ -484,6 +484,25 @@ impl TreeView { } } +pub fn tree_view_help() -> Vec { + vec![ + "j Down", + "k Up", + "h Go to parent", + "l Expand", + "zz Align view center", + "zt Align view top", + "zb Align view bottom", + "gg Go to top", + "ge Go to end", + "^d Page down", + "^u Page up", + ] + .into_iter() + .map(|s| s.to_string()) + .collect() +} + impl TreeView { pub fn on_enter(&mut self, cx: &mut Context, params: &mut T::Params, selected_index: usize) { // if let Some(next_level) = self.next_item().map(|elem| elem.level) {