-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathinput.rs
More file actions
168 lines (137 loc) · 4.66 KB
/
Copy pathinput.rs
File metadata and controls
168 lines (137 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::{borrow::Cow, ops::Range};
use anyhow::Result;
use crossterm::cursor::SetCursorStyle;
use tokio::sync::mpsc;
use yazi_config::YAZI;
use yazi_macro::act;
use yazi_shared::{Ids, SStr};
use yazi_shim::path::CROSS_SEPARATOR;
use super::{InputSnap, InputSnaps, mode::InputMode, op::InputOp};
use crate::{CLIPBOARD, input::{InputEvent, InputOpt}};
#[derive(Default)]
pub struct Input {
pub id: SStr,
pub snaps: InputSnaps,
pub limit: usize,
pub obscure: bool,
pub realtime: bool,
pub completion: bool,
pub tx: Option<mpsc::UnboundedSender<InputEvent>>,
pub ticket: Ids,
}
impl Input {
pub fn new(opt: InputOpt) -> Result<Self> {
let limit = opt.cfg.position.offset.width.saturating_sub(YAZI.input.border()) as usize;
let mut input = Self {
id: opt.cfg.id,
snaps: InputSnaps::new(opt.cfg.value, opt.cfg.obscure, limit),
limit,
obscure: opt.cfg.obscure,
realtime: opt.cfg.realtime,
completion: opt.cfg.completion,
tx: Some(opt.tx),
..Default::default()
};
if let Some(cursor) = opt.cfg.cursor {
input.snap_mut().cursor = cursor;
act!(r#move, input)?;
}
Ok(input)
}
pub(super) fn handle_op(&mut self, cursor: usize, include: bool) -> bool {
let old = self.snap().clone();
let snap = self.snap_mut();
match snap.op {
InputOp::None | InputOp::Select(_) => {
snap.cursor = cursor;
}
InputOp::Delete(cut, insert, _) => {
let range = snap.op.range(cursor, include).unwrap();
let Range { start, end } = snap.idx(range.start)..snap.idx(range.end);
let drain = snap.value.drain(start.unwrap()..end.unwrap()).collect::<String>();
if cut {
futures::executor::block_on(CLIPBOARD.set(&drain));
}
snap.op = InputOp::None;
snap.mode = if insert { InputMode::Insert } else { InputMode::Normal };
snap.cursor = range.start;
}
InputOp::Yank(_) => {
let range = snap.op.range(cursor, include).unwrap();
let Range { start, end } = snap.idx(range.start)..snap.idx(range.end);
let yanked = &snap.value[start.unwrap()..end.unwrap()];
snap.op = InputOp::None;
futures::executor::block_on(CLIPBOARD.set(yanked));
}
};
snap.cursor = snap.count().saturating_sub(snap.mode.delta()).min(snap.cursor);
if snap == &old {
return false;
}
if !matches!(old.op, InputOp::None | InputOp::Select(_)) {
self.snaps.tag(self.limit).then(|| self.flush_type());
}
true
}
pub(super) fn flush_type(&mut self) {
self.ticket.next();
if let Some(tx) = self.tx.as_ref().filter(|_| self.realtime) {
tx.send(InputEvent::Type(self.value().to_owned())).ok();
}
self.flush_trigger(true);
}
pub(super) fn flush_trigger(&self, force: bool) {
if let Some(tx) = self.tx.as_ref().filter(|_| self.completion) {
tx.send(InputEvent::Trigger(
self.partition().0.to_owned(),
Some(self.ticket.current()).filter(|_| !force),
))
.ok();
}
}
}
impl Input {
pub fn value(&self) -> &str { &self.snap().value }
pub fn display(&self) -> Cow<'_, str> {
if self.obscure {
"•".repeat(self.snap().window(self.limit).len()).into()
} else {
self.snap().slice(self.snap().window(self.limit)).into()
}
}
pub fn mode(&self) -> InputMode { self.snap().mode }
pub fn cursor(&self) -> u16 { self.snap().width(self.snap().offset..self.snap().cursor) }
pub fn cursor_shape(&self) -> SetCursorStyle {
use InputMode as M;
match self.mode() {
M::Normal if YAZI.input.cursor_blink => SetCursorStyle::BlinkingBlock,
M::Normal if !YAZI.input.cursor_blink => SetCursorStyle::SteadyBlock,
M::Insert if YAZI.input.cursor_blink => SetCursorStyle::BlinkingBar,
M::Insert if !YAZI.input.cursor_blink => SetCursorStyle::SteadyBar,
M::Replace if YAZI.input.cursor_blink => SetCursorStyle::BlinkingUnderScore,
M::Replace if !YAZI.input.cursor_blink => SetCursorStyle::SteadyUnderScore,
M::Normal | M::Insert | M::Replace => unreachable!(),
}
}
pub fn selected(&self) -> Option<Range<u16>> {
let snap = self.snap();
let start = snap.op.start()?;
let (start, end) =
if start < snap.cursor { (start, snap.cursor) } else { (snap.cursor + 1, start + 1) };
let win = snap.window(self.limit);
let Range { start, end } = start.max(win.start)..end.min(win.end);
let s = snap.width(snap.offset..start);
Some(s..s + snap.width(start..end))
}
pub fn partition(&self) -> (&str, &str) {
let snap = self.snap();
let idx = snap.idx(snap.cursor).unwrap();
if let Some(sep) = snap.value[idx..].find(CROSS_SEPARATOR).map(|i| idx + i) {
(&snap.value[..sep], &snap.value[sep + 1..])
} else {
(&snap.value, "")
}
}
pub fn snap(&self) -> &InputSnap { self.snaps.current() }
pub fn snap_mut(&mut self) -> &mut InputSnap { self.snaps.current_mut() }
}