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
use crate::app::selection::Selection;
use crate::app::style::Style;
use crate::args::Args;
use crate::widget::style::Color;
use tui::style::Color as TuiColor;

/// Application states (flags) for managing the launcher.
#[derive(Clone, Debug)]
pub struct State {
	/// Is app running?
	pub running: bool,
	/// Style of the app.
	pub style: Style,
	/// Accent color of the app.
	pub color: TuiColor,
	/// Is the options menu (popup) showing?
	pub show_options: bool,
	/// Is the splash screen showing?
	pub show_splash: bool,
	/// Is the selection mode enabled?
	pub select: Option<Selection>,
	/// File explorer to run.
	pub file_explorer: String,
	/// Exit message of the app.
	pub exit_message: Option<String>,
}

impl Default for State {
	fn default() -> Self {
		Self {
			running: true,
			style: Style::default(),
			color: Color::default().get(),
			show_options: false,
			show_splash: false,
			select: None,
			file_explorer: String::from("xplr"),
			exit_message: None,
		}
	}
}

impl<'a> From<&'a Args> for State {
	fn from(args: &'a Args) -> Self {
		State {
			style: args.style,
			color: args.color.get(),
			show_splash: args.splash,
			select: args.select,
			file_explorer: args.file_explorer.to_string(),
			..Self::default()
		}
	}
}

impl State {
	/// Reverts back the values to default.
	pub fn refresh(&mut self) {
		let style = self.style;
		*self = Self::default();
		self.style = style;
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use pretty_assertions::assert_eq;
	#[test]
	fn test_app_state() {
		let mut state = State::default();
		state.refresh();
		assert_eq!(true, state.running);
		assert_eq!(Style::Plain, state.style);
		assert_eq!(TuiColor::Gray, state.color);
		assert_eq!(false, state.show_options);
		assert_eq!(false, state.show_splash);
		assert_eq!(None, state.select);
		assert_eq!("xplr", state.file_explorer);
		assert_eq!(None, state.exit_message);
	}
}