From 2ee3be203d0074277b10b0bd8dcee311497be5df Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 27 Mar 2025 23:02:02 +0800 Subject: [PATCH] Simplify the code --- .github/workflows/check.yml | 2 +- Cargo.lock | 8 +-- Cargo.toml | 2 +- yazi-config/preset/keymap-default.toml | 76 ++++++++++++------------- yazi-core/src/cmp/commands/arrow.rs | 28 +++++---- yazi-core/src/confirm/commands/arrow.rs | 10 +--- yazi-core/src/help/commands/arrow.rs | 31 +++++----- yazi-core/src/pick/commands/arrow.rs | 30 +++++----- yazi-core/src/tab/folder.rs | 7 +-- yazi-core/src/tasks/commands/arrow.rs | 11 +--- yazi-fs/src/step.rs | 2 +- 11 files changed, 95 insertions(+), 112 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index d37f59fa..63f7015a 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -42,7 +42,7 @@ jobs: uses: mozilla-actions/sccache-action@v0.0.6 - name: Rustfmt - run: cargo +nightly fmt --all -- --check + run: rustfmt +nightly --check **/*.rs stylua: runs-on: ubuntu-22.04 diff --git a/Cargo.lock b/Cargo.lock index b6de71cd..2ec600e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -367,9 +367,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.32" +version = "4.5.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6088f3ae8c3608d19260cd7445411865a485688711b78b5be70d78cd96136f83" +checksum = "e958897981290da2a852763fe9cdb89cd36977a5d729023127095fa94d95e2ff" dependencies = [ "clap_builder", "clap_derive", @@ -377,9 +377,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.32" +version = "4.5.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a7ef7f676155edfb82daa97f99441f3ebf4a58d5e32f295a56259f1b6facc8" +checksum = "83b0f35019843db2160b5bb19ae09b4e6411ac33fc6a712003c33e03090e2489" dependencies = [ "anstream", "anstyle", diff --git a/Cargo.toml b/Cargo.toml index 80ebeda0..1a8e6d1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ ansi-to-tui = "7.0.0" anyhow = "1.0.97" base64 = "0.22.1" bitflags = "2.9.0" -clap = { version = "4.5.32", features = [ "derive" ] } +clap = { version = "4.5.34", features = [ "derive" ] } core-foundation-sys = "0.8.7" crossterm = { version = "0.28.1", features = [ "event-stream" ] } dirs = "6.0.0" diff --git a/yazi-config/preset/keymap-default.toml b/yazi-config/preset/keymap-default.toml index 2a71a7bf..e60a9266 100644 --- a/yazi-config/preset/keymap-default.toml +++ b/yazi-config/preset/keymap-default.toml @@ -13,11 +13,11 @@ keymap = [ { on = "", run = "suspend", desc = "Suspend the process" }, # Hopping - { on = "k", run = "arrow prev", desc = "Go to previous file" }, - { on = "j", run = "arrow next", desc = "Go to next file" }, + { on = "k", run = "arrow prev", desc = "Previous file" }, + { on = "j", run = "arrow next", desc = "Next file" }, - { on = "", run = "arrow prev", desc = "Go to previous file" }, - { on = "", run = "arrow next", desc = "Go to next file" }, + { on = "", run = "arrow prev", desc = "Previous file" }, + { on = "", run = "arrow next", desc = "Next file" }, { on = "", run = "arrow -50%", desc = "Move cursor up half page" }, { on = "", run = "arrow 50%", desc = "Move cursor down half page" }, @@ -29,8 +29,8 @@ keymap = [ { on = "", run = "arrow -100%", desc = "Move cursor up one page" }, { on = "", run = "arrow 100%", desc = "Move cursor down one page" }, - { on = [ "g", "g" ], run = "arrow top", desc = "Move cursor to the top" }, - { on = "G", run = "arrow bot", desc = "Move cursor to the bottom" }, + { on = [ "g", "g" ], run = "arrow top", desc = "Go to top" }, + { on = "G", run = "arrow bot", desc = "Go to bottom" }, # Navigation { on = "h", run = "leave", desc = "Back to the parent directory" }, @@ -105,8 +105,8 @@ keymap = [ # Find { on = "/", run = "find --smart", desc = "Find next file" }, { on = "?", run = "find --previous --smart", desc = "Find previous file" }, - { on = "n", run = "find_arrow", desc = "Go to next found" }, - { on = "N", run = "find_arrow --previous", desc = "Go to previous found" }, + { on = "n", run = "find_arrow", desc = "Next found" }, + { on = "N", run = "find_arrow --previous", desc = "Previous found" }, # Sorting { on = [ ",", "m" ], run = [ "sort mtime --reverse=no", "linemode mtime" ], desc = "Sort by modified time" }, @@ -164,11 +164,11 @@ keymap = [ { on = "", run = "close", desc = "Close task manager" }, { on = "w", run = "close", desc = "Close task manager" }, - { on = "k", run = "arrow -1", desc = "Move cursor up" }, - { on = "j", run = "arrow 1", desc = "Move cursor down" }, + { on = "k", run = "arrow prev", desc = "Previous task" }, + { on = "j", run = "arrow next", desc = "Next task" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous task" }, + { on = "", run = "arrow next", desc = "Next task" }, { on = "", run = "inspect", desc = "Inspect the task" }, { on = "x", run = "cancel", desc = "Cancel the task" }, @@ -186,15 +186,15 @@ keymap = [ { on = "", run = "close", desc = "Close the spot" }, { on = "", run = "close", desc = "Close the spot" }, - { on = "k", run = "arrow -1", desc = "Move cursor up" }, - { on = "j", run = "arrow 1", desc = "Move cursor down" }, - { on = "h", run = "swipe -1", desc = "Swipe to previous file" }, - { on = "l", run = "swipe 1", desc = "Swipe to next file" }, + { on = "k", run = "arrow prev", desc = "Previous line" }, + { on = "j", run = "arrow next", desc = "Next line" }, + { on = "h", run = "swipe prev", desc = "Swipe to previous file" }, + { on = "l", run = "swipe next", desc = "Swipe to next file" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, - { on = "", run = "swipe -1", desc = "Swipe to next file" }, - { on = "", run = "swipe 1", desc = "Swipe to previous file" }, + { on = "", run = "arrow prev", desc = "Previous line" }, + { on = "", run = "arrow next", desc = "Next line" }, + { on = "", run = "swipe prev", desc = "Swipe to previous file" }, + { on = "", run = "swipe next", desc = "Swipe to next file" }, # Copy { on = [ "c", "c" ], run = "copy cell", desc = "Copy selected cell" }, @@ -212,11 +212,11 @@ keymap = [ { on = "", run = "close", desc = "Cancel pick" }, { on = "", run = "close --submit", desc = "Submit the pick" }, - { on = "k", run = "arrow -1", desc = "Move cursor up" }, - { on = "j", run = "arrow 1", desc = "Move cursor down" }, + { on = "k", run = "arrow prev", desc = "Previous option" }, + { on = "j", run = "arrow next", desc = "Next option" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous option" }, + { on = "", run = "arrow next", desc = "Next option" }, # Help { on = "~", run = "help", desc = "Open help" }, @@ -314,11 +314,11 @@ keymap = [ { on = "n", run = "close", desc = "Cancel the confirm" }, { on = "y", run = "close --submit", desc = "Submit the confirm" }, - { on = "k", run = "arrow -1", desc = "Move cursor up" }, - { on = "j", run = "arrow 1", desc = "Move cursor down" }, + { on = "k", run = "arrow prev", desc = "Previous line" }, + { on = "j", run = "arrow next", desc = "Next line" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous line" }, + { on = "", run = "arrow next", desc = "Next line" }, # Help { on = "~", run = "help", desc = "Open help" }, @@ -332,14 +332,14 @@ keymap = [ { on = "", run = "close --submit", desc = "Submit the completion" }, { on = "", run = [ "close --submit", "input:close --submit" ], desc = "Complete and submit the input" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous item" }, + { on = "", run = "arrow next", desc = "Next item" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous item" }, + { on = "", run = "arrow next", desc = "Next item" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous item" }, + { on = "", run = "arrow next", desc = "Next item" }, # Help { on = "~", run = "help", desc = "Open help" }, @@ -354,11 +354,11 @@ keymap = [ { on = "", run = "close", desc = "Hide the help" }, # Navigation - { on = "k", run = "arrow -1", desc = "Move cursor up" }, - { on = "j", run = "arrow 1", desc = "Move cursor down" }, + { on = "k", run = "arrow prev", desc = "Previous line" }, + { on = "j", run = "arrow next", desc = "Next line" }, - { on = "", run = "arrow -1", desc = "Move cursor up" }, - { on = "", run = "arrow 1", desc = "Move cursor down" }, + { on = "", run = "arrow prev", desc = "Previous line" }, + { on = "", run = "arrow next", desc = "Next line" }, # Filtering { on = "f", run = "filter", desc = "Apply a filter for the help items" }, diff --git a/yazi-core/src/cmp/commands/arrow.rs b/yazi-core/src/cmp/commands/arrow.rs index e203ce01..bb1fb46a 100644 --- a/yazi-core/src/cmp/commands/arrow.rs +++ b/yazi-core/src/cmp/commands/arrow.rs @@ -17,11 +17,6 @@ impl From for Opt { impl Cmp { #[yazi_codegen::command] pub fn arrow(&mut self, opt: Opt) { - let len = self.cands.len(); - if len == 0 { - return; - } - let new = opt.step.add(self.cursor, self.cands.len(), self.limit()); if new > self.cursor { self.next(new); @@ -31,25 +26,28 @@ impl Cmp { } fn next(&mut self, new: usize) { - let len = self.cands.len(); let old = self.cursor; - self.cursor = new.min(len - 1); + self.cursor = new; - let limit = self.limit(); - if self.cursor >= len.min(self.offset + limit) { - self.offset = len.saturating_sub(limit).min(self.offset + self.cursor - old); - } + let (len, limit) = (self.cands.len(), self.limit()); + self.offset = if self.cursor < len.min(self.offset + limit) { + self.offset.min(len.saturating_sub(1)) + } else { + len.saturating_sub(limit).min(self.offset + self.cursor - old) + }; render!(old != self.cursor); } fn prev(&mut self, new: usize) { let old = self.cursor; - self.cursor = new.min(self.cands.len().saturating_sub(1)); + self.cursor = new; - if self.cursor < self.offset { - self.offset = self.offset.saturating_sub(old - self.cursor); - } + self.offset = if self.cursor < self.offset { + self.offset.saturating_sub(old - self.cursor) + } else { + self.offset.min(self.cands.len().saturating_sub(1)) + }; render!(old != self.cursor); } diff --git a/yazi-core/src/confirm/commands/arrow.rs b/yazi-core/src/confirm/commands/arrow.rs index b2c29730..103aa4ed 100644 --- a/yazi-core/src/confirm/commands/arrow.rs +++ b/yazi-core/src/confirm/commands/arrow.rs @@ -17,15 +17,11 @@ impl From for Opt { impl Confirm { #[yazi_codegen::command] pub fn arrow(&mut self, opt: Opt, mgr: &Mgr) { - let width = mgr.area(self.position).width; - let height = self.list.line_count(width); - if height == 0 { - return; - } + let area = mgr.area(self.position); + let len = self.list.line_count(area.width); let old = self.offset; - let new = opt.step.add(self.offset, height, height); - self.offset = new.min(height - 1); + self.offset = opt.step.add(self.offset, len, area.height as _); render!(old != self.offset); } diff --git a/yazi-core/src/help/commands/arrow.rs b/yazi-core/src/help/commands/arrow.rs index 04b389bc..3fc71b8a 100644 --- a/yazi-core/src/help/commands/arrow.rs +++ b/yazi-core/src/help/commands/arrow.rs @@ -28,30 +28,29 @@ impl Help { } } - fn next(&mut self, step: usize) { - let len = self.bindings.len(); - if len == 0 { - return; - } - + fn next(&mut self, new: usize) { let old = self.cursor; - self.cursor = (self.cursor + step).min(len - 1); + self.cursor = new; - 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); - } + let (len, limit) = (self.bindings.len(), Self::limit()); + self.offset = if self.cursor < (self.offset + limit).min(len).saturating_sub(5) { + self.offset.min(len.saturating_sub(1)) + } else { + len.saturating_sub(limit).min(self.offset + self.cursor - old) + }; render!(old != self.cursor); } - fn prev(&mut self, step: usize) { + fn prev(&mut self, new: usize) { let old = self.cursor; - self.cursor = self.cursor.saturating_sub(step); + self.cursor = new; - if self.cursor < self.offset + 5 { - self.offset = self.offset.saturating_sub(old - self.cursor); - } + self.offset = if self.cursor < self.offset + 5 { + self.offset.saturating_sub(old - self.cursor) + } else { + self.offset.min(self.bindings.len().saturating_sub(1)) + }; render!(old != self.cursor); } diff --git a/yazi-core/src/pick/commands/arrow.rs b/yazi-core/src/pick/commands/arrow.rs index 423ea00e..7491ba57 100644 --- a/yazi-core/src/pick/commands/arrow.rs +++ b/yazi-core/src/pick/commands/arrow.rs @@ -17,12 +17,7 @@ impl From for Opt { impl Pick { #[yazi_codegen::command] pub fn arrow(&mut self, opt: Opt) { - let len = self.items.len(); - if len == 0 { - return; - } - - let new = opt.step.add(self.cursor, len, self.limit()); + let new = opt.step.add(self.cursor, self.items.len(), self.limit()); if new > self.cursor { self.next(new); } else { @@ -31,25 +26,28 @@ impl Pick { } fn next(&mut self, new: usize) { - let len = self.items.len(); let old = self.cursor; - self.cursor = new.min(len - 1); + self.cursor = new; - let limit = self.limit(); - if self.cursor >= len.min(self.offset + limit) { - self.offset = len.saturating_sub(limit).min(self.offset + self.cursor - old); - } + let (len, limit) = (self.items.len(), self.limit()); + self.offset = if self.cursor < len.min(self.offset + limit) { + self.offset.min(len.saturating_sub(1)) + } else { + len.saturating_sub(limit).min(self.offset + self.cursor - old) + }; render!(old != self.cursor); } fn prev(&mut self, new: usize) { let old = self.cursor; - self.cursor = new.min(self.items.len().saturating_sub(1)); + self.cursor = new; - if self.cursor < self.offset { - self.offset = self.offset.saturating_sub(old - self.cursor); - } + self.offset = if self.cursor < self.offset { + self.offset.saturating_sub(old - self.cursor) + } else { + self.offset.min(self.items.len().saturating_sub(1)) + }; render!(old != self.cursor); } diff --git a/yazi-core/src/tab/folder.rs b/yazi-core/src/tab/folder.rs index 4d0845e6..12d2de04 100644 --- a/yazi-core/src/tab/folder.rs +++ b/yazi-core/src/tab/folder.rs @@ -142,7 +142,7 @@ impl Folder { let limit = LAYOUT.get().limit(); let scrolloff = (limit / 2).min(YAZI.mgr.scrolloff as usize); - self.cursor = new.min(len.saturating_sub(1)); + self.cursor = new; self.offset = if self.cursor < (self.offset + limit).min(len).saturating_sub(scrolloff) { self.offset.min(len.saturating_sub(1)) } else { @@ -154,16 +154,15 @@ impl Folder { fn prev(&mut self, new: usize) -> bool { let old = (self.cursor, self.offset); - let max = self.files.len().saturating_sub(1); let limit = LAYOUT.get().limit(); let scrolloff = (limit / 2).min(YAZI.mgr.scrolloff as usize); - self.cursor = new.min(max); + self.cursor = new; self.offset = if self.cursor < self.offset + scrolloff { self.offset.saturating_sub(old.0 - self.cursor) } else { - self.offset.min(max) + self.offset.min(self.files.len().saturating_sub(1)) }; old != (self.cursor, self.offset) diff --git a/yazi-core/src/tasks/commands/arrow.rs b/yazi-core/src/tasks/commands/arrow.rs index 95bc18cb..40bd5569 100644 --- a/yazi-core/src/tasks/commands/arrow.rs +++ b/yazi-core/src/tasks/commands/arrow.rs @@ -15,22 +15,15 @@ impl From for Opt { } impl From for Opt { - fn from(step: isize) -> Self { Self { step: step.into() } } + fn from(n: isize) -> Self { Self { step: n.into() } } } impl Tasks { #[yazi_codegen::command] pub fn arrow(&mut self, opt: Opt) { - let max = Self::limit().min(self.summaries.len()); let old = self.cursor; - let new = opt.step.add(self.cursor, max, max); - if new > old { - self.cursor += 1; - } else { - self.cursor = self.cursor.saturating_sub(1); - } + self.cursor = opt.step.add(self.cursor, self.summaries.len(), Self::limit()); - self.cursor = self.cursor.min(max.saturating_sub(1)); render!(self.cursor != old); } } diff --git a/yazi-fs/src/step.rs b/yazi-fs/src/step.rs index 976ffcc4..916060b4 100644 --- a/yazi-fs/src/step.rs +++ b/yazi-fs/src/step.rs @@ -68,7 +68,7 @@ impl Step { } else if matches!(self, Self::Prev | Self::Next) { fixed.saturating_add_unsigned(pos).rem_euclid(len as _) as _ } else if fixed > 0 { - pos + fixed as usize + pos.saturating_add_signed(fixed).min(len - 1) } else { pos.saturating_sub(fixed.unsigned_abs()) }