Skip to main content

gpg_tui/app/
state.rs

1use crate::app::selection::Selection;
2use crate::app::style::Style;
3use crate::args::Args;
4use crate::gpg::key::KeyDetail;
5use crate::widget::style::Color;
6use log::LevelFilter;
7use ratatui::style::Color as TuiColor;
8use tui_logger::TuiWidgetState;
9
10/// Application states (flags) for managing the launcher.
11pub struct State {
12	/// Is app running?
13	pub running: bool,
14	/// Style of the app.
15	pub style: Style,
16	/// Accent color of the app.
17	pub color: TuiColor,
18	/// Is the options menu (popup) showing?
19	pub show_options: bool,
20	/// Is the splash screen showing?
21	pub show_splash: bool,
22	/// Is the selection mode enabled?
23	pub select: Option<Selection>,
24	/// File explorer to run.
25	pub file_explorer: String,
26	/// Detail level for the keys table.
27	pub detail_level: KeyDetail,
28	/// Exit message of the app.
29	pub exit_message: Option<String>,
30	/// Whether if the logs are shown.
31	pub show_logs: bool,
32	/// Logger state.
33	pub logger_state: TuiWidgetState,
34}
35
36impl Default for State {
37	fn default() -> Self {
38		Self {
39			running: true,
40			style: Style::default(),
41			color: Color::default().get(),
42			show_options: false,
43			show_splash: false,
44			select: None,
45			file_explorer: String::from("xplr"),
46			detail_level: KeyDetail::default(),
47			exit_message: None,
48			show_logs: false,
49			logger_state: TuiWidgetState::new()
50				.set_default_display_level(LevelFilter::Trace),
51		}
52	}
53}
54
55impl<'a> From<&'a Args> for State {
56	fn from(args: &'a Args) -> Self {
57		State {
58			style: args.style,
59			color: args.color.get(),
60			show_splash: args.splash,
61			select: args.select,
62			file_explorer: args.file_explorer.to_string(),
63			detail_level: args.detail_level,
64			..Self::default()
65		}
66	}
67}
68
69impl State {
70	/// Reverts back the values to default.
71	pub fn refresh(&mut self) {
72		let style = self.style;
73		let detail_level = self.detail_level;
74		let color = self.color;
75		*self = Self::default();
76		self.style = style;
77		self.detail_level = detail_level;
78		self.color = color;
79	}
80}
81
82#[cfg(test)]
83mod tests {
84	use super::*;
85	use pretty_assertions::assert_eq;
86	#[test]
87	fn test_app_state() {
88		let mut state = State::default();
89		state.refresh();
90		assert_eq!(true, state.running);
91		assert_eq!(Style::Plain, state.style);
92		assert_eq!(TuiColor::Gray, state.color);
93		assert_eq!(false, state.show_options);
94		assert_eq!(false, state.show_splash);
95		assert_eq!(None, state.select);
96		assert_eq!(KeyDetail::default(), state.detail_level);
97		assert_eq!("xplr", state.file_explorer);
98		assert_eq!(None, state.exit_message);
99	}
100}