summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: c348d7fa68122fda9357f60b525f45e86bf7d390 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use textmode::Textmode as _;

pub struct State {
    readline: crate::readline::Readline,
    history: crate::history::History,
    focus: Focus,
    escape: bool,
    hide_readline: bool,
}

impl State {
    pub fn new() -> Self {
        let readline = crate::readline::Readline::new();
        let history = crate::history::History::new();
        let focus = Focus::Readline;
        Self {
            readline,
            history,
            focus,
            escape: false,
            hide_readline: false,
        }
    }

    pub async fn handle_key(
        &mut self,
        key: textmode::Key,
    ) -> Option<crate::action::Action> {
        if self.escape {
            self.escape = false;
            let mut fallthrough = false;
            match key {
                textmode::Key::Ctrl(b'e') => {
                    fallthrough = true;
                }
                textmode::Key::Ctrl(b'l') => {
                    return Some(crate::action::Action::ForceRedraw);
                }
                textmode::Key::Char('f') => {
                    if let Focus::History(idx) = self.focus {
                        return Some(
                            crate::action::Action::ToggleFullscreen(idx),
                        );
                    }
                }
                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,
                    };
                    return Some(crate::action::Action::UpdateFocus(
                        new_focus,
                    ));
                }
                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)
                        }
                    };
                    return Some(crate::action::Action::UpdateFocus(
                        new_focus,
                    ));
                }
                textmode::Key::Char('r') => {
                    return Some(crate::action::Action::UpdateFocus(
                        Focus::Readline,
                    ));
                }
                _ => {}
            }
            if !fallthrough {
                return None;
            }
        } else if key == textmode::Key::Ctrl(b'e') {
            self.escape = true;
            return None;
        }

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

    pub async fn render(
        &self,
        out: &mut textmode::Output,
        hard: bool,
    ) -> anyhow::Result<()> {
        out.clear();
        match self.focus {
            Focus::Readline => {
                self.history
                    .render(out, self.readline.lines(), None)
                    .await?;
                self.readline.render(out, true).await?;
            }
            Focus::History(idx) => {
                if self.hide_readline || self.history.is_fullscreen(idx).await
                {
                    self.history.render(out, 0, Some(idx)).await?;
                } else {
                    self.history
                        .render(out, self.readline.lines(), Some(idx))
                        .await?;
                    let pos = out.screen().cursor_position();
                    self.readline.render(out, false).await?;
                    out.move_to(pos.0, pos.1);
                }
            }
        }
        if hard {
            out.hard_refresh().await?;
        } else {
            out.refresh().await?;
        }
        Ok(())
    }

    pub async fn handle_action(
        &mut self,
        action: crate::action::Action,
        out: &mut textmode::Output,
        action_w: &async_std::channel::Sender<crate::action::Action>,
    ) {
        let mut hard_refresh = false;
        match action {
            crate::action::Action::Render => {}
            crate::action::Action::ForceRedraw => {
                hard_refresh = true;
            }
            crate::action::Action::Run(ref cmd) => {
                let idx =
                    self.history.run(cmd, action_w.clone()).await.unwrap();
                self.focus = Focus::History(idx);
                self.hide_readline = true;
            }
            crate::action::Action::UpdateFocus(new_focus) => {
                self.focus = new_focus;
                self.hide_readline = false;
            }
            crate::action::Action::ToggleFullscreen(idx) => {
                self.history.toggle_fullscreen(idx).await;
            }
            crate::action::Action::Resize(new_size) => {
                self.readline.resize(new_size).await;
                self.history.resize(new_size).await;
                out.set_size(new_size.0, new_size.1);
                out.hard_refresh().await.unwrap();
            }
            crate::action::Action::Quit => {
                // the debouncer should return None in this case
                unreachable!();
            }
        }
        self.render(out, hard_refresh).await.unwrap();
    }
}

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