mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
refactor(history): generalize up/down to offset
This commit is contained in:
parent
311830b234
commit
21e5b77178
4 changed files with 33 additions and 34 deletions
|
|
@ -305,10 +305,10 @@ keymap = [
|
|||
{ on = "P", run = "paste --before", desc = "Paste copied characters before the cursor" },
|
||||
|
||||
# History
|
||||
{ on = "<Up>", run = "history up", desc = "Navigate to the previous history entry" },
|
||||
{ on = "<Down>", run = "history down", desc = "Navigate to the next history entry" },
|
||||
{ on = "<C-p>", run = "history up", desc = "Navigate to the previous history entry" },
|
||||
{ on = "<C-n>", run = "history down", desc = "Navigate to the next history entry" },
|
||||
{ on = "<Up>", run = "history -1", desc = "Navigate to the previous history entry" },
|
||||
{ on = "<Down>", run = "history +1", desc = "Navigate to the next history entry" },
|
||||
{ on = "<C-p>", run = "history -1", desc = "Navigate to the previous history entry" },
|
||||
{ on = "<C-n>", run = "history +1", desc = "Navigate to the next history entry" },
|
||||
|
||||
# Undo/Redo/Casefy
|
||||
{ on = "u", run = [ "undo", "casefy lower" ], desc = "Undo, or lowercase if in visual mode" },
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use anyhow::Result;
|
|||
use yazi_macro::{act, render, succ};
|
||||
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 {
|
||||
pub fn history(&mut self, opt: HistoryOpt) -> Result<Data> {
|
||||
|
|
@ -10,7 +10,7 @@ impl Input {
|
|||
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 snap = self.snap_mut();
|
||||
|
|
|
|||
|
|
@ -29,35 +29,28 @@ impl InputHistory {
|
|||
self.draft.clear();
|
||||
}
|
||||
|
||||
pub fn navigate(&mut self, up: bool, current: &str) -> Option<String> {
|
||||
if self.entries.is_empty() {
|
||||
pub fn navigate(&mut self, step: i64, current: &str) -> Option<String> {
|
||||
if self.entries.is_empty() || step == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if up {
|
||||
let new_idx = match self.idx {
|
||||
None => {
|
||||
self.draft = current.to_owned();
|
||||
self.entries.len() - 1
|
||||
let len = self.entries.len() as i64;
|
||||
let pos = self.idx.map_or(len, |i| i as i64);
|
||||
let new_pos = (pos + step).clamp(0, len);
|
||||
|
||||
if new_pos == pos {
|
||||
return None;
|
||||
}
|
||||
Some(0) => return None,
|
||||
Some(i) => i - 1,
|
||||
};
|
||||
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() => {
|
||||
|
||||
if new_pos == len {
|
||||
self.idx = None;
|
||||
let draft = std::mem::take(&mut self.draft);
|
||||
Some(draft)
|
||||
}
|
||||
Some(i) => {
|
||||
self.idx = Some(i + 1);
|
||||
Some(self.entries[i + 1].clone())
|
||||
}
|
||||
Some(std::mem::take(&mut self.draft))
|
||||
} else {
|
||||
if self.idx.is_none() {
|
||||
self.draft = current.to_owned();
|
||||
}
|
||||
self.idx = Some(new_pos as usize);
|
||||
Some(self.entries[new_pos as usize].clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,17 +3,23 @@ use yazi_shared::event::ActionCow;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct HistoryOpt {
|
||||
pub up: bool,
|
||||
pub offset: i64,
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue