aboutsummaryrefslogtreecommitdiff
path: root/src/tui/popup.rs
blob: 20ff4674723079207186e79543cbc3a094e6dd47 (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
// 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::{
    style::{Color, Stylize},
    text::{Line, Span, Text},
    widgets::ListState,
};

use crate::{MAIN_BLUE_COLOR_INDEX, MAIN_PURPLE_COLOR_INDEX};

#[derive(Debug)]
pub enum PopupKind {
    Help,
    Message,
    Selection,
}

#[derive(Debug)]
pub struct PopupArea {
    pub is_popup: bool,
    pub popup_kind: Option<PopupKind>,
    pub popup_message: String,
    pub popup_list: Vec<String>,
    pub popup_state: ListState,
}

impl Default for PopupArea {
    fn default() -> Self {
        PopupArea {
            is_popup: false,
            popup_kind: None,
            popup_message: String::new(),
            popup_list: Vec::new(),
            popup_state: ListState::default(),
        }
    }
}

impl PopupArea {
    pub fn popup_help<'a>() -> Text<'a> {
        let help = [
            ("General", "first"),
            ("TAB: ", "Toggle areas (Entries, Keywords)"),
            ("/|Ctrl+f: ", "Enter search mode"),
            ("q|Ctrl+c: ", "Quit bibiman"),
            ("Entry Table", "sub"),
            ("j,k|↓,↑: ", "Select next/previous entry"),
            ("h,l|←,→: ", "Select next/previous column"),
            ("g|Home: ", "Go to first entry"),
            ("G|End: ", "Go to last entry"),
            ("s: ", "sort entries by selected column (toggles reversed)"),
            ("y: ", "yank/copy citekey of selected entry to clipboard"),
            ("e: ", "Open editor at selected entry"),
            ("o: ", "Open with selected entry associated PDF"),
            ("u: ", "Open DOI/URL of selected entry"),
            ("ESC: ", "Reset all lists"),
            ("Keyword List", "sub"),
            ("j,k|↓,↑: ", "Select next/previous item"),
            ("g|Home: ", "Go to first keyword"),
            ("G|End: ", "Go to last keyword"),
            ("ENTER: ", "Filter by selected keyword"),
            ("Search", "sub"),
            ("↓,↑,←,→: ", "Move cursor"),
            ("ENTER: ", "Confirm search"),
            ("ESC: ", "Abort search"),
        ];

        // let help_text: Vec<Line<'_>> = help
        //     .into_iter()
        //     .map(|(keys, help)| {
        //         if help == "first" {
        //             Line::from(
        //                 Span::raw(keys)
        //                     .bold()
        //                     .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)),
        //             )
        //         } else if help == "sub" {
        //             Line::from(
        //                 Span::raw(keys)
        //                     .bold()
        //                     .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)),
        //             )
        //         } else {
        //             Line::from(vec![
        //                 Span::raw(keys)
        //                     .bold()
        //                     .fg(Color::Indexed(MAIN_PURPLE_COLOR_INDEX)),
        //                 Span::raw(help),
        //             ])
        //         }
        //     })
        //     .collect();

        let mut helptext: Vec<Line<'_>> = vec![];

        for (keys, help) in help {
            if help == "first" {
                helptext.push(Line::from(
                    Span::raw(keys)
                        .bold()
                        .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)),
                ))
            } else if help == "sub" {
                helptext.push(Line::from(""));
                helptext.push(Line::from(
                    Span::raw(keys)
                        .bold()
                        .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)),
                ))
            } else {
                helptext.push(Line::from(vec![
                    Span::raw(keys)
                        .bold()
                        .fg(Color::Indexed(MAIN_PURPLE_COLOR_INDEX)),
                    Span::raw(help),
                ]))
            }
        }

        let text = Text::from(helptext);
        text
    }

    pub fn popup_message(&mut self, message: &str, object: String) {
        if object.is_empty() {
            self.popup_message = message.into();
        } else {
            self.popup_message = format!("{} \"{}\"", message, object);
        }
        self.popup_kind = Some(PopupKind::Message);
        self.is_popup = true;
    }

    pub fn popup_close_message(&mut self) {
        self.is_popup = false;
        self.popup_message.clear();
        self.popup_kind = None
    }
}