yazi_core/tab/
mode.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
43
44
45
46
47
48
use std::{collections::BTreeSet, fmt::Display, mem};

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Mode {
	#[default]
	Normal,
	Select(usize, BTreeSet<usize>),
	Unset(usize, BTreeSet<usize>),
}

impl Mode {
	pub fn visual_mut(&mut self) -> Option<(usize, &mut BTreeSet<usize>)> {
		match self {
			Mode::Normal => None,
			Mode::Select(start, indices) => Some((*start, indices)),
			Mode::Unset(start, indices) => Some((*start, indices)),
		}
	}

	pub fn take_visual(&mut self) -> Option<(usize, BTreeSet<usize>)> {
		match mem::take(self) {
			Mode::Normal => None,
			Mode::Select(start, indices) => Some((start, indices)),
			Mode::Unset(start, indices) => Some((start, indices)),
		}
	}
}

impl Mode {
	#[inline]
	pub fn is_select(&self) -> bool { matches!(self, Mode::Select(..)) }

	#[inline]
	pub fn is_unset(&self) -> bool { matches!(self, Mode::Unset(..)) }

	#[inline]
	pub fn is_visual(&self) -> bool { matches!(self, Mode::Select(..) | Mode::Unset(..)) }
}

impl Display for Mode {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.write_str(match self {
			Mode::Normal => "normal",
			Mode::Select(..) => "select",
			Mode::Unset(..) => "unset",
		})
	}
}