This commit is contained in:
sxyazi 2023-11-09 11:01:20 +08:00
parent eac62492aa
commit f8b3ab0ce7
No known key found for this signature in database
6 changed files with 97 additions and 56 deletions

View file

@ -0,0 +1,48 @@
use yazi_config::keymap::Exec;
use crate::select::Select;
pub struct Opt {
step: isize,
}
impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self {
Self { step: e.args.first().and_then(|s| s.parse().ok()).unwrap_or(0) }
}
}
impl Select {
fn next(&mut self, step: usize) -> bool {
let len = self.items.len();
if len == 0 {
return false;
}
let old = self.cursor;
self.cursor = (self.cursor + step).min(len - 1);
let limit = self.limit();
if self.cursor >= len.min(self.offset + limit) {
self.offset = len.saturating_sub(limit).min(self.offset + self.cursor - old);
}
old != self.cursor
}
fn prev(&mut self, step: usize) -> bool {
let old = self.cursor;
self.cursor = self.cursor.saturating_sub(step);
if self.cursor < self.offset {
self.offset = self.offset.saturating_sub(old - self.cursor);
}
old != self.cursor
}
pub fn arrow(&mut self, opt: impl Into<Opt>) -> bool {
let opt = opt.into() as Opt;
if opt.step > 0 { self.next(opt.step as usize) } else { self.prev(opt.step.unsigned_abs()) }
}
}

View file

@ -0,0 +1,29 @@
use anyhow::anyhow;
use yazi_config::keymap::Exec;
use crate::select::Select;
pub struct Opt {
submit: bool,
}
impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { submit: e.named.contains_key("submit") } }
}
impl From<bool> for Opt {
fn from(submit: bool) -> Self { Self { submit } }
}
impl Select {
pub fn close(&mut self, opt: impl Into<Opt>) -> bool {
let opt = opt.into() as Opt;
if let Some(cb) = self.callback.take() {
_ = cb.send(if opt.submit { Ok(self.cursor) } else { Err(anyhow!("canceled")) });
}
self.cursor = 0;
self.offset = 0;
self.visible = false;
true
}
}

View file

@ -0,0 +1,2 @@
mod arrow;
mod close;

View file

@ -1,3 +1,4 @@
mod commands;
mod option; mod option;
mod select; mod select;

View file

@ -1,4 +1,4 @@
use anyhow::{anyhow, Result}; use anyhow::Result;
use tokio::sync::oneshot::Sender; use tokio::sync::oneshot::Sender;
use super::SelectOpt; use super::SelectOpt;
@ -6,13 +6,13 @@ use crate::Position;
#[derive(Default)] #[derive(Default)]
pub struct Select { pub struct Select {
title: String, title: String,
items: Vec<String>, pub(super) items: Vec<String>,
pub position: Position, pub position: Position,
offset: usize, pub(super) offset: usize,
cursor: usize, pub(super) cursor: usize,
callback: Option<Sender<Result<usize>>>, pub(super) callback: Option<Sender<Result<usize>>>,
pub visible: bool, pub visible: bool,
} }
@ -29,45 +29,6 @@ impl Select {
self.visible = true; self.visible = true;
} }
pub fn close(&mut self, submit: bool) -> bool {
if let Some(cb) = self.callback.take() {
_ = cb.send(if submit { Ok(self.cursor) } else { Err(anyhow!("canceled")) });
}
self.cursor = 0;
self.offset = 0;
self.visible = false;
true
}
pub fn next(&mut self, step: usize) -> bool {
let len = self.items.len();
if len == 0 {
return false;
}
let old = self.cursor;
self.cursor = (self.cursor + step).min(len - 1);
let limit = self.limit();
if self.cursor >= len.min(self.offset + limit) {
self.offset = len.saturating_sub(limit).min(self.offset + self.cursor - old);
}
old != self.cursor
}
pub fn prev(&mut self, step: usize) -> bool {
let old = self.cursor;
self.cursor = self.cursor.saturating_sub(step);
if self.cursor < self.offset {
self.offset = self.offset.saturating_sub(old - self.cursor);
}
old != self.cursor
}
#[inline] #[inline]
pub fn window(&self) -> &[String] { pub fn window(&self) -> &[String] {
let end = (self.offset + self.limit()).min(self.items.len()); let end = (self.offset + self.limit()).min(self.items.len());

View file

@ -166,18 +166,18 @@ impl<'a> Executor<'a> {
} }
fn select(&mut self, exec: &Exec) -> bool { fn select(&mut self, exec: &Exec) -> bool {
match exec.cmd.as_str() { macro_rules! on {
"close" => self.cx.select.close(exec.named.contains_key("submit")), ($name:ident) => {
if exec.cmd == stringify!($name) {
"arrow" => { return self.cx.select.$name(exec);
let step: isize = exec.args.first().and_then(|s| s.parse().ok()).unwrap_or(0);
if step > 0 {
self.cx.select.next(step as usize)
} else {
self.cx.select.prev(step.unsigned_abs())
} }
} };
}
on!(close);
on!(arrow);
match exec.cmd.as_str() {
"help" => self.cx.help.toggle(KeymapLayer::Select), "help" => self.cx.help.toggle(KeymapLayer::Select),
_ => false, _ => false,
} }