From 7a8923a3b66d7714307cf344a6ab094f51cd9f39 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 30 Aug 2023 10:03:29 +0800 Subject: [PATCH] feat: help --- app/src/context.rs | 12 +- app/src/executor.rs | 26 +++- app/src/help/bindings.rs | 55 ++++++++ app/src/help/layout.rs | 28 ++++ app/src/help/mod.rs | 5 + app/src/main.rs | 1 + app/src/root.rs | 5 + config/docs/keymap.md | 46 +++++- config/preset/keymap.toml | 275 ++++++++++++++++++++---------------- config/src/keymap/keymap.rs | 5 + core/src/help/help.rs | 76 ++++++++++ core/src/help/mod.rs | 5 + core/src/lib.rs | 1 + 13 files changed, 405 insertions(+), 135 deletions(-) create mode 100644 app/src/help/bindings.rs create mode 100644 app/src/help/layout.rs create mode 100644 app/src/help/mod.rs create mode 100644 core/src/help/help.rs create mode 100644 core/src/help/mod.rs diff --git a/app/src/context.rs b/app/src/context.rs index 652f255b..c6caa25d 100644 --- a/app/src/context.rs +++ b/app/src/context.rs @@ -1,4 +1,4 @@ -use core::{input::Input, manager::Manager, select::Select, tasks::Tasks, which::Which, Position}; +use core::{help::Help, input::Input, manager::Manager, select::Select, tasks::Tasks, which::Which, Position}; use config::keymap::KeymapLayer; use crossterm::terminal::WindowSize; @@ -8,8 +8,9 @@ use shared::Term; pub struct Ctx { pub manager: Manager, pub which: Which, - pub select: Select, + pub help: Help, pub input: Input, + pub select: Select, pub tasks: Tasks, } @@ -18,8 +19,9 @@ impl Ctx { Self { manager: Manager::make(), which: Default::default(), - select: Default::default(), + help: Default::default(), input: Default::default(), + select: Default::default(), tasks: Tasks::start(), } } @@ -67,6 +69,8 @@ impl Ctx { pub(super) fn layer(&self) -> KeymapLayer { if self.which.visible { KeymapLayer::Which + } else if self.help.visible { + KeymapLayer::Help } else if self.input.visible { KeymapLayer::Input } else if self.select.visible { @@ -80,6 +84,6 @@ impl Ctx { #[inline] pub(super) fn image_layer(&self) -> bool { - !matches!(self.layer(), KeymapLayer::Which | KeymapLayer::Tasks) + !matches!(self.layer(), KeymapLayer::Which | KeymapLayer::Help | KeymapLayer::Tasks) } } diff --git a/app/src/executor.rs b/app/src/executor.rs index 6028691a..7b3558bc 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -44,6 +44,7 @@ impl Executor { KeymapLayer::Tasks => Self::tasks(cx, e), KeymapLayer::Select => Self::select(cx, e), KeymapLayer::Input => Self::input(cx, e), + KeymapLayer::Help => Self::help(cx, e), KeymapLayer::Which => unreachable!(), }; } @@ -173,6 +174,9 @@ impl Executor { // Tasks "tasks_show" => cx.tasks.toggle(), + // Help + "help" => cx.help.toggle(cx.layer()), + _ => false, } } @@ -187,8 +191,9 @@ impl Executor { } "inspect" => cx.tasks.inspect(), - "cancel" => cx.tasks.cancel(), + + "help" => cx.help.toggle(cx.layer()), _ => false, } } @@ -202,6 +207,7 @@ impl Executor { if step > 0 { cx.select.next(step as usize) } else { cx.select.prev(step.unsigned_abs()) } } + "help" => cx.help.toggle(cx.layer()), _ => false, } } @@ -235,6 +241,8 @@ impl Executor { "undo" => cx.input.undo(), "redo" => cx.input.redo(), + + "help" => cx.help.toggle(cx.layer()), _ => false, }, InputMode::Insert => match exec.cmd.as_str() { @@ -243,4 +251,20 @@ impl Executor { }, } } + + fn help(cx: &mut Ctx, exec: &Exec) -> bool { + match exec.cmd.as_str() { + "close" => cx.help.toggle(cx.layer()), + "escape" => cx.help.escape(), + + "arrow" => { + let step = exec.args.get(0).and_then(|s| s.parse().ok()).unwrap_or(0); + cx.help.arrow(step) + } + + "filter" => cx.help.filter(), + + _ => false, + } + } } diff --git a/app/src/help/bindings.rs b/app/src/help/bindings.rs new file mode 100644 index 00000000..c273132a --- /dev/null +++ b/app/src/help/bindings.rs @@ -0,0 +1,55 @@ +use ratatui::{layout::{self, Constraint}, prelude::{Buffer, Direction, Rect}, style::{Modifier, Style}, widgets::{List, ListItem, Widget}}; + +use crate::context::Ctx; + +pub(super) struct Bindings<'a> { + cx: &'a Ctx, +} + +impl<'a> Bindings<'a> { + pub(super) fn new(cx: &'a Ctx) -> Self { Self { cx } } +} + +impl Widget for Bindings<'_> { + fn render(self, area: Rect, buf: &mut Buffer) { + let bindings = &self.cx.help.window(); + let cursor = self.cx.help.rel_cursor(); + + let col1 = bindings + .iter() + .enumerate() + .map(|(i, c)| { + let mut x = ListItem::new(c.on.iter().map(ToString::to_string).collect::()); + + if i == cursor { + x = x.style(Style::new().add_modifier(Modifier::UNDERLINED)); + } + x + }) + .collect::>(); + + let col2 = bindings + .iter() + .enumerate() + .map(|(i, c)| { + let mut x = + ListItem::new(c.exec.iter().map(ToString::to_string).collect::>().join("; ")); + + if i == cursor { + x = x.style(Style::new().add_modifier(Modifier::UNDERLINED)); + } + x + }) + .collect::>(); + + let chunks = layout::Layout::new() + .direction(Direction::Horizontal) + .constraints( + [Constraint::Ratio(1, 9), Constraint::Ratio(4, 9), Constraint::Ratio(4, 9)].as_ref(), + ) + .split(area); + + List::new(col1).render(chunks[0], buf); + List::new(col2).render(chunks[1], buf); + } +} diff --git a/app/src/help/layout.rs b/app/src/help/layout.rs new file mode 100644 index 00000000..f2250d6e --- /dev/null +++ b/app/src/help/layout.rs @@ -0,0 +1,28 @@ +use ratatui::{buffer::Buffer, layout::{self, Rect}, prelude::{Constraint, Direction}, style::{Color, Style}, widgets::{Clear, Paragraph, Widget}}; + +use super::Bindings; +use crate::Ctx; + +pub(crate) struct Layout<'a> { + cx: &'a Ctx, +} + +impl<'a> Layout<'a> { + pub fn new(cx: &'a Ctx) -> Self { Self { cx } } +} + +impl<'a> Widget for Layout<'a> { + fn render(self, area: Rect, buf: &mut Buffer) { + let chunks = layout::Layout::new() + .direction(Direction::Vertical) + .constraints([Constraint::Min(0), Constraint::Length(1)].as_ref()) + .split(area); + + Clear.render(area, buf); + Paragraph::new("manager.help") + .style(Style::new().fg(Color::Rgb(35, 39, 59)).bg(Color::Rgb(200, 211, 248))) + .render(chunks[1], buf); + + Bindings::new(self.cx).render(chunks[0], buf); + } +} diff --git a/app/src/help/mod.rs b/app/src/help/mod.rs new file mode 100644 index 00000000..30a7e99d --- /dev/null +++ b/app/src/help/mod.rs @@ -0,0 +1,5 @@ +mod bindings; +mod layout; + +use bindings::*; +pub use layout::*; diff --git a/app/src/main.rs b/app/src/main.rs index 89ff02c1..825752a5 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -4,6 +4,7 @@ mod app; mod context; mod executor; mod header; +mod help; mod input; mod logs; mod manager; diff --git a/app/src/root.rs b/app/src/root.rs index 0460be6a..3e1cc5cd 100644 --- a/app/src/root.rs +++ b/app/src/root.rs @@ -1,6 +1,7 @@ use ratatui::{buffer::Buffer, layout::{Constraint, Direction, Layout, Rect}, widgets::Widget}; use super::{header, input, manager, select, status, tasks, which, Ctx}; +use crate::help; pub(super) struct Root<'a> { cx: &'a Ctx, @@ -33,6 +34,10 @@ impl<'a> Widget for Root<'a> { input::Input::new(self.cx).render(area, buf); } + if self.cx.help.visible { + help::Layout::new(self.cx).render(area, buf); + } + if self.cx.which.visible { which::Which::new(self.cx).render(area, buf); } diff --git a/config/docs/keymap.md b/config/docs/keymap.md index 2bc77893..c3d591d9 100644 --- a/config/docs/keymap.md +++ b/config/docs/keymap.md @@ -16,6 +16,10 @@ - enter: Enter the child directory. - back: Go back to the previous directory. - forward: Go forward to the next directory. +- peek + + - `n`: Peek up or down at file contents in the preview. Use negative values to peek up and positive values to peek down. + - cd: Change the current directory. - `path`: the path to change to. @@ -54,11 +58,11 @@ - `--force`: Overwrite the destination file if it exists. - `--follow`: Copy the file pointed to by a symbolic link, rather than the link itself. Only valid during copying. -- remove: Move the file to the trash/recycle bin. +- remove: Move the files to the trash/recycle bin. - - `--permanently`: Permanently delete the file. + - `--permanently`: Permanently delete the files. -- create: Create a file or directory (ends with `/` for directory). +- create: Create a file or directory (ends with `/` for directories). - rename: Rename a file or directory. - copy: Copy the path of files or directories that are selected or hovered on. @@ -90,12 +94,23 @@ - `fzf`: Jump to a directory, or reveal a file using fzf. - `zoxide`: Jump to a directory using zoxide. +- sort + + - `by` + - `"alphabetical"`: Sort alphabetically, e.g. `1.md` < `10.md` < `2.md` + - `"created"`: Sort by creation time. + - `"modified"`: Sort by last modified time. + - `"natural"`: Sort naturally, e.g. `1.md` < `2.md` < `10.md` + - `"size"`: Sort by file size. + - `--reverse`: Display files in reverse order. + - `--dir_first`: Display directories first. + ### Tabs - tab_create - `path`: Create a new tab using the specified path. - - `--current`: Create a new tab based on the current directory. + - `--current`: Create a new tab using the current path. - tab_close @@ -112,7 +127,11 @@ ### Tasks -- tasks_show: Display the task manager. +- tasks_show: Show the task manager. + +### Help + +- help: Open the help menu. ## tasks @@ -120,7 +139,9 @@ - arrow: - `-1`: Move the cursor up 1 line. - `1`: Move the cursor down 1 line. +- inspect: Inspect the task. - cancel: Cancel the task. +- help: Open the help menu. ## select @@ -128,6 +149,7 @@ - `--submit`: Submit the selection. - arrow - `n`: Move the cursor up or down n lines. Negative value for up, positive value for down. +- help: Open the help menu. ## input @@ -135,7 +157,7 @@ - `--submit`: Submit the input. -- escape: Cancel visual mode and enter normal mode. +- escape: Go back the normal mode, or cancel input. - move: Move the cursor left or right. - `n`: Move the cursor n characters left or right. Negative value for left, positive value for right. @@ -155,7 +177,7 @@ - delete: Delete the selected characters. - - `--cut`: Cut the selected characters into the clipboard, instead of only deleting them. + - `--cut`: Cut the selected characters into clipboard, instead of only deleting them. - `--insert`: Delete and enter insert mode. - yank: Copy the selected characters. @@ -166,6 +188,8 @@ - undo: Undo the last operation. - redo: Redo the last operation. +- help: Open the help menu. + ### Insert mode - close: Cancel input. @@ -174,3 +198,11 @@ - escape: Cancel insert mode and enter normal mode. - backspace: Delete the character before the cursor. + +## Help + +- close: Hide the help menu. +- escape: Clear the filter, or hide the help menu. +- arrow + - `n`: Move the cursor up or down n lines. Negative value for up, positive value for down. +- filter: Apply a filter for the help items. diff --git a/config/preset/keymap.toml b/config/preset/keymap.toml index 960c26e0..23e31b93 100644 --- a/config/preset/keymap.toml +++ b/config/preset/keymap.toml @@ -1,185 +1,214 @@ [manager] keymap = [ - { on = [ "" ], exec = "escape" }, - { on = [ "q" ], exec = "quit" }, - # close current tab and exit if it is last tab - { on = [ "" ], exec = "close" }, + { on = [ "" ], exec = "escape", desc = "Exit visual mode, clear selected, or cancel search" }, + { on = [ "q" ], exec = "quit", desc = "Exit the process" }, + { on = [ "" ], exec = "close", desc = "Close the current tab, or quit if it's last tab" }, # Navigation - { on = [ "k" ], exec = "arrow -1" }, - { on = [ "j" ], exec = "arrow 1" }, + { on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "j" ], exec = "arrow 1", desc = "Move cursor down" }, - { on = [ "K" ], exec = "arrow -5" }, - { on = [ "J" ], exec = "arrow 5" }, + { on = [ "K" ], exec = "arrow -5", desc = "Move cursor up 5 lines" }, + { on = [ "J" ], exec = "arrow 5", desc = "Move cursor down 5 line" }, - { on = [ "h" ], exec = "leave" }, - { on = [ "l" ], exec = "enter" }, + { on = [ "h" ], exec = "leave", desc = "Go back to the parent directory" }, + { on = [ "l" ], exec = "enter", desc = "Enter the child directory" }, - { on = [ "H" ], exec = "back" }, - { on = [ "L" ], exec = "forward" }, + { on = [ "H" ], exec = "back", desc = "Go back to the previous directory" }, + { on = [ "L" ], exec = "forward", desc = "Go forward to the next directory" }, - { on = [ "" ], exec = "peek -5" }, - { on = [ "" ], exec = "peek 5" }, + { on = [ "" ], exec = "peek -5", desc = "Peek up 5 units in the preview" }, + { on = [ "" ], exec = "peek 5", desc = "Peek down 5 units in the preview" }, - { on = [ "" ], exec = "arrow -1" }, - { on = [ "" ], exec = "arrow 1" }, - { on = [ "" ], exec = "leave" }, - { on = [ "" ], exec = "enter" }, + { on = [ "" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "" ], exec = "arrow 1", desc = "Move cursor down" }, + { on = [ "" ], exec = "leave", desc = "Go back to the parent directory" }, + { on = [ "" ], exec = "enter", desc = "Enter the child directory" }, # Selection - { on = [ "" ], exec = [ "select --state=none", "arrow 1" ] }, - { on = [ "v" ], exec = "visual_mode" }, - { on = [ "V" ], exec = "visual_mode --unset" }, - { on = [ "" ], exec = "select_all --state=true" }, - { on = [ "" ], exec = "select_all --state=none" }, + { on = [ "" ], exec = [ "select --state=none", "arrow 1" ], desc = "Toggle the current selection state" }, + { on = [ "v" ], exec = "visual_mode", desc = "Enter visual mode (selection mode)" }, + { on = [ "V" ], exec = "visual_mode --unset", desc = "Enter visual mode (unset mode)" }, + { on = [ "" ], exec = "select_all --state=true", desc = "Select all files" }, + { on = [ "" ], exec = "select_all --state=none", desc = "Inverse selection of all files" }, # Operation - { on = [ "o" ], exec = "open" }, - { on = [ "O" ], exec = "open --interactive" }, - { on = [ "" ], exec = "open" }, - { on = [ "" ], exec = "open --interactive" }, # It's cool if you're using a terminal that supports CSI u - { on = [ "y" ], exec = "yank" }, - { on = [ "x" ], exec = "yank --cut" }, - { on = [ "p" ], exec = "paste" }, - { on = [ "P" ], exec = "paste --force" }, - { on = [ "k" ], exec = "paste --follow" }, - { on = [ "K" ], exec = "paste --follow --force" }, - { on = [ "d" ], exec = "remove" }, - { on = [ "D" ], exec = "remove --permanently" }, - { on = [ "a" ], exec = "create" }, - { on = [ "r" ], exec = "rename" }, - { on = [ ";" ], exec = "shell" }, - { on = [ ":" ], exec = "shell --block" }, - { on = [ "." ], exec = "hidden toggle" }, - { on = [ "s" ], exec = "search fd" }, - { on = [ "S" ], exec = "search rg" }, - { on = [ "" ], exec = "search none" }, - { on = [ "z" ], exec = "jump zoxide" }, - { on = [ "Z" ], exec = "jump fzf" }, + { on = [ "o" ], exec = "open", desc = "Open the selected files" }, + { on = [ "O" ], exec = "open --interactive", desc = "Open the selected files with an interactive UI to choose the opening method" }, + { on = [ "" ], exec = "open", desc = "Open the selected files" }, + { on = [ "" ], exec = "open --interactive", desc = "Open the selected files with an interactive UI to choose the opening method" }, # It's cool if you're using a terminal that supports CSI u + { on = [ "y" ], exec = "yank", desc = "Copy the selected files" }, + { on = [ "x" ], exec = "yank --cut", desc = "Cut the selected files" }, + { on = [ "p" ], exec = "paste", desc = "Paste the files" }, + { on = [ "P" ], exec = "paste --force", desc = "Paste the files (overwrite if the destination exists)" }, + { on = [ "k" ], exec = "paste --follow", desc = "Paste the files (follow the symlinks)" }, + { on = [ "K" ], exec = "paste --follow --force", desc = "Paste the files (overwrite + follow)" }, + { on = [ "d" ], exec = "remove", desc = "Move the files to the trash" }, + { on = [ "D" ], exec = "remove --permanently", desc = "Permanently delete the files" }, + { on = [ "a" ], exec = "create", desc = "Create a file or directory (ends with / for directories)" }, + { on = [ "r" ], exec = "rename", desc = "Rename a file or directory" }, + { on = [ ";" ], exec = "shell", desc = "Run a shell command" }, + { on = [ ":" ], exec = "shell --block", desc = "Run a shell command (block the UI until the command finishes)" }, + { on = [ "." ], exec = "hidden toggle", desc = "Toggle the visibility of hidden files" }, + { on = [ "s" ], exec = "search fd", desc = "Search files by content using ripgrep" }, + { on = [ "S" ], exec = "search rg", desc = "Search files by name using fd" }, + { on = [ "" ], exec = "search none", desc = "Cancel the ongoing search" }, + { on = [ "z" ], exec = "jump zoxide", desc = "Jump to a directory, or reveal a file using fzf" }, + { on = [ "Z" ], exec = "jump fzf", desc = "Jump to a directory using zoxide" }, # Copy - { on = [ "c", "c" ], exec = "copy path" }, - { on = [ "c", "d" ], exec = "copy dirname" }, - { on = [ "c", "f" ], exec = "copy filename" }, - { on = [ "c", "n" ], exec = "copy name_without_ext" }, + { on = [ "c", "c" ], exec = "copy path", desc = "Copy the full absolute path" }, + { on = [ "c", "d" ], exec = "copy dirname", desc = "Copy the path of the parent directory" }, + { on = [ "c", "f" ], exec = "copy filename", desc = "Copy the name of the file" }, + { on = [ "c", "n" ], exec = "copy name_without_ext", desc = "Copy the name of the file without the extension" }, # Sorting - { on = [ ",", "a" ], exec = "sort alphabetical --dir_first" }, - { on = [ ",", "A" ], exec = "sort alphabetical --reverse --dir_first" }, - { on = [ ",", "c" ], exec = "sort created --dir_first" }, - { on = [ ",", "C" ], exec = "sort created --reverse --dir_first" }, - { on = [ ",", "m" ], exec = "sort modified --dir_first" }, - { on = [ ",", "M" ], exec = "sort modified --reverse --dir_first" }, - { on = [ ",", "n" ], exec = "sort natural --dir_first" }, - { on = [ ",", "N" ], exec = "sort natural --reverse --dir_first" }, - { on = [ ",", "s" ], exec = "sort size --dir_first" }, - { on = [ ",", "S" ], exec = "sort size --reverse --dir_first" }, + { on = [ ",", "a" ], exec = "sort alphabetical --dir_first", desc = "Sort alphabetically, directories first" }, + { on = [ ",", "A" ], exec = "sort alphabetical --reverse --dir_first", desc = "Sort alphabetically, directories first (reverse)" }, + { on = [ ",", "c" ], exec = "sort created --dir_first", desc = "Sort by creation time, directories first" }, + { on = [ ",", "C" ], exec = "sort created --reverse --dir_first", desc = "Sort by creation time, directories first (reverse)" }, + { on = [ ",", "m" ], exec = "sort modified --dir_first", desc = "Sort by modified time, directories first" }, + { on = [ ",", "M" ], exec = "sort modified --reverse --dir_first", desc = "Sort by modified time, directories first (reverse)" }, + { on = [ ",", "n" ], exec = "sort natural --dir_first", desc = "Sort naturally, directories first" }, + { on = [ ",", "N" ], exec = "sort natural --reverse --dir_first", desc = "Sort naturally, directories first (reverse)" }, + { on = [ ",", "s" ], exec = "sort size --dir_first", desc = "Sort by size, directories first" }, + { on = [ ",", "S" ], exec = "sort size --reverse --dir_first", desc = "Sort by size, directories first (reverse)" }, # Tabs - { on = [ "t" ], exec = "tab_create --current" }, + { on = [ "t" ], exec = "tab_create --current", desc = "Create a new tab using the current path" }, - { on = [ "1" ], exec = "tab_switch 0" }, - { on = [ "2" ], exec = "tab_switch 1" }, - { on = [ "3" ], exec = "tab_switch 2" }, - { on = [ "4" ], exec = "tab_switch 3" }, - { on = [ "5" ], exec = "tab_switch 4" }, - { on = [ "6" ], exec = "tab_switch 5" }, - { on = [ "7" ], exec = "tab_switch 6" }, - { on = [ "8" ], exec = "tab_switch 7" }, - { on = [ "9" ], exec = "tab_switch 8" }, + { on = [ "1" ], exec = "tab_switch 0", desc = "Switch to the first tab" }, + { on = [ "2" ], exec = "tab_switch 1", desc = "Switch to the second tab" }, + { on = [ "3" ], exec = "tab_switch 2", desc = "Switch to the third tab" }, + { on = [ "4" ], exec = "tab_switch 3", desc = "Switch to the fourth tab" }, + { on = [ "5" ], exec = "tab_switch 4", desc = "Switch to the fifth tab" }, + { on = [ "6" ], exec = "tab_switch 5", desc = "Switch to the sixth tab" }, + { on = [ "7" ], exec = "tab_switch 6", desc = "Switch to the seventh tab" }, + { on = [ "8" ], exec = "tab_switch 7", desc = "Switch to the eighth tab" }, + { on = [ "9" ], exec = "tab_switch 8", desc = "Switch to the ninth tab" }, - { on = [ "[" ], exec = "tab_switch -1 --relative" }, - { on = [ "]" ], exec = "tab_switch 1 --relative" }, + { on = [ "[" ], exec = "tab_switch -1 --relative", desc = "Switch to the previous tab" }, + { on = [ "]" ], exec = "tab_switch 1 --relative", desc = "Switch to the next tab" }, - { on = [ "{" ], exec = "tab_swap -1" }, - { on = [ "}" ], exec = "tab_swap 1" }, + { on = [ "{" ], exec = "tab_swap -1", desc = "Swap the current tab with the previous tab" }, + { on = [ "}" ], exec = "tab_swap 1", desc = "Swap the current tab with the next tab" }, # Tasks - { on = [ "w" ], exec = "tasks_show" }, + { on = [ "w" ], exec = "tasks_show", desc = "Show the tasks manager" }, # Goto - { on = [ "g", "h" ], exec = "cd ~" }, - { on = [ "g", "c" ], exec = "cd ~/.config" }, - { on = [ "g", "d" ], exec = "cd ~/Downloads" }, - { on = [ "g", "t" ], exec = "cd /tmp" }, - { on = [ "g", "" ], exec = "cd --interactive" }, + { on = [ "g", "h" ], exec = "cd ~", desc = "Go to the home directory" }, + { on = [ "g", "c" ], exec = "cd ~/.config", desc = "Go to the config directory" }, + { on = [ "g", "d" ], exec = "cd ~/Downloads", desc = "Go to the downloads directory" }, + { on = [ "g", "t" ], exec = "cd /tmp", desc = "Go to the temporary directory" }, + { on = [ "g", "" ], exec = "cd --interactive", desc = "Go to a directory interactively" }, + + # Help + { on = [ "?" ], exec = "help", desc = "Open help" }, ] [tasks] keymap = [ - { on = [ "" ], exec = "close" }, - { on = [ "" ], exec = "close" }, - { on = [ "w" ], exec = "close" }, + { on = [ "" ], exec = "close", desc = "Hide the task manager" }, + { on = [ "" ], exec = "close", desc = "Hide the task manager" }, + { on = [ "w" ], exec = "close", desc = "Hide the task manager" }, - { on = [ "k" ], exec = "arrow -1" }, - { on = [ "j" ], exec = "arrow 1" }, + { on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "j" ], exec = "arrow 1", desc = "Move cursor down" }, - { on = [ "" ], exec = "arrow -1" }, - { on = [ "" ], exec = "arrow 1" }, + { on = [ "" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "" ], exec = "arrow 1", desc = "Move cursor down" }, - { on = [ "" ], exec = "inspect" }, - { on = [ "x" ], exec = "cancel" }, + { on = [ "" ], exec = "inspect", desc = "Inspect the task" }, + { on = [ "x" ], exec = "cancel", desc = "Cancel the task" }, + + { on = [ "?" ], exec = "help", desc = "Open help" } ] [select] keymap = [ - { on = [ "" ], exec = "close" }, - { on = [ "" ], exec = "close" }, - { on = [ "" ], exec = "close --submit" }, + { on = [ "" ], exec = "close", desc = "Cancel selection" }, + { on = [ "" ], exec = "close", desc = "Cancel selection" }, + { on = [ "" ], exec = "close --submit", desc = "Submit the selection" }, - { on = [ "k" ], exec = "arrow -1" }, - { on = [ "j" ], exec = "arrow 1" }, + { on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "j" ], exec = "arrow 1", desc = "Move cursor down" }, - { on = [ "K" ], exec = "arrow -5" }, - { on = [ "J" ], exec = "arrow 5" }, + { on = [ "K" ], exec = "arrow -5", desc = "Move cursor up 5 lines" }, + { on = [ "J" ], exec = "arrow 5", desc = "Move cursor down 5 lines" }, - { on = [ "" ], exec = "arrow -1" }, - { on = [ "" ], exec = "arrow 1" }, + { on = [ "" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "" ], exec = "arrow 1", desc = "Move cursor down" }, + + { on = [ "?" ], exec = "help", desc = "Open help" } ] [input] keymap = [ - { on = [ "" ], exec = "close" }, - { on = [ "" ], exec = "close --submit" }, - { on = [ "" ], exec = "escape" }, - { on = [ "" ], exec = "backspace" }, + { on = [ "" ], exec = "close", desc = "Cancel input" }, + { on = [ "" ], exec = "close --submit", desc = "Submit the input" }, + { on = [ "" ], exec = "escape", desc = "Go back the normal mode, or cancel input" }, + { on = [ "" ], exec = "backspace", desc = "Delete the character before the cursor" }, # Mode - { on = [ "i" ], exec = "insert" }, - { on = [ "a" ], exec = "insert --append" }, - { on = [ "v" ], exec = "visual" }, + { on = [ "i" ], exec = "insert", desc = "Enter insert mode" }, + { on = [ "a" ], exec = "insert --append", desc = "Enter append mode" }, + { on = [ "v" ], exec = "visual", desc = "Enter visual mode" }, # Navigation - { on = [ "h" ], exec = "move -1" }, - { on = [ "l" ], exec = "move 1" }, + { on = [ "h" ], exec = "move -1", desc = "Move cursor left" }, + { on = [ "l" ], exec = "move 1", desc = "Move cursor right" }, - { on = [ "0" ], exec = "move -999" }, - { on = [ "$" ], exec = "move 999" }, - { on = [ "I" ], exec = [ "move -999", "insert" ] }, - { on = [ "A" ], exec = [ "move 999", "insert --append" ] }, + { on = [ "0" ], exec = "move -999", desc = "Move to the BOL" }, + { on = [ "$" ], exec = "move 999", desc = "Move to the EOL" }, + { on = [ "I" ], exec = [ "move -999", "insert" ], desc = "Move to the BOL, and enter insert mode" }, + { on = [ "A" ], exec = [ "move 999", "insert --append" ], desc = "Move to the EOL, and enter append mode" }, - { on = [ "" ], exec = "move -1" }, - { on = [ "" ], exec = "move 1" }, + { on = [ "" ], exec = "move -1", desc = "Move cursor left" }, + { on = [ "" ], exec = "move 1", desc = "Move cursor right" }, - { on = [ "b" ], exec = "backward" }, - { on = [ "w" ], exec = "forward" }, - { on = [ "e" ], exec = "forward --end-of-word" }, + { on = [ "b" ], exec = "backward", desc = "Move to the beginning of the previous word" }, + { on = [ "w" ], exec = "forward", desc = "Move to the beginning of the next word" }, + { on = [ "e" ], exec = "forward --end-of-word", desc = "Move to the end of the next word" }, # Deletion - { on = [ "d" ], exec = "delete --cut" }, - { on = [ "c" ], exec = "delete --cut --insert" }, - { on = [ "x" ], exec = [ "delete --cut", "move 1 --in-operating" ] }, + { on = [ "d" ], exec = "delete --cut", desc = "Cut the selected characters" }, + { on = [ "c" ], exec = "delete --cut --insert", desc = "Cut the selected characters, and enter insert mode" }, + { on = [ "x" ], exec = [ "delete --cut", "move 1 --in-operating" ], desc = "Cut the current character" }, # Yank/Paste - { on = [ "y" ], exec = "yank" }, - { on = [ "p" ], exec = "paste" }, - { on = [ "P" ], exec = "paste --before" }, + { on = [ "y" ], exec = "yank", desc = "Copy the selected characters" }, + { on = [ "p" ], exec = "paste", desc = "Paste the copied characters after the cursor" }, + { on = [ "P" ], exec = "paste --before", desc = "Paste the copied characters before the cursor" }, # Undo/Redo - { on = [ "u" ], exec = "undo" }, - { on = [ "" ], exec = "redo" }, + { on = [ "u" ], exec = "undo", desc = "Undo the last operation" }, + { on = [ "" ], exec = "redo", desc = "Redo the last operation" }, + + # Help + { on = [ "?" ], exec = "help", desc = "Open help" } +] + +[help] + +keymap = [ + { on = [ "" ], exec = "close", desc = "Hide the help" }, + { on = [ "" ], exec = "escape", desc = "Clear the filter, or hide the help" }, + + # Navigation + { on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "j" ], exec = "arrow 1", desc = "Move cursor down" }, + + { on = [ "K" ], exec = "arrow -5", desc = "Move cursor up 5 lines" }, + { on = [ "J" ], exec = "arrow 5", desc = "Move cursor down 5 lines" }, + + { on = [ "" ], exec = "arrow -1", desc = "Move cursor up" }, + { on = [ "" ], exec = "arrow 1", desc = "Move cursor down" }, + + # Filtering + { on = [ "/" ], exec = "filter", desc = "Apply a filter for the help items" }, ] diff --git a/config/src/keymap/keymap.rs b/config/src/keymap/keymap.rs index 3c864368..392eb9f6 100644 --- a/config/src/keymap/keymap.rs +++ b/config/src/keymap/keymap.rs @@ -16,6 +16,7 @@ pub struct Keymap { pub tasks: Vec, pub select: Vec, pub input: Vec, + pub help: Vec, } #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] @@ -24,6 +25,7 @@ pub enum KeymapLayer { Tasks, Select, Input, + Help, Which, } @@ -38,6 +40,7 @@ impl<'de> Deserialize<'de> for Keymap { tasks: Inner, select: Inner, input: Inner, + help: Inner, } #[derive(Deserialize)] struct Inner { @@ -50,6 +53,7 @@ impl<'de> Deserialize<'de> for Keymap { tasks: shadow.tasks.keymap, select: shadow.select.keymap, input: shadow.input.keymap, + help: shadow.help.keymap, }) } } @@ -66,6 +70,7 @@ impl Keymap { KeymapLayer::Tasks => &self.tasks, KeymapLayer::Select => &self.select, KeymapLayer::Input => &self.input, + KeymapLayer::Help => &self.help, KeymapLayer::Which => unreachable!(), } } diff --git a/core/src/help/help.rs b/core/src/help/help.rs new file mode 100644 index 00000000..e34226c9 --- /dev/null +++ b/core/src/help/help.rs @@ -0,0 +1,76 @@ +use config::{keymap::{Control, KeymapLayer}, KEYMAP}; +use shared::Term; + +use super::HELP_MARGIN; +use crate::emit; + +#[derive(Default)] +pub struct Help { + pub visible: bool, + bindings: Vec, + + offset: usize, + cursor: usize, +} + +impl Help { + #[inline] + pub fn limit() -> usize { Term::size().rows.saturating_sub(HELP_MARGIN) as usize } + + pub fn toggle(&mut self, layer: KeymapLayer) -> bool { + self.visible = !self.visible; + self.bindings = if self.visible { KEYMAP.get(layer).clone() } else { Vec::new() }; + + emit!(Peek); // Show/hide preview for images + true + } + + pub fn escape(&mut self) -> bool { todo!() } + + #[inline] + pub fn arrow(&mut self, step: isize) -> bool { + if step > 0 { self.next(step as usize) } else { self.prev(step.unsigned_abs()) } + } + + pub fn next(&mut self, step: usize) -> bool { + let len = self.bindings.len(); + if len == 0 { + return false; + } + + let old = self.cursor; + self.cursor = (self.cursor + step).min(len - 1); + + let limit = Self::limit(); + if self.cursor >= (self.offset + limit).min(len).saturating_sub(5) { + self.offset = len.saturating_sub(limit).min(self.offset + self.cursor - old); + } + + old != self.cursor + } + + pub fn prev(&mut self, step: usize) -> bool { + let old = self.cursor; + self.cursor = self.cursor.saturating_sub(step); + + if self.cursor < self.offset + 5 { + self.offset = self.offset.saturating_sub(old - self.cursor); + } + + old != self.cursor + } + + #[inline] + pub fn window(&self) -> &[Control] { + let end = (self.offset + Self::limit()).min(self.bindings.len()); + &self.bindings[self.offset..end] + } + + pub fn filter(&mut self) -> bool { todo!() } +} + +impl Help { + // --- Cursor + #[inline] + pub fn rel_cursor(&self) -> usize { self.cursor - self.offset } +} diff --git a/core/src/help/mod.rs b/core/src/help/mod.rs new file mode 100644 index 00000000..6525211a --- /dev/null +++ b/core/src/help/mod.rs @@ -0,0 +1,5 @@ +mod help; + +pub use help::*; + +pub const HELP_MARGIN: u16 = 1; diff --git a/core/src/lib.rs b/core/src/lib.rs index 0b819f3d..3696401f 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -10,6 +10,7 @@ mod blocker; mod event; pub mod external; pub mod files; +pub mod help; mod highlighter; pub mod input; pub mod manager;