yazi_core/confirm/commands/
arrow.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
33
34
35
36
37
38
39
40
41
42
use yazi_macro::render;
use yazi_shared::event::{CmdCow, Data};

use crate::{confirm::Confirm, manager::Manager};

struct Opt {
	step: isize,
}

impl From<CmdCow> for Opt {
	fn from(c: CmdCow) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
}

impl Confirm {
	#[yazi_codegen::command]
	pub fn arrow(&mut self, opt: Opt, manager: &Manager) {
		if opt.step > 0 {
			self.next(opt.step as usize, manager.area(self.position).width)
		} else {
			self.prev(opt.step.unsigned_abs())
		}
	}

	fn next(&mut self, step: usize, width: u16) {
		let height = self.list.line_count(width);
		if height == 0 {
			return;
		}

		let old = self.offset;
		self.offset = (self.offset + step).min(height - 1);

		render!(old != self.offset);
	}

	fn prev(&mut self, step: usize) {
		let old = self.offset;
		self.offset -= step.min(self.offset);

		render!(old != self.offset);
	}
}