aboutsummaryrefslogtreecommitdiff
path: root/src/tui/commands.rs
blob: dc6b2c815746fb43bb560845f33c19924ac1f26f (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// bibiman - a TUI for managing BibLaTeX databases
// Copyright (C) 2024  lukeflo
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
/////

use ratatui::crossterm::event::{
    Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind,
};
use tui_input::Input;

// Possible ressources to open
#[derive(Debug, PartialEq, Eq)]
pub enum OpenRessource {
    Pdf,
    WebLink,
    Note,
}

/// Application command.
#[derive(Debug, PartialEq, Eq)]
pub enum CmdAction {
    // Toggle area
    ToggleArea,
    // Scroll list/table down
    SelectNextRow(u16),
    // Scroll list/table up.
    SelectPrevRow(u16),
    // Select nex table col.
    SelectNextCol,
    // Select previous table col.
    SelectPrevCol,
    // Scroll info/preview area down
    ScrollInfoDown,
    // Scroll info/preview area up
    ScrollInfoUp,
    // Go to top.
    Top,
    // Go to bottom.
    Bottom,
    // Search list
    SearchList,
    // Reset lists
    ResetList,
    // Confirm search/selection
    Confirm,
    // Sort table/list
    SortList,
    // Yank selected item
    YankItem,
    // Edit file
    EditFile,
    // Open linked ressource
    Open(OpenRessource),
    // Input command.
    Input(InputCmdAction),
    // Hexdump command.
    Exit,
    // Do nothing.
    Nothing,
}

impl From<KeyEvent> for CmdAction {
    fn from(key_event: KeyEvent) -> Self {
        match key_event.code {
            // Go to first/last entry of selected list/table
            KeyCode::Char('g') | KeyCode::Home => Self::Top,
            KeyCode::Char('G') | KeyCode::End => Self::Bottom,
            // Scroll columns of EntryTable
            KeyCode::Right | KeyCode::Char('l') => Self::SelectNextCol,
            KeyCode::Left | KeyCode::Char('h') => Self::SelectPrevCol,
            // Scroll table/list vertically by 1
            KeyCode::Down | KeyCode::Char('j') => {
                if key_event.modifiers == KeyModifiers::ALT {
                    Self::ScrollInfoDown
                } else {
                    Self::SelectNextRow(1)
                }
            }
            KeyCode::Up | KeyCode::Char('k') => {
                if key_event.modifiers == KeyModifiers::ALT {
                    Self::ScrollInfoUp
                } else {
                    Self::SelectPrevRow(1)
                }
            }
            // Scroll table/list vertically by 5
            KeyCode::Char('d') => {
                if key_event.modifiers == KeyModifiers::CONTROL {
                    Self::SelectNextRow(5)
                } else {
                    Self::Nothing
                }
            }
            KeyCode::Char('u') => {
                if key_event.modifiers == KeyModifiers::CONTROL {
                    Self::SelectPrevRow(5)
                } else {
                    Self::Open(OpenRessource::WebLink)
                }
            }
            // Scroll info/preview area
            KeyCode::PageDown => Self::ScrollInfoDown,
            KeyCode::PageUp => Self::ScrollInfoUp,
            // Exit App
            KeyCode::Char('q') => Self::Exit,
            KeyCode::Char('c') | KeyCode::Char('C') => {
                if key_event.modifiers == KeyModifiers::CONTROL {
                    Self::Exit
                } else {
                    Self::Nothing
                }
            }
            // Switch selected area
            KeyCode::Tab => Self::ToggleArea,
            KeyCode::BackTab => Self::ToggleArea,
            // Enter search mode
            KeyCode::Char('/') => Self::Input(InputCmdAction::Enter),
            KeyCode::Char('f') => {
                if key_event.modifiers == KeyModifiers::CONTROL {
                    Self::Input(InputCmdAction::Enter)
                } else {
                    Self::Nothing
                }
            }
            // KeyCode::Backspace => Self::Input(InputCommand::Resume(Event::Key(key_event))),
            // Confirm selection
            KeyCode::Enter => Self::Confirm,
            // Reset lists/tables
            KeyCode::Esc => Self::ResetList,
            // Open linked ressource
            KeyCode::Char('o') => Self::Open(OpenRessource::Pdf),
            // KeyCode::Char('u') => Self::Open(OpenRessource::WebLink),
            // Edit currently selected entry
            KeyCode::Char('e') => Self::EditFile,
            // Yank selected item/value
            KeyCode::Char('y') => Self::YankItem,
            // Sort entry table by selected col
            KeyCode::Char('s') => Self::SortList,
            // Else do nothing
            _ => Self::Nothing,
        }
    }
}

impl From<MouseEvent> for CmdAction {
    fn from(mouse_event: MouseEvent) -> Self {
        match mouse_event.kind {
            MouseEventKind::ScrollDown => Self::SelectNextRow(1),
            MouseEventKind::ScrollUp => Self::SelectPrevRow(1),
            _ => Self::Nothing,
        }
    }
}

/// Input mode command.
#[derive(Debug, PartialEq, Eq)]
pub enum InputCmdAction {
    // Handle input.
    Handle(Event),
    // Enter input mode.
    Enter,
    // Confirm input.
    Confirm,
    // Exit input mode
    Exit,
    // Do nothing
    Nothing,
}

impl InputCmdAction {
    /// Parses the event.
    pub fn parse(key_event: KeyEvent, input: &Input) -> Self {
        if key_event.code == KeyCode::Backspace && input.value().is_empty() {
            Self::Nothing
        } else if key_event.code == KeyCode::Esc {
            Self::Exit
        } else if key_event.code == KeyCode::Enter {
            Self::Confirm
        } else {
            Self::Handle(Event::Key(key_event))
        }
    }
}