mirror of
https://github.com/sxyazi/yazi.git
synced 2024-10-05 14:47:19 +03:00
Merge 1182ce69ec
into 11547eefe0
This commit is contained in:
commit
2ce718311f
@ -39,8 +39,11 @@ impl Input {
|
||||
));
|
||||
|
||||
let (limit, snap) = (self.limit(), self.snap_mut());
|
||||
if snap.offset > snap.cursor {
|
||||
snap.offset = snap.cursor;
|
||||
let offset = InputSnap::find_window_backward(&snap.value, snap.cursor, limit / 2)
|
||||
.start
|
||||
.min(InputSnap::find_window_backward(&snap.value, snap.value.chars().count(), limit).start);
|
||||
if snap.offset != offset {
|
||||
snap.offset = offset;
|
||||
} else if snap.value.is_empty() {
|
||||
snap.offset = 0;
|
||||
} else {
|
||||
|
@ -87,4 +87,25 @@ impl InputSnap {
|
||||
}
|
||||
*v.first().unwrap()..v.last().unwrap() + 1
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn find_window_backward(s: &str, offset: usize, limit: usize) -> Range<usize> {
|
||||
let mut width = 0;
|
||||
let len = s.chars().count();
|
||||
let v: Vec<_> = s
|
||||
.chars()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.skip(len.saturating_sub(offset + 1))
|
||||
.map_while(|(i, c)| {
|
||||
width += c.width().unwrap_or(0);
|
||||
if width < limit { Some(len.saturating_sub(i + 1)) } else { None }
|
||||
})
|
||||
.collect();
|
||||
|
||||
if v.is_empty() {
|
||||
return 0..0;
|
||||
}
|
||||
*v.last().unwrap()..v.first().unwrap() + 1
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user