summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: 67a29e83240d9317e98eb075b4cff6dcf5ea6978 (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use textmode::Textmode as _;

pub struct State {
    readline: crate::readline::Readline,
    history: crate::history::History,
    focus: Focus,
    output: textmode::Output,
    escape: bool,
    action: async_std::channel::Sender<crate::action::Action>,
}

impl State {
    pub fn new(
        action: async_std::channel::Sender<crate::action::Action>,
        output: textmode::Output,
    ) -> Self {
        let readline = crate::readline::Readline::new(action.clone());
        let history = crate::history::History::new(action.clone());
        let focus = Focus::Readline;
        Self {
            readline,
            history,
            focus,
            output,
            escape: false,
            action,
        }
    }

    pub async fn render(&mut self) -> anyhow::Result<()> {
        self.output.clear();
        match self.focus {
            Focus::Readline => {
                self.history
                    .render(&mut self.output, self.readline.lines(), None)
                    .await?;
                self.readline.render(&mut self.output, true).await?;
            }
            Focus::History(idx) => {
                self.history.render(&mut self.output, 0, Some(idx)).await?;
            }
        }
        self.output.refresh().await?;
        Ok(())
    }

    pub async fn handle_action(&mut self, action: crate::action::Action) {
        match action {
            crate::action::Action::Render => {
                self.render().await.unwrap();
            }
            crate::action::Action::Run(ref cmd) => {
                self.history.run(cmd).await.unwrap();
            }
            crate::action::Action::UpdateFocus(new_focus) => {
                self.focus = new_focus;
                self.render().await.unwrap();
            }
            crate::action::Action::Resize(new_size) => {
                self.readline.resize(new_size).await;
                self.history.resize(new_size).await;
                self.output.set_size(new_size.0, new_size.1);
                self.output.hard_refresh().await.unwrap();
                self.render().await.unwrap();
            }
        }
    }

    pub async fn handle_input(&mut self, key: textmode::Key) -> bool {
        if self.escape {
            let mut ret = true;
            match key {
                textmode::Key::Ctrl(b'e') => {
                    ret = false; // fall through and handle normally
                }
                textmode::Key::Char('f') => {
                    if let Focus::History(idx) = self.focus {
                        self.history.toggle_fullscreen(idx).await;
                        self.render().await.unwrap();
                    }
                }
                textmode::Key::Char('j') => {
                    let new_focus = match self.focus {
                        Focus::History(idx) => {
                            if idx >= self.history.entry_count() - 1 {
                                Focus::Readline
                            } else {
                                Focus::History(idx + 1)
                            }
                        }
                        Focus::Readline => Focus::Readline,
                    };
                    self.focus = new_focus;
                    self.render().await.unwrap();
                }
                textmode::Key::Char('k') => {
                    let new_focus = match self.focus {
                        Focus::History(idx) => {
                            if idx == 0 {
                                Focus::History(0)
                            } else {
                                Focus::History(idx - 1)
                            }
                        }
                        Focus::Readline => {
                            Focus::History(self.history.entry_count() - 1)
                        }
                    };
                    self.focus = new_focus;
                    self.render().await.unwrap();
                }
                textmode::Key::Char('r') => {
                    self.focus = Focus::Readline;
                    self.render().await.unwrap();
                }
                _ => {}
            }
            self.escape = false;
            if ret {
                return false;
            }
        } else if key == textmode::Key::Ctrl(b'e') {
            self.escape = true;
            return false;
        }

        match self.focus {
            Focus::Readline => self.readline.handle_key(key).await,
            Focus::History(idx) => self.history.handle_key(key, idx).await,
        }
    }

    pub async fn resize(&mut self) {
        let size = terminal_size::terminal_size().map_or(
            (24, 80),
            |(terminal_size::Width(w), terminal_size::Height(h))| (h, w),
        );
        self.action
            .send(crate::action::Action::Resize(size))
            .await
            .unwrap();
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Focus {
    Readline,
    History(usize),
}