Simplify the code

This commit is contained in:
sxyazi 2025-03-27 23:02:02 +08:00
parent 13fde686ed
commit 2ee3be203d
No known key found for this signature in database
11 changed files with 95 additions and 112 deletions

View file

@ -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

8
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -13,11 +13,11 @@ keymap = [
{ on = "<C-z>", 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 = "<Up>", run = "arrow prev", desc = "Go to previous file" },
{ on = "<Down>", run = "arrow next", desc = "Go to next file" },
{ on = "<Up>", run = "arrow prev", desc = "Previous file" },
{ on = "<Down>", run = "arrow next", desc = "Next file" },
{ on = "<C-u>", run = "arrow -50%", desc = "Move cursor up half page" },
{ on = "<C-d>", run = "arrow 50%", desc = "Move cursor down half page" },
@ -29,8 +29,8 @@ keymap = [
{ on = "<PageUp>", run = "arrow -100%", desc = "Move cursor up one page" },
{ on = "<PageDown>", 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 = "<C-c>", 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 = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<Up>", run = "arrow prev", desc = "Previous task" },
{ on = "<Down>", run = "arrow next", desc = "Next task" },
{ on = "<Enter>", run = "inspect", desc = "Inspect the task" },
{ on = "x", run = "cancel", desc = "Cancel the task" },
@ -186,15 +186,15 @@ keymap = [
{ on = "<C-c>", run = "close", desc = "Close the spot" },
{ on = "<Tab>", 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 = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<Left>", run = "swipe -1", desc = "Swipe to next file" },
{ on = "<Right>", run = "swipe 1", desc = "Swipe to previous file" },
{ on = "<Up>", run = "arrow prev", desc = "Previous line" },
{ on = "<Down>", run = "arrow next", desc = "Next line" },
{ on = "<Left>", run = "swipe prev", desc = "Swipe to previous file" },
{ on = "<Right>", 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 = "<C-c>", run = "close", desc = "Cancel pick" },
{ on = "<Enter>", 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 = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<Up>", run = "arrow prev", desc = "Previous option" },
{ on = "<Down>", 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 = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<Up>", run = "arrow prev", desc = "Previous line" },
{ on = "<Down>", run = "arrow next", desc = "Next line" },
# Help
{ on = "~", run = "help", desc = "Open help" },
@ -332,14 +332,14 @@ keymap = [
{ on = "<Tab>", run = "close --submit", desc = "Submit the completion" },
{ on = "<Enter>", run = [ "close --submit", "input:close --submit" ], desc = "Complete and submit the input" },
{ on = "<A-k>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<A-j>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<A-k>", run = "arrow prev", desc = "Previous item" },
{ on = "<A-j>", run = "arrow next", desc = "Next item" },
{ on = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<Up>", run = "arrow prev", desc = "Previous item" },
{ on = "<Down>", run = "arrow next", desc = "Next item" },
{ on = "<C-p>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<C-n>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<C-p>", run = "arrow prev", desc = "Previous item" },
{ on = "<C-n>", run = "arrow next", desc = "Next item" },
# Help
{ on = "~", run = "help", desc = "Open help" },
@ -354,11 +354,11 @@ keymap = [
{ on = "<C-c>", 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 = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = "<Up>", run = "arrow prev", desc = "Previous line" },
{ on = "<Down>", run = "arrow next", desc = "Next line" },
# Filtering
{ on = "f", run = "filter", desc = "Apply a filter for the help items" },

View file

@ -17,11 +17,6 @@ impl From<CmdCow> 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);
}

View file

@ -17,15 +17,11 @@ impl From<CmdCow> 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);
}

View file

@ -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);
}

View file

@ -17,12 +17,7 @@ impl From<CmdCow> 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);
}

View file

@ -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)

View file

@ -15,22 +15,15 @@ impl From<CmdCow> for Opt {
}
impl From<isize> 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);
}
}

View file

@ -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())
}