yazi_core/input/commands/
replace.rs

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
use yazi_macro::render;
use yazi_shared::event::CmdCow;

use crate::input::{Input, InputMode, op::InputOp};

impl Input {
	#[yazi_codegen::command]
	pub fn replace(&mut self, _: CmdCow) {
		let snap = self.snap_mut();
		if snap.mode == InputMode::Normal {
			snap.op = InputOp::None;
			snap.mode = InputMode::Replace;
			render!();
		}
	}

	pub fn replace_str(&mut self, s: &str) {
		let snap = self.snap_mut();
		snap.mode = InputMode::Normal;

		let start = snap.idx(snap.cursor).unwrap();
		let mut it = snap.value[start..].char_indices();
		match (it.next(), it.next()) {
			(None, _) => {}
			(Some(_), None) => snap.value.replace_range(start..snap.len(), s),
			(Some(_), Some((len, _))) => snap.value.replace_range(start..start + len, s),
		}

		render!();
		self.snaps.tag(self.limit()).then(|| self.flush_value());
	}
}