mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
..
This commit is contained in:
parent
5c527cd5b7
commit
a6fb8ee64b
4 changed files with 46 additions and 28 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{num::ParseIntError, str::FromStr};
|
use std::{num::ParseIntError, str::FromStr};
|
||||||
|
|
||||||
use unicode_width::UnicodeWidthStr;
|
|
||||||
use yazi_macro::render;
|
use yazi_macro::render;
|
||||||
use yazi_shared::event::{CmdCow, Data};
|
use yazi_shared::event::{CmdCow, Data};
|
||||||
|
|
||||||
|
|
@ -40,10 +39,10 @@ impl Input {
|
||||||
snap.offset = 0;
|
snap.offset = 0;
|
||||||
} else {
|
} else {
|
||||||
let delta = snap.mode.delta();
|
let delta = snap.mode.delta();
|
||||||
let s = snap.slice(snap.offset..snap.cursor + delta);
|
let range = snap.offset..snap.cursor + delta;
|
||||||
if s.width() >= limit {
|
if snap.width(range.clone()) >= limit as u16 {
|
||||||
let range = InputSnap::find_window(s.chars().rev(), 0, limit);
|
let it = snap.slice(range).chars().rev().map(|c| if snap.obscure { '•' } else { c });
|
||||||
snap.offset = snap.cursor - range.end.saturating_sub(delta);
|
snap.offset = snap.cursor - InputSnap::find_window(it, 0, limit).end.saturating_sub(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use std::ops::Range;
|
use std::{borrow::Cow, ops::Range};
|
||||||
|
|
||||||
use crossterm::cursor::SetCursorStyle;
|
use crossterm::cursor::SetCursorStyle;
|
||||||
use unicode_width::UnicodeWidthStr;
|
|
||||||
use yazi_config::YAZI;
|
use yazi_config::YAZI;
|
||||||
use yazi_plugin::CLIPBOARD;
|
use yazi_plugin::CLIPBOARD;
|
||||||
|
|
||||||
|
|
@ -12,14 +11,14 @@ pub type InputCallback = Box<dyn Fn(&str, &str)>;
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Input {
|
pub struct Input {
|
||||||
pub snaps: InputSnaps,
|
pub snaps: InputSnaps,
|
||||||
pub limit: usize,
|
pub limit: usize, // FIXME
|
||||||
pub obscure: bool,
|
pub obscure: bool,
|
||||||
pub callback: Option<InputCallback>,
|
pub callback: Option<InputCallback>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Input {
|
impl Input {
|
||||||
pub fn new(value: String, limit: usize, obscure: bool, callback: InputCallback) -> Self {
|
pub fn new(value: String, limit: usize, obscure: bool, callback: InputCallback) -> Self {
|
||||||
Self { snaps: InputSnaps::new(value, limit), limit, obscure, callback: Some(callback) }
|
Self { snaps: InputSnaps::new(value, obscure, limit), limit, obscure, callback: Some(callback) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn handle_op(&mut self, cursor: usize, include: bool) -> bool {
|
pub(super) fn handle_op(&mut self, cursor: usize, include: bool) -> bool {
|
||||||
|
|
@ -78,9 +77,9 @@ impl Input {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn display(&self) -> Cow<str> {
|
pub fn display(&self) -> Cow<str> {
|
||||||
if self.obscure {
|
if self.obscure {
|
||||||
"•".repeat(self.window(self.limit).len()).into()
|
"•".repeat(self.snap().window(self.limit).len()).into()
|
||||||
} else {
|
} else {
|
||||||
self.snap().slice(self.window(self.limit)).into()
|
self.snap().slice(self.snap().window(self.limit)).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,10 +87,7 @@ impl Input {
|
||||||
pub fn mode(&self) -> InputMode { self.snap().mode }
|
pub fn mode(&self) -> InputMode { self.snap().mode }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cursor(&self) -> u16 {
|
pub fn cursor(&self) -> u16 { self.snap().width(self.snap().offset..self.snap().cursor) }
|
||||||
let snap = self.snap();
|
|
||||||
snap.slice(snap.offset..snap.cursor).width() as u16
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cursor_shape(&self) -> SetCursorStyle {
|
pub fn cursor_shape(&self) -> SetCursorStyle {
|
||||||
use InputMode as M;
|
use InputMode as M;
|
||||||
|
|
@ -117,8 +113,8 @@ impl Input {
|
||||||
let win = snap.window(self.limit);
|
let win = snap.window(self.limit);
|
||||||
let Range { start, end } = start.max(win.start)..end.min(win.end);
|
let Range { start, end } = start.max(win.start)..end.min(win.end);
|
||||||
|
|
||||||
let s = snap.slice(snap.offset..start).width() as u16;
|
let s = snap.width(snap.offset..start);
|
||||||
Some(s..s + snap.slice(start..end).width() as u16)
|
Some(s..s + snap.width(start..end))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
use unicode_width::UnicodeWidthChar;
|
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
|
||||||
|
|
||||||
use super::{InputMode, InputOp};
|
use super::{InputMode, InputOp};
|
||||||
|
|
||||||
|
|
@ -10,20 +10,24 @@ pub struct InputSnap {
|
||||||
|
|
||||||
pub op: InputOp,
|
pub op: InputOp,
|
||||||
|
|
||||||
pub mode: InputMode,
|
pub mode: InputMode,
|
||||||
|
pub obscure: bool,
|
||||||
|
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
pub cursor: usize,
|
pub cursor: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputSnap {
|
impl InputSnap {
|
||||||
pub(super) fn new(value: String, limit: usize) -> Self {
|
pub(super) fn new(value: String, obscure: bool, limit: usize) -> Self {
|
||||||
let mut snap = Self {
|
let mut snap = Self {
|
||||||
value,
|
value,
|
||||||
|
|
||||||
op: Default::default(),
|
op: Default::default(),
|
||||||
|
|
||||||
mode: Default::default(),
|
mode: Default::default(),
|
||||||
offset: usize::MAX,
|
obscure,
|
||||||
|
|
||||||
|
offset: 0,
|
||||||
cursor: usize::MAX,
|
cursor: usize::MAX,
|
||||||
};
|
};
|
||||||
snap.resize(limit);
|
snap.resize(limit);
|
||||||
|
|
@ -32,9 +36,19 @@ impl InputSnap {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn resize(&mut self, limit: usize) {
|
pub(super) fn resize(&mut self, limit: usize) {
|
||||||
let range = Self::find_window(self.value.chars().rev(), 0, limit);
|
let count = self.count();
|
||||||
|
let limit = if self.obscure {
|
||||||
|
count.min(limit)
|
||||||
|
} else {
|
||||||
|
Self::find_window(self.value.chars().rev(), 0, limit).end
|
||||||
|
};
|
||||||
|
|
||||||
self.cursor = self.cursor.min(self.count().saturating_sub(self.mode.delta()));
|
self.cursor = self.cursor.min(self.count().saturating_sub(self.mode.delta()));
|
||||||
self.offset = self.offset.min(self.cursor.saturating_sub(range.end));
|
self.offset = if self.cursor < (self.offset + limit).min(count) {
|
||||||
|
count.saturating_sub(limit).min(self.offset)
|
||||||
|
} else {
|
||||||
|
count.saturating_sub(limit).min(self.cursor.saturating_sub(limit) + 1)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,9 +75,18 @@ impl InputSnap {
|
||||||
&self.value[s.unwrap()..e.unwrap()]
|
&self.value[s.unwrap()..e.unwrap()]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(super) fn width(&self, range: Range<usize>) -> u16 {
|
||||||
|
if self.obscure { range.len() as u16 } else { self.slice(range).width() as u16 }
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn window(&self, limit: usize) -> Range<usize> {
|
pub(super) fn window(&self, limit: usize) -> Range<usize> {
|
||||||
Self::find_window(self.value.chars(), self.offset, limit)
|
Self::find_window(
|
||||||
|
self.value.chars().map(|c| if self.obscure { '•' } else { c }),
|
||||||
|
self.offset,
|
||||||
|
limit,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn find_window<T>(it: T, offset: usize, limit: usize) -> Range<usize>
|
pub(super) fn find_window<T>(it: T, offset: usize, limit: usize) -> Range<usize>
|
||||||
|
|
|
||||||
|
|
@ -13,15 +13,15 @@ impl Default for InputSnaps {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
idx: 0,
|
idx: 0,
|
||||||
versions: vec![InputSnap::new(String::new(), 0)],
|
versions: vec![InputSnap::new(String::new(), false, 0)],
|
||||||
current: InputSnap::new(String::new(), 0),
|
current: InputSnap::new(String::new(), false, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputSnaps {
|
impl InputSnaps {
|
||||||
pub fn new(value: String, limit: usize) -> Self {
|
pub fn new(value: String, obscure: bool, limit: usize) -> Self {
|
||||||
let current = InputSnap::new(value, limit);
|
let current = InputSnap::new(value, obscure, limit);
|
||||||
Self { idx: 0, versions: vec![current.clone()], current }
|
Self { idx: 0, versions: vec![current.clone()], current }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue