poddy/app/
mod.rs

1use crate::app::state::list::ListWatcher;
2use crate::app::state::AppState;
3use crate::client::Client;
4use crate::input::key::Key;
5use crate::Args;
6
7pub mod state;
8pub mod ui;
9
10#[derive(Debug, PartialEq, Eq)]
11pub enum AppReturn {
12    Exit,
13    Continue,
14}
15
16pub struct App {
17    state: AppState,
18    client: Client,
19    args: Args,
20    global: Global,
21}
22
23pub struct Global {
24    pub logs: bool,
25    pub help: bool,
26}
27
28impl Default for Global {
29    fn default() -> Self {
30        Self {
31            logs: false,
32            help: false,
33        }
34    }
35}
36
37impl App {
38    pub fn new(args: Args) -> Self {
39        let client = Client::new(args.clone());
40        Self {
41            state: AppState::Pods(ListWatcher::new(client.clone())),
42            client,
43            args,
44            global: Default::default(),
45        }
46    }
47
48    /// Handle a user action
49    pub async fn do_action(&mut self, key: Key) -> AppReturn {
50        log::debug!("Key: {key:?}");
51
52        match key {
53            Key::Ctrl('c') | Key::Char('q') => return AppReturn::Exit,
54            Key::Esc => {
55                if self.global.help {
56                    self.global.help = false;
57                } else {
58                    return AppReturn::Exit;
59                }
60            }
61            Key::Char('d') => {
62                self.state = AppState::Deployments(ListWatcher::new(self.client.clone()))
63            }
64            Key::Char('p') => self.state = AppState::Pods(ListWatcher::new(self.client.clone())),
65            Key::Char('l') => self.global.logs = !self.global.logs,
66            Key::Char('h') | Key::Char('?') => self.global.help = !self.global.help,
67            Key::Left => self.prev(),
68            Key::Right => self.next(),
69            _ => {
70                self.state.on_key(key).await;
71            }
72        }
73        AppReturn::Continue
74    }
75
76    pub fn state(&self) -> &AppState {
77        &self.state
78    }
79
80    pub fn global(&self) -> &Global {
81        &self.global
82    }
83
84    pub fn prev(&mut self) {
85        self.state = match &self.state {
86            AppState::Initializing => AppState::Initializing,
87            AppState::Pods(_) => AppState::Deployments(ListWatcher::new(self.client.clone())),
88            AppState::Deployments(_) => AppState::Pods(ListWatcher::new(self.client.clone())),
89        }
90    }
91
92    pub fn next(&mut self) {
93        self.state = match &self.state {
94            AppState::Initializing => AppState::Initializing,
95            AppState::Pods(_) => AppState::Deployments(ListWatcher::new(self.client.clone())),
96            AppState::Deployments(_) => AppState::Pods(ListWatcher::new(self.client.clone())),
97        }
98    }
99}