Skip to content

Commit

Permalink
refactor: using definite patterns to simplify keybindings (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Feb 17, 2024
1 parent 53525b5 commit 1ec9943
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 179 deletions.
58 changes: 29 additions & 29 deletions yazi-config/preset/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ keymap = [
{ on = [ "<PageUp>" ], exec = "arrow -100%", desc = "Move cursor up one page" },
{ on = [ "<PageDown>" ], exec = "arrow 100%", desc = "Move cursor down one page" },

{ on = [ "h" ], exec = [ "escape --visual", "leave" ], desc = "Go back to the parent directory" },
{ on = [ "l" ], exec = [ "escape --visual", "enter" ], desc = "Enter the child directory" },
{ on = [ "h" ], exec = "leave", desc = "Go back to the parent directory" },
{ on = [ "l" ], exec = "enter", desc = "Enter the child directory" },

{ on = [ "H" ], exec = "back", desc = "Go back to the previous directory" },
{ on = [ "L" ], exec = "forward", desc = "Go forward to the next directory" },
Expand All @@ -58,29 +58,29 @@ keymap = [
{ on = [ "<C-r>" ], exec = "select_all --state=none", desc = "Inverse selection of all files" },

# Operation
{ on = [ "o" ], exec = [ "escape --visual", "open" ], desc = "Open the selected files" },
{ on = [ "O" ], exec = [ "escape --visual", "open --interactive" ], desc = "Open the selected files interactively" },
{ on = [ "<Enter>" ], exec = [ "escape --visual", "open" ], desc = "Open the selected files" },
{ on = [ "<C-Enter>" ], exec = [ "escape --visual", "open --interactive" ], desc = "Open the selected files interactively" },
{ on = [ "y" ], exec = [ "escape --visual", "yank" ], desc = "Copy the selected files" },
{ on = [ "Y" ], exec = "unyank", desc = "Cancel the yank status of files" },
{ on = [ "x" ], exec = [ "escape --visual", "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 = [ "-" ], exec = "link", desc = "Symlink the absolute path of files" },
{ on = [ "_" ], exec = "link --relative", desc = "Symlink the relative path of files" },
{ on = [ "d" ], exec = [ "escape --visual", "remove" ], desc = "Move the files to the trash" },
{ on = [ "D" ], exec = [ "escape --visual", "remove --permanently" ], desc = "Permanently delete the files" },
{ on = [ "a" ], exec = "create", desc = "Create a file or directory (ends with / for directories)" },
{ on = [ "r" ], exec = [ "escape --visual", "rename --cursor=before_ext" ], desc = "Rename a file or directory" },
{ on = [ ";" ], exec = [ "escape --visual", "shell" ], desc = "Run a shell command" },
{ on = [ ":" ], exec = [ "escape --visual", "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 name using fd" },
{ on = [ "S" ], exec = "search rg", desc = "Search files by content using ripgrep" },
{ on = [ "<C-s>" ], exec = "search none", desc = "Cancel the ongoing search" },
{ on = [ "z" ], exec = "jump zoxide", desc = "Jump to a directory using zoxide" },
{ on = [ "Z" ], exec = "jump fzf", desc = "Jump to a directory, or reveal a file using fzf" },
{ on = [ "o" ], exec = "open", desc = "Open the selected files" },
{ on = [ "O" ], exec = "open --interactive", desc = "Open the selected files interactively" },
{ on = [ "<Enter>" ], exec = "open", desc = "Open the selected files" },
{ on = [ "<C-Enter>" ], exec = "open --interactive", desc = "Open the selected files interactively" },
{ on = [ "y" ], exec = "yank", desc = "Copy the selected files" },
{ on = [ "Y" ], exec = "unyank", desc = "Cancel the yank status of 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 = [ "-" ], exec = "link", desc = "Symlink the absolute path of files" },
{ on = [ "_" ], exec = "link --relative", desc = "Symlink the relative path of files" },
{ 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 --cursor=before_ext", 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 name using fd" },
{ on = [ "S" ], exec = "search rg", desc = "Search files by content using ripgrep" },
{ on = [ "<C-s>" ], exec = "search none", desc = "Cancel the ongoing search" },
{ on = [ "z" ], exec = "jump zoxide", desc = "Jump to a directory using zoxide" },
{ on = [ "Z" ], exec = "jump fzf", desc = "Jump to a directory, or reveal a file using fzf" },

# Linemode
{ on = [ "m", "s" ], exec = "linemode size", desc = "Set linemode to size" },
Expand All @@ -89,10 +89,10 @@ keymap = [
{ on = [ "m", "n" ], exec = "linemode none", desc = "Set linemode to none" },

# Copy
{ on = [ "c", "c" ], exec = [ "escape --visual", "copy path" ], desc = "Copy the absolute path" },
{ on = [ "c", "d" ], exec = [ "escape --visual", "copy dirname" ], desc = "Copy the path of the parent directory" },
{ on = [ "c", "f" ], exec = [ "escape --visual", "copy filename" ], desc = "Copy the name of the file" },
{ on = [ "c", "n" ], exec = [ "escape --visual", "copy name_without_ext" ], desc = "Copy the name of the file without the extension" },
{ on = [ "c", "c" ], exec = "copy path", desc = "Copy the 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" },

# Filter
{ on = [ "f" ], exec = "filter --smart", desc = "Filter the files" },
Expand Down
4 changes: 2 additions & 2 deletions yazi-config/src/theme/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ pub struct Manager {
#[validate(range(min = 1, message = "Must be greater than 0"))]
tab_width: u8,

// Selected counter
count_selected: Style,
// Count
count_copied: Style,
count_cut: Style,
count_selected: Style,

// Border
pub border_symbol: String,
Expand Down
5 changes: 2 additions & 3 deletions yazi-core/src/help/help.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crossterm::event::KeyCode;
use unicode_width::UnicodeWidthStr;
use yazi_config::{keymap::{Control, Key}, KEYMAP};
use yazi_shared::{render, term::Term, Layer};
use yazi_shared::{render, render_and, term::Term, Layer};

use super::HELP_MARGIN;
use crate::input::Input;
Expand Down Expand Up @@ -49,8 +49,7 @@ impl Help {
}
Key { code: KeyCode::Enter, shift: false, ctrl: false, alt: false } => {
self.in_filter = None;
render!();
return true; // Don't do the `filter_apply` below, since we already have the filtered results.
return render_and!(true); // Don't do the `filter_apply` below, since we already have the filtered results.
}
Key { code: KeyCode::Backspace, shift: false, ctrl: false, alt: false } => {
input.backspace(false);
Expand Down
5 changes: 4 additions & 1 deletion yazi-core/src/manager/commands/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ impl From<Cmd> for Opt {

impl Manager {
pub fn open(&mut self, opt: impl Into<Opt>, tasks: &Tasks) {
let mut opt = opt.into() as Opt;
if !self.active_mut().try_escape_visual() {
return;
}

let mut opt = opt.into() as Opt;
let selected = if opt.hovered {
self.hovered().map(|h| vec![&h.url]).unwrap_or_default()
} else {
Expand Down
7 changes: 4 additions & 3 deletions yazi-core/src/manager/commands/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ impl Manager {
let opt = opt.into() as Opt;

let dest = self.cwd();
if self.yanked.cut {
tasks.file_cut(&self.yanked, dest, opt.force);
} else {
if !self.yanked.cut {
tasks.file_copy(&self.yanked, dest, opt.force, opt.follow);
}

tasks.file_cut(&self.yanked, dest, opt.force);
self.unyank(());
}
}
4 changes: 4 additions & 0 deletions yazi-core/src/manager/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl From<Cmd> for Opt {

impl Manager {
pub fn remove(&mut self, opt: impl Into<Opt>, tasks: &Tasks) {
if !self.active_mut().try_escape_visual() {
return;
}

let opt = opt.into() as Opt;
let targets = self.selected_or_hovered().into_iter().cloned().collect();
tasks.file_remove(targets, opt.force, opt.permanently);
Expand Down
6 changes: 4 additions & 2 deletions yazi-core/src/manager/commands/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ impl Manager {
Ok(Self::_hover(Some(new)))
}

pub fn rename(&self, opt: impl Into<Opt>) {
if !self.active().selected.is_empty() {
pub fn rename(&mut self, opt: impl Into<Opt>) {
if !self.active_mut().try_escape_visual() {
return;
} else if !self.active().selected.is_empty() {
return self.bulk_rename();
}

Expand Down
11 changes: 10 additions & 1 deletion yazi-core/src/manager/commands/unyank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ use yazi_shared::{event::Cmd, render};

use crate::manager::Manager;

pub struct Opt;

impl From<Cmd> for Opt {
fn from(_: Cmd) -> Self { Self }
}
impl From<()> for Opt {
fn from(_: ()) -> Self { Self }
}

impl Manager {
pub fn unyank(&mut self, _: Cmd) {
pub fn unyank(&mut self, _: impl Into<Opt>) {
render!(!self.yanked.is_empty());

self.yanked = Default::default();
Expand Down
4 changes: 4 additions & 0 deletions yazi-core/src/manager/commands/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ impl From<Cmd> for Opt {

impl Manager {
pub fn yank(&mut self, opt: impl Into<Opt>) {
if !self.active_mut().try_escape_visual() {
return;
}

let selected: HashSet<_> = self.selected_or_hovered().into_iter().cloned().collect();
if selected.is_empty() {
return;
Expand Down
7 changes: 7 additions & 0 deletions yazi-core/src/tab/commands/back.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use yazi_shared::event::Cmd;

use crate::tab::Tab;

impl Tab {
pub fn back(&mut self, _: Cmd) { self.backstack.shift_backward().cloned().map(|u| self.cd(u)); }
}
25 changes: 0 additions & 25 deletions yazi-core/src/tab/commands/backstack.rs

This file was deleted.

4 changes: 4 additions & 0 deletions yazi-core/src/tab/commands/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl Tab {
}

pub fn cd(&mut self, opt: impl Into<Opt>) {
if !self.try_escape_visual() {
return;
}

let opt = opt.into() as Opt;
if opt.interactive {
return self.cd_interactive();
Expand Down
5 changes: 4 additions & 1 deletion yazi-core/src/tab/commands/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ impl From<Cmd> for Opt {
}

impl Tab {
pub fn copy(&self, opt: impl Into<Opt>) {
pub fn copy(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if !self.try_escape_visual() {
return;
}

let mut s = OsString::new();
let mut it = self.selected_or_hovered().into_iter().peekable();
Expand Down
39 changes: 4 additions & 35 deletions yazi-core/src/tab/commands/enter.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
use std::mem;
use yazi_shared::event::Cmd;

use yazi_shared::{event::Cmd, render};

use crate::{manager::Manager, tab::Tab};

pub struct Opt;
impl From<()> for Opt {
fn from(_: ()) -> Self { Self }
}
impl From<Cmd> for Opt {
fn from(_: Cmd) -> Self { Self }
}
use crate::tab::Tab;

impl Tab {
pub fn enter(&mut self, _: impl Into<Opt>) {
let Some(hovered) = self.current.hovered().filter(|h| h.is_dir()).map(|h| h.url()) else {
return;
};

// Current
let rep = self.history_new(&hovered);
let rep = mem::replace(&mut self.current, rep);
if rep.cwd.is_regular() {
self.history.insert(rep.cwd.clone(), rep);
}

// Parent
if let Some(rep) = self.parent.take() {
self.history.insert(rep.cwd.clone(), rep);
}
self.parent = Some(self.history_new(&hovered.parent_url().unwrap()));

// Backstack
self.backstack.push(hovered);

Manager::_refresh();
render!();
pub fn enter(&mut self, _: Cmd) {
self.current.hovered().filter(|h| h.is_dir()).map(|h| h.url()).map(|u| self.cd(u));
}
}
Loading

0 comments on commit 1ec9943

Please sign in to comment.