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
use crate::app::state::{AppState, Pods};
use crate::client::Client;
use crate::input::key::Key;
use crate::Args;

pub mod state;
pub mod ui;

#[derive(Debug, PartialEq, Eq)]
pub enum AppReturn {
    Exit,
    Continue,
}

pub struct App {
    state: AppState,
    client: Client,
    args: Args,
    global: Global,
}

pub struct Global {
    pub logs: bool,
    pub help: bool,
}

impl Default for Global {
    fn default() -> Self {
        Self {
            logs: false,
            help: false,
        }
    }
}

impl App {
    pub fn new(args: Args) -> Self {
        let client = Client::new(args.clone());
        Self {
            state: AppState::Pods(Pods::new(client.clone())),
            client,
            args,
            global: Default::default(),
        }
    }

    /// Handle a user action
    pub async fn do_action(&mut self, key: Key) -> AppReturn {
        log::debug!("Key: {key:?}");

        match key {
            Key::Ctrl('c') | Key::Char('q') => return AppReturn::Exit,
            Key::Esc => {
                if self.global.help {
                    self.global.help = false;
                } else {
                    return AppReturn::Exit;
                }
            }
            Key::Char('d') => self.state = AppState::Deployments,
            Key::Char('p') => self.state = AppState::Pods(Pods::new(self.client.clone())),
            Key::Char('l') => self.global.logs = !self.global.logs,
            Key::Char('h') | Key::Char('?') => self.global.help = !self.global.help,
            _ => {
                self.state.on_key(key).await;
            }
        }
        AppReturn::Continue
    }

    pub fn state(&self) -> &AppState {
        &self.state
    }

    pub fn global(&self) -> &Global {
        &self.global
    }
}