aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app.rs
blob: 970a064359d73e81702f3a7fa4d0f65e41c196e5 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// 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 crate::backend::{bib::*, search};
use std::error;

use arboard::Clipboard;
use itertools::Itertools;
use ratatui::widgets::{ListState, TableState};

// Application result type.
pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;

// Areas in which actions are possible
#[derive(Debug)]
pub enum CurrentArea {
    EntryArea,
    TagArea,
    SearchArea,
    HelpArea,
}

// Check which area was active when popup set active
#[derive(Debug)]
pub enum FormerArea {
    EntryArea,
    TagArea,
    SearchArea,
}

// Application.
#[derive(Debug)]
pub struct App {
    // Is the application running?
    pub running: bool,
    // main bibliography
    pub main_biblio: BibiMain,
    // bibliographic data
    pub biblio_data: BibiData,
    // tag list
    pub tag_list: TagList,
    // table items
    pub entry_table: EntryTable,
    // scroll state info buffer
    pub scroll_info: u16,
    // area
    pub current_area: CurrentArea,
    // mode for popup window
    pub former_area: Option<FormerArea>,
    // search string
    pub search_string: String,
}

// Define the fundamental List
#[derive(Debug)]
pub struct TagList {
    pub tag_list_items: Vec<TagListItem>,
    pub tag_list_state: ListState,
}

// Structure of the list items.
#[derive(Debug)]
pub struct TagListItem {
    pub info: String,
}

// Function to process inputed characters and convert them (to string, or more complex function)
impl TagListItem {
    pub fn new(info: &str) -> Self {
        Self {
            info: info.to_string(),
        }
    }
}

impl FromIterator<String> for TagList {
    fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
        let tag_list_items = iter
            .into_iter()
            .map(|info| TagListItem::new(&info))
            .collect();
        let tag_list_state = ListState::default(); // for preselection: .with_selected(Some(0));
        Self {
            tag_list_items,
            tag_list_state,
        }
    }
}

// // Also possible with vector.
impl FromIterator<Vec<String>> for EntryTable {
    fn from_iter<T: IntoIterator<Item = Vec<String>>>(iter: T) -> Self {
        let entry_table_items = iter
            .into_iter()
            .sorted()
            .map(|i| EntryTableItem::new(&i[0], &i[1], &i[2], &i[3], &i[4]))
            .collect();
        let entry_table_state = TableState::default().with_selected(0);
        Self {
            entry_table_items,
            entry_table_state,
        }
    }
}

// Define list containing entries as table
#[derive(Debug)]
pub struct EntryTable {
    pub entry_table_items: Vec<EntryTableItem>,
    pub entry_table_state: TableState,
}

// Define contents of each entry table row
#[derive(Debug)]
pub struct EntryTableItem {
    pub authors: String,
    pub title: String,
    pub year: String,
    pub pubtype: String,
    pub citekey: String,
    // pub year: u16,
}

impl EntryTableItem {
    pub fn new(authors: &str, title: &str, year: &str, pubtype: &str, citekey: &str) -> Self {
        Self {
            authors: authors.to_string(),
            title: title.to_string(),
            year: year.to_string(),
            pubtype: pubtype.to_string(),
            citekey: citekey.to_string(),
        }
    }

    // This functions decides which fields are rendered in the entry table
    // Fields which should be usable but not visible can be left out
    pub fn ref_vec(&self) -> Vec<&String> {
        vec![&self.authors, &self.title, &self.year, &self.pubtype]
    }

    pub fn authors(&self) -> &str {
        &self.authors
    }

    pub fn title(&self) -> &str {
        &self.title
    }

    pub fn year(&self) -> &str {
        &self.year
    }

    pub fn pubtype(&self) -> &str {
        &self.pubtype
    }

    pub fn citekey(&self) -> &str {
        &self.citekey
    }
}

impl Default for App {
    fn default() -> Self {
        let running = true;
        let main_biblio = BibiMain::new();
        let biblio_data = BibiData::new(&main_biblio.bibliography, &main_biblio.citekeys);
        let tag_list = TagList::from_iter(main_biblio.citekeys.clone());
        let entry_table = EntryTable::from_iter(biblio_data.entry_list.bibentries.clone());
        let current_area = CurrentArea::EntryArea;
        Self {
            running,
            main_biblio,
            biblio_data,
            tag_list,
            entry_table,
            scroll_info: 0,
            current_area,
            former_area: None,
            search_string: String::new(),
        }
    }
}

impl App {
    // Constructs a new instance of [`App`].
    pub fn new() -> Self {
        Self::default()
    }

    // Handles the tick event of the terminal.
    pub fn tick(&self) {}

    // Set running to false to quit the application.
    pub fn quit(&mut self) {
        self.running = false;
    }

    // Toggle moveable list between entries and tags
    pub fn toggle_area(&mut self) {
        match self.current_area {
            CurrentArea::EntryArea => {
                self.current_area = CurrentArea::TagArea;
                self.tag_list.tag_list_state.select(Some(0))
            }
            CurrentArea::TagArea => {
                self.current_area = CurrentArea::EntryArea;
                self.tag_list.tag_list_state.select(None)
            }
            CurrentArea::SearchArea => {
                if let Some(former_area) = &self.former_area {
                    match former_area {
                        FormerArea::EntryArea => self.current_area = CurrentArea::EntryArea,
                        FormerArea::TagArea => self.current_area = CurrentArea::TagArea,
                        _ => {}
                    }
                }
            }
            CurrentArea::HelpArea => {
                if let Some(former_area) = &self.former_area {
                    match former_area {
                        FormerArea::EntryArea => self.current_area = CurrentArea::EntryArea,
                        FormerArea::TagArea => self.current_area = CurrentArea::TagArea,
                        FormerArea::SearchArea => self.current_area = CurrentArea::SearchArea,
                    }
                }
            }
        }
    }

    pub fn scroll_info_down(&mut self) {
        self.scroll_info = self.scroll_info + 1;
    }

    pub fn scroll_info_up(&mut self) {
        if self.scroll_info == 0 {
            {}
        } else {
            self.scroll_info = self.scroll_info - 1;
        }
    }

    pub fn select_none(&mut self) {
        match self.current_area {
            CurrentArea::EntryArea => self.entry_table.entry_table_state.select(None),
            CurrentArea::TagArea => self.tag_list.tag_list_state.select(None),
            _ => {}
        }
        // self.tag_list.tag_list_state.select(None);
    }

    pub fn select_next(&mut self) {
        self.scroll_info = 0;
        match self.current_area {
            CurrentArea::EntryArea => self.entry_table.entry_table_state.select_next(),
            CurrentArea::TagArea => self.tag_list.tag_list_state.select_next(),
            _ => {}
        }
        // self.tag_list.tag_list_state.select_next();
    }
    pub fn select_previous(&mut self) {
        self.scroll_info = 0;
        match self.current_area {
            CurrentArea::EntryArea => self.entry_table.entry_table_state.select_previous(),
            CurrentArea::TagArea => self.tag_list.tag_list_state.select_previous(),
            _ => {}
        }
        // self.tag_list.tag_list_state.select_previous();
    }

    pub fn select_first(&mut self) {
        match self.current_area {
            CurrentArea::EntryArea => {
                self.scroll_info = 0;
                self.entry_table.entry_table_state.select_first();
            }
            CurrentArea::TagArea => self.tag_list.tag_list_state.select_first(),
            _ => {}
        }
        // self.tag_list.tag_list_state.select_first();
    }

    pub fn select_last(&mut self) {
        match self.current_area {
            CurrentArea::EntryArea => {
                self.scroll_info = 0;
                self.entry_table.entry_table_state.select_last();
            }
            CurrentArea::TagArea => self.tag_list.tag_list_state.select_last(),
            _ => {}
        }
        // self.tag_list.tag_list_state.select_last();
    }

    // Get the citekey of the selected entry
    pub fn get_selected_citekey(&self) -> &str {
        let idx = self.entry_table.entry_table_state.selected().unwrap();
        let citekey = &self.entry_table.entry_table_items[idx].citekey;
        citekey
    }

    // Yank the passed string to system clipboard
    pub fn yank_text(selection: &str) {
        let mut clipboard = Clipboard::new().unwrap();
        let yanked_text = selection.to_string();
        clipboard.set_text(yanked_text).unwrap();
    }

    // Search entry list
    pub fn search_entries(&mut self) {
        let orig_list = &self.biblio_data.entry_list.bibentries;
        let filtered_list = search::search_entry_list(&self.search_string, orig_list.clone());
        self.entry_table = EntryTable::from_iter(filtered_list)
    }

    pub fn reset_entry_table(&mut self) {
        self.entry_table = EntryTable::from_iter(self.biblio_data.entry_list.bibentries.clone())
    }
}