diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-10-09 14:37:38 +0200 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-10-09 14:37:38 +0200 |
commit | aa6f693e456350fd6ebf85d764da371144ebd512 (patch) | |
tree | a108a55ae0eab59c5793427566bf924ce8c9beca | |
parent | 6022e828c288ca1382594255c3a8eb32d5b8cb64 (diff) |
mode shortcut with predefined shortcutsshortcuts
-rw-r--r-- | readme.md | 2 | ||||
-rw-r--r-- | src/actioner.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 9 | ||||
-rw-r--r-- | src/display.rs | 19 | ||||
-rw-r--r-- | src/event_char.rs | 2 | ||||
-rw-r--r-- | src/help.rs | 1 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/mode.rs | 3 | ||||
-rw-r--r-- | src/shortcut.rs | 67 | ||||
-rw-r--r-- | src/status.rs | 25 |
10 files changed, 133 insertions, 2 deletions
@@ -86,6 +86,8 @@ - [ ] display / event separation. use async and message passing between coroutines - [ ] rename file_window to content_window ? - [ ] integrate fzf or another fuzzy finder +- [ ] multiple tabs +- [ ] shortcuts ## BUGS diff --git a/src/actioner.rs b/src/actioner.rs index d62acd4..5db9dd3 100644 --- a/src/actioner.rs +++ b/src/actioner.rs @@ -45,6 +45,7 @@ impl Actioner { (keybindings.symlink, EventChar::Symlink), (keybindings.preview, EventChar::Preview), (keybindings.history, EventChar::History), + (keybindings.shortcut, EventChar::Shortcut), ]); Self { binds } } @@ -88,6 +89,7 @@ impl Actioner { Mode::Normal | Mode::Preview => status.event_up_one_row(), Mode::Jump => status.event_jumplist_prev(), Mode::History => status.event_history_prev(), + Mode::Shortcut => status.event_shortcut_prev(), Mode::Goto | Mode::Exec | Mode::Search => { status.completion.prev(); } @@ -101,6 +103,7 @@ impl Actioner { Mode::Normal | Mode::Preview => status.event_down_one_row(), Mode::Jump => status.event_jumplist_next(), Mode::History => status.event_history_next(), + Mode::Shortcut => status.event_shortcut_next(), Mode::Goto | Mode::Exec | Mode::Search => { status.completion.next(); } @@ -213,6 +216,7 @@ impl Actioner { Mode::RegexMatch => status.exec_regex(), Mode::Jump => status.exec_jump(), Mode::History => status.exec_history(), + Mode::Shortcut => status.exec_shortcut(), Mode::Normal | Mode::NeedConfirmation | Mode::Help | Mode::Sort | Mode::Preview => (), } @@ -254,7 +258,7 @@ impl Actioner { Some(event_char) => event_char.match_char(status), None => (), }, - Mode::Help | Mode::Preview => status.event_normal(), + Mode::Help | Mode::Preview | Mode::Shortcut => status.event_normal(), Mode::Jump => (), Mode::History => (), Mode::NeedConfirmation => { diff --git a/src/config.rs b/src/config.rs index a21b368..e6e2d13 100644 --- a/src/config.rs +++ b/src/config.rs @@ -50,7 +50,8 @@ use tuikit::attr::Color; /// jump: j /// nvim: i /// sort_by: O -/// preview: S +/// preview: P +/// shortcut: S #[derive(Debug, Clone)] pub struct Config { /// Color of every kind of file @@ -205,6 +206,8 @@ pub struct Keybindings { pub preview: char, /// Display a stack of visited directories pub history: char, + /// Display predefined shortcuts + pub shortcut: char, } impl Keybindings { @@ -289,6 +292,9 @@ impl Keybindings { if let Some(history) = yaml["history"].as_str().map(|s| s.to_string()) { self.history = history.chars().next().unwrap_or('H'); } + if let Some(shortcut) = yaml["shortcut"].as_str().map(|s| s.to_string()) { + self.shortcut = shortcut.chars().next().unwrap_or('G'); + } } /// Returns a new `Keybindings` instance with hardcoded values. @@ -320,6 +326,7 @@ impl Keybindings { symlink: 'S', preview: 'P', history: 'H', + shortcut: 'G', } } } diff --git a/src/display.rs b/src/display.rs index 31b3f69..fa91c60 100644 --- a/src/display.rs +++ b/src/display.rs @@ -58,6 +58,7 @@ impl Display { self.history(status); self.completion(status); self.preview(status); + self.shortcuts(status); } /// Reads and returns the `tuikit::term::Term` height. @@ -168,6 +169,7 @@ impl Display { } } } + /// Display the history of visited directories. fn history(&mut self, status: &Status) { if let Mode::History = status.mode { @@ -185,6 +187,23 @@ impl Display { } } + /// Display the predefined shortcuts. + fn shortcuts(&mut self, status: &Status) { + if let Mode::Shortcut = status.mode { + let _ = self.term.clear(); + let _ = self.term.print(0, 0, "Go to..."); + for (row, path) in status.shortcut.shortcuts.iter().enumerate() { + let mut attr = Attr::default(); + if row == status.shortcut.index { + attr.effect |= Effect::REVERSE; + } + let _ = self + .term + .print_with_attr(row + 1, 4, path.to_str().unwrap(), attr); + } + } + } + /// Display the possible completion items. The currently selected one is /// reversed. fn completion(&mut self, status: &Status) { diff --git a/src/event_char.rs b/src/event_char.rs index 74ac4a7..9c9583e 100644 --- a/src/event_char.rs +++ b/src/event_char.rs @@ -27,6 +27,7 @@ pub enum EventChar { Sort, Symlink, Preview, + Shortcut, } impl EventChar { @@ -58,6 +59,7 @@ impl EventChar { EventChar::Sort => status.event_sort(), EventChar::Symlink => status.event_symlink(), EventChar::Preview => status.event_preview(), + EventChar::Shortcut => status.event_shortcut(), } } } diff --git a/src/help.rs b/src/help.rs index 0d964d7..19dd845 100644 --- a/src/help.rs +++ b/src/help.rs @@ -43,6 +43,7 @@ P: preview this file j: JUMP O: SORT H: HISTORY + G: SHORTCUT Enter: Execute mode then NORMAL Esc: NORMAL "; @@ -11,5 +11,6 @@ pub mod input; pub mod last_edition; pub mod mode; pub mod preview; +pub mod shortcut; pub mod status; pub mod visited; diff --git a/src/mode.rs b/src/mode.rs index a2384fd..89bcb03 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -35,6 +35,8 @@ pub enum Mode { Preview, /// Display a stack of visited directories, History, + /// Display predefined shortcuts + Shortcut, } impl fmt::Debug for Mode { @@ -55,6 +57,7 @@ impl fmt::Debug for Mode { Mode::NeedConfirmation => write!(f, "Y/N :"), Mode::Sort => write!(f, "(N)ame (D)ate (S)ize (E)xt (R)ev :"), Mode::Preview => write!(f, "Preview : "), + Mode::Shortcut => write!(f, "Shortcut :"), } } } diff --git a/src/shortcut.rs b/src/shortcut.rs new file mode 100644 index 0000000..841d8fe --- /dev/null +++ b/src/shortcut.rs @@ -0,0 +1,67 @@ +use std::borrow::Borrow; +use std::{path::PathBuf, str::FromStr}; + +#[derive(Debug, Clone)] +pub struct Shortcut { + pub shortcuts: [PathBuf; 10], + pub index: usize, +} + +impl Default for Shortcut { + fn default() -> Self { + Self::new() + } +} + +impl Shortcut { + pub fn new() -> Self { + let expanded_cow_path = shellexpand::tilde("~"); + let expanded_target: &str = expanded_cow_path.borrow(); + let path = std::fs::canonicalize(expanded_target).unwrap(); + Self { + shortcuts: [ + PathBuf::from_str("/").unwrap(), + PathBuf::from_str("/dev").unwrap(), + PathBuf::from_str("/etc").unwrap(), + PathBuf::from_str("/media").unwrap(), + PathBuf::from_str("/mnt").unwrap(), + PathBuf::from_str("/opt").unwrap(), + PathBuf::from_str("/run/media").unwrap(), + PathBuf::from_str("/tmp").unwrap(), + PathBuf::from_str("/usr").unwrap(), + std::fs::canonicalize(path).unwrap(), + ], + index: 0, + } + } + + pub fn is_empty(&self) -> bool { + self.shortcuts.is_empty() + } + + pub fn len(&self) -> usize { + self.shortcuts.len() + } + + pub fn next(&mut self) { + if self.is_empty() { + self.index = 0; + } else { + self.index = (self.index + 1) % self.len() + } + } + + pub fn prev(&mut self) { + if self.is_empty() { + self.index = 0 + } else if self.index > 0 { + self.index -= 1; + } else { + self.index = self.len() - 1 + } + } + + pub fn selected(&self) -> PathBuf { + self.shortcuts[self.index].clone() + } +} diff --git a/src/status.rs b/src/status.rs index 7eb7170..0ee00bd 100644 --- a/src/status.rs +++ b/src/status.rs @@ -17,6 +17,7 @@ use crate::input::Input; use crate::last_edition::LastEdition; use crate::mode::Mode; use crate::preview::Preview; +use crate::shortcut::Shortcut; use crate::visited::History; /// Holds every thing about the current status of the application. @@ -56,6 +57,8 @@ pub struct Status { pub preview: Preview, /// Visited directories pub history: History, + /// Predefined shortcuts + pub shortcut: Shortcut, } impl Status { @@ -81,6 +84,7 @@ impl Status { let preview = Preview::Empty; let mut history = History::default(); history.push(&path); + let shortcut = Shortcut::default(); Self { mode, line_index, @@ -98,6 +102,7 @@ impl Status { must_quit, preview, history, + shortcut, } } @@ -209,6 +214,14 @@ impl Status { } } + pub fn event_shortcut_next(&mut self) { + self.shortcut.next() + } + + pub fn event_shortcut_prev(&mut self) { + self.shortcut.prev() + } + pub fn event_history_next(&mut self) { self.history.next() } @@ -448,6 +461,10 @@ impl Status { self.mode = Mode::History } + pub fn event_shortcut(&mut self) { + self.mode = Mode::Shortcut + } + pub fn event_right_click(&mut self, row: u16) { if self.path_content.files.is_empty() || row as usize > self.path_content.files.len() { return; @@ -669,6 +686,14 @@ impl Status { self.window.scroll_to(self.line_index); } + pub fn exec_shortcut(&mut self) { + self.input.reset(); + let path = self.shortcut.selected(); + self.history.push(&path); + self.path_content = PathContent::new(path, self.show_hidden); + self.event_normal(); + } + pub fn exec_history(&mut self) { self.input.reset(); if let Some(path) = self.history.selected() { |