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
10pub struct State {
12 pub running: bool,
14 pub style: Style,
16 pub color: TuiColor,
18 pub show_options: bool,
20 pub show_splash: bool,
22 pub select: Option<Selection>,
24 pub file_explorer: String,
26 pub detail_level: KeyDetail,
28 pub exit_message: Option<String>,
30 pub show_logs: bool,
32 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 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}