refactor(history): generalize up/down to offset

This commit is contained in:
OliverGuy 2026-05-07 00:46:38 +02:00
parent 311830b234
commit 21e5b77178
4 changed files with 33 additions and 34 deletions

View file

@ -305,10 +305,10 @@ keymap = [
{ on = "P", run = "paste --before", desc = "Paste copied characters before the cursor" }, { on = "P", run = "paste --before", desc = "Paste copied characters before the cursor" },
# History # History
{ on = "<Up>", run = "history up", desc = "Navigate to the previous history entry" }, { on = "<Up>", run = "history -1", desc = "Navigate to the previous history entry" },
{ on = "<Down>", run = "history down", desc = "Navigate to the next history entry" }, { on = "<Down>", run = "history +1", desc = "Navigate to the next history entry" },
{ on = "<C-p>", run = "history up", desc = "Navigate to the previous history entry" }, { on = "<C-p>", run = "history -1", desc = "Navigate to the previous history entry" },
{ on = "<C-n>", run = "history down", desc = "Navigate to the next history entry" }, { on = "<C-n>", run = "history +1", desc = "Navigate to the next history entry" },
# Undo/Redo/Casefy # Undo/Redo/Casefy
{ on = "u", run = [ "undo", "casefy lower" ], desc = "Undo, or lowercase if in visual mode" }, { on = "u", run = [ "undo", "casefy lower" ], desc = "Undo, or lowercase if in visual mode" },

View file

@ -2,7 +2,7 @@ use anyhow::Result;
use yazi_macro::{act, render, succ}; use yazi_macro::{act, render, succ};
use yazi_shared::data::Data; use yazi_shared::data::Data;
use crate::input::{Input, InputOp, INPUT_HISTORY, parser::HistoryOpt}; use crate::input::{INPUT_HISTORY, Input, InputOp, parser::HistoryOpt};
impl Input { impl Input {
pub fn history(&mut self, opt: HistoryOpt) -> Result<Data> { pub fn history(&mut self, opt: HistoryOpt) -> Result<Data> {
@ -10,7 +10,7 @@ impl Input {
succ!(); succ!();
} }
let new_value = INPUT_HISTORY.lock().unwrap().navigate(opt.up, &self.snap().value); let new_value = INPUT_HISTORY.lock().unwrap().navigate(opt.offset, &self.snap().value);
let Some(value) = new_value else { succ!() }; let Some(value) = new_value else { succ!() };
let snap = self.snap_mut(); let snap = self.snap_mut();

View file

@ -29,35 +29,28 @@ impl InputHistory {
self.draft.clear(); self.draft.clear();
} }
pub fn navigate(&mut self, up: bool, current: &str) -> Option<String> { pub fn navigate(&mut self, step: i64, current: &str) -> Option<String> {
if self.entries.is_empty() { if self.entries.is_empty() || step == 0 {
return None; return None;
} }
if up { let len = self.entries.len() as i64;
let new_idx = match self.idx { let pos = self.idx.map_or(len, |i| i as i64);
None => { let new_pos = (pos + step).clamp(0, len);
self.draft = current.to_owned();
self.entries.len() - 1 if new_pos == pos {
return None;
} }
Some(0) => return None,
Some(i) => i - 1, if new_pos == len {
};
self.idx = Some(new_idx);
Some(self.entries[new_idx].clone())
} else {
match self.idx {
None => None,
Some(i) if i + 1 >= self.entries.len() => {
self.idx = None; self.idx = None;
let draft = std::mem::take(&mut self.draft); Some(std::mem::take(&mut self.draft))
Some(draft) } else {
} if self.idx.is_none() {
Some(i) => { self.draft = current.to_owned();
self.idx = Some(i + 1);
Some(self.entries[i + 1].clone())
}
} }
self.idx = Some(new_pos as usize);
Some(self.entries[new_pos as usize].clone())
} }
} }
} }

View file

@ -3,17 +3,23 @@ use yazi_shared::event::ActionCow;
#[derive(Debug)] #[derive(Debug)]
pub struct HistoryOpt { pub struct HistoryOpt {
pub up: bool, pub offset: i64,
} }
impl From<ActionCow> for HistoryOpt { impl From<ActionCow> for HistoryOpt {
fn from(a: ActionCow) -> Self { Self { up: a.str(0) == "up" } } fn from(a: ActionCow) -> Self {
Self { offset: a.str(0).parse().unwrap_or(0) }
}
} }
impl FromLua for HistoryOpt { impl FromLua for HistoryOpt {
fn from_lua(_: Value, _: &Lua) -> mlua::Result<Self> { Err("unsupported".into_lua_err()) } fn from_lua(_: Value, _: &Lua) -> mlua::Result<Self> {
Err("unsupported".into_lua_err())
}
} }
impl IntoLua for HistoryOpt { impl IntoLua for HistoryOpt {
fn into_lua(self, _: &Lua) -> mlua::Result<Value> { Err("unsupported".into_lua_err()) } fn into_lua(self, _: &Lua) -> mlua::Result<Value> {
Err("unsupported".into_lua_err())
}
} }