aboutsummaryrefslogtreecommitdiff
path: root/src/tui/command.rs
blob: 823a1dc4389cff5b0387b7bffde5b051209d6fc2 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// 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::bibiman::entries::EntryTableColumn;
use crate::bibiman::search::BibiSearch;
use crate::bibiman::{Bibiman, FormerArea};
use crate::tui::Tui;
use color_eyre::eyre::{Context, Ok, Result};
use core::panic;
use editor_command::EditorBuilder;
use ratatui::widgets::ScrollbarState;
use std::process::{Command, Stdio};

impl Bibiman {
    // Entry Table commands

    /// Select next entry in Table holding the bibliographic entries.
    ///
    /// Takes u16 value as argument to specify number of entries which
    /// should be scrolled
    pub fn select_next_entry(&mut self, entries: u16) {
        self.entry_table.entry_info_scroll = 0;
        self.entry_table.entry_info_scroll_state =
            self.entry_table.entry_info_scroll_state.position(0);
        self.entry_table.entry_table_state.scroll_down_by(entries);
        self.entry_table.entry_scroll_state = self
            .entry_table
            .entry_scroll_state
            .position(self.entry_table.entry_table_state.selected().unwrap());
    }

    /// Select previous entry in Table holding the bib entries.
    ///
    /// Takes u16 value as argument to specify number of entries which
    /// should be scrolled
    pub fn select_previous_entry(&mut self, entries: u16) {
        self.entry_table.entry_info_scroll = 0;
        self.entry_table.entry_info_scroll_state =
            self.entry_table.entry_info_scroll_state.position(0);
        self.entry_table.entry_table_state.scroll_up_by(entries);
        self.entry_table.entry_scroll_state = self
            .entry_table
            .entry_scroll_state
            .position(self.entry_table.entry_table_state.selected().unwrap());
    }

    /// Select first entry in bib list
    pub fn select_first_entry(&mut self) {
        self.entry_table.entry_info_scroll = 0;
        self.entry_table.entry_info_scroll_state =
            self.entry_table.entry_info_scroll_state.position(0);
        self.entry_table.entry_table_state.select_first();
        self.entry_table.entry_scroll_state = self.entry_table.entry_scroll_state.position(0);
    }

    /// Select last entry in bib list
    pub fn select_last_entry(&mut self) {
        self.entry_table.entry_info_scroll = 0;
        self.entry_table.entry_info_scroll_state =
            self.entry_table.entry_info_scroll_state.position(0);
        self.entry_table.entry_table_state.select_last();
        self.entry_table.entry_scroll_state = self
            .entry_table
            .entry_scroll_state
            .position(self.entry_table.entry_table_items.len());
    }

    /// Select next (right) column of entry table
    pub fn select_next_column(&mut self) {
        match self.entry_table.entry_table_selected_column {
            EntryTableColumn::Authors => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Title;
            }
            EntryTableColumn::Title => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Year;
            }
            EntryTableColumn::Year => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Pubtype;
            }
            EntryTableColumn::Pubtype => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Authors;
            }
        }
    }

    /// Select previous (left) column of entry table
    pub fn select_prev_column(&mut self) {
        match self.entry_table.entry_table_selected_column {
            EntryTableColumn::Authors => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Pubtype;
            }
            EntryTableColumn::Title => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Authors;
            }
            EntryTableColumn::Year => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Title;
            }
            EntryTableColumn::Pubtype => {
                self.entry_table.entry_table_selected_column = EntryTableColumn::Year;
            }
        }
    }

    // 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
    }

    pub fn run_editor(&mut self, tui: &mut Tui) -> Result<()> {
        // get filecontent and citekey for calculating line number
        let citekey = self.get_selected_citekey();
        // create independent copy of citekey for finding entry after updating list
        let saved_key = citekey.to_owned();
        let filepath = self.main_biblio.bibfile.display().to_string();
        let filecontent = self.main_biblio.bibfilestring.clone();
        let mut line_count = 0;

        for line in filecontent.lines() {
            line_count = line_count + 1;
            // if reaching the citekey break the loop
            // if reaching end of lines without match, reset to 0
            if line.contains(&citekey) {
                break;
            } else if line_count == filecontent.len() {
                eprintln!(
                    "Citekey {} not found, opening file {} at line 1",
                    &citekey, &filepath
                );
                line_count = 0;
                break;
            }
        }

        // Exit TUI to enter editor
        tui.exit()?;
        // Use VISUAL or EDITOR. Set "vi" as last fallback
        let mut cmd: Command = EditorBuilder::new()
            .environment()
            .source(Some("vi"))
            .build()
            .unwrap();
        // Prepare arguments to open file at specific line
        let args: Vec<String> = vec![format!("+{}", line_count), filepath];
        let status = cmd.args(&args).status()?;
        if !status.success() {
            eprintln!("Spawning editor failed with status {}", status);
        }

        // Enter TUI again
        tui.enter()?;
        tui.terminal.clear()?;

        // Update the database and the lists to show changes
        self.update_lists();

        // Search for entry, selected before editing, by matching citekeys
        // Use earlier saved copy of citekey to match
        let mut idx_count = 0;
        loop {
            if self.entry_table.entry_table_items[idx_count]
                .citekey
                .contains(&saved_key)
            {
                break;
            }
            idx_count = idx_count + 1
        }

        // Set selected entry to vec-index of match
        self.entry_table.entry_table_state.select(Some(idx_count));

        Ok(())
    }

    // Search entry list
    pub fn search_entries(&mut self) {
        // Use snapshot of entry list saved when starting the search
        // so deleting a char, will show former entries too
        let orig_list = self.entry_table.entry_table_at_search_start.clone();
        let filtered_list =
            BibiSearch::search_entry_list(&mut self.search_struct.search_string, orig_list.clone());
        self.entry_table.entry_table_items = filtered_list;
        if self.entry_table.entry_table_reversed_sort {
            self.entry_table.sort_entry_table(false);
        }
        self.entry_table.entry_scroll_state = ScrollbarState::content_length(
            self.entry_table.entry_scroll_state,
            self.entry_table.entry_table_items.len(),
        );
    }

    // Open file connected with entry through 'file' or 'pdf' field
    pub fn open_connected_file(&mut self) -> Result<()> {
        let idx = self.entry_table.entry_table_state.selected().unwrap();
        let filepath = &self.entry_table.entry_table_items[idx].filepath.clone();

        // Build command to execute pdf-reader. 'xdg-open' is Linux standard
        let cmd = {
            match std::env::consts::OS {
                "linux" => String::from("xdg-open"),
                "macos" => String::from("open"),
                "windows" => String::from("start"),
                _ => panic!("Couldn't detect OS for setting correct opener"),
            }
        };

        // Pass filepath as argument, pipe stdout and stderr to /dev/null
        // to keep the TUI clean (where is it piped on Windows???)
        let _ = Command::new(&cmd)
            .arg(&filepath)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .wrap_err("Opening file not possible");

        Ok(())
    }

    pub fn open_doi_url(&mut self) -> Result<()> {
        let idx = self.entry_table.entry_table_state.selected().unwrap();
        let web_adress = self.entry_table.entry_table_items[idx].doi_url.clone();

        // Resolve strings using the resolving function of dx.doi.org, so the
        // terminal is not blocked by the resolving process
        let url = if web_adress.starts_with("10.") {
            let prefix = "https://doi.org/".to_string();
            prefix + &web_adress
        } else if web_adress.starts_with("www.") {
            let prefix = "https://".to_string();
            prefix + &web_adress
        } else {
            web_adress
        };

        // Build command to execute browser. 'xdg-open' is Linux standard
        let cmd = {
            match std::env::consts::OS {
                "linux" => String::from("xdg-open"),
                "macos" => String::from("open"),
                "windows" => String::from("start"),
                _ => panic!("Couldn't detect OS for setting correct opener"),
            }
        };

        // Pass filepath as argument, pipe stdout and stderr to /dev/null
        // to keep the TUI clean (where is it piped on Windows???)
        let _ = Command::new(&cmd)
            .arg(url)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .wrap_err("Opening file not possible");

        Ok(())
    }
}

impl Bibiman {
    // Tag List commands

    // Movement
    pub fn select_next_tag(&mut self, keywords: u16) {
        self.tag_list.tag_list_state.scroll_down_by(keywords);
        self.tag_list.tag_scroll_state = self
            .tag_list
            .tag_scroll_state
            .position(self.tag_list.tag_list_state.selected().unwrap());
    }

    pub fn select_previous_tag(&mut self, keywords: u16) {
        self.tag_list.tag_list_state.scroll_up_by(keywords);
        self.tag_list.tag_scroll_state = self
            .tag_list
            .tag_scroll_state
            .position(self.tag_list.tag_list_state.selected().unwrap());
    }

    pub fn select_first_tag(&mut self) {
        self.tag_list.tag_list_state.select_first();
        self.tag_list.tag_scroll_state = self.tag_list.tag_scroll_state.position(0);
    }

    pub fn select_last_tag(&mut self) {
        self.tag_list.tag_list_state.select_last();
        self.tag_list.tag_scroll_state = self
            .tag_list
            .tag_scroll_state
            .position(self.tag_list.tag_list_items.len());
    }

    pub fn get_selected_tag(&self) -> &str {
        let idx = self.tag_list.tag_list_state.selected().unwrap();
        let keyword = &self.tag_list.tag_list_items[idx];
        // let keyword = &self.tag_list.tag_list_items[idx].keyword;
        keyword
    }

    pub fn search_tags(&mut self) {
        let orig_list = &self.main_biblio.keyword_list;
        let filtered_list =
            BibiSearch::search_tag_list(&self.search_struct.search_string, orig_list.clone());
        self.tag_list.tag_list_items = filtered_list;
        // Update scrollbar length after filtering list
        self.tag_list.tag_scroll_state = ScrollbarState::content_length(
            self.tag_list.tag_scroll_state,
            self.tag_list.tag_list_items.len(),
        );
    }

    pub fn filter_tags_by_entries(&mut self) {
        let mut filtered_keywords: Vec<String> = Vec::new();

        let orig_list = &self.entry_table.entry_table_items;

        for e in orig_list {
            if !e.keywords.is_empty() {
                let mut key_vec: Vec<String> = e
                    .keywords
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
                filtered_keywords.append(&mut key_vec);
            }
        }

        filtered_keywords.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
        filtered_keywords.dedup();

        self.search_struct.filtered_tag_list = filtered_keywords.clone();
        self.tag_list.tag_list_items = filtered_keywords;
        self.tag_list.tag_scroll_state = ScrollbarState::content_length(
            self.tag_list.tag_scroll_state,
            self.tag_list.tag_list_items.len(),
        );
    }

    // Filter the entry list by tags when hitting enter
    // If already inside a filtered tag or entry list, apply the filtering
    // to the already filtered list only
    pub fn filter_for_tags(&mut self) {
        let orig_list = &self.entry_table.entry_table_items;
        let keyword = self.get_selected_tag();
        let filtered_list = BibiSearch::filter_entries_by_tag(&keyword, &orig_list);
        // self.tag_list.selected_keyword = keyword.to_string();
        self.tag_list.selected_keywords.push(keyword.to_string());
        self.entry_table.entry_table_items = filtered_list;
        // Update scrollbar state with new lenght of itemlist
        self.entry_table.entry_scroll_state = ScrollbarState::content_length(
            self.entry_table.entry_scroll_state,
            self.entry_table.entry_table_items.len(),
        );
        self.filter_tags_by_entries();
        self.toggle_area();
        self.entry_table.entry_table_state.select(Some(0));
        self.former_area = Some(FormerArea::TagArea);
    }
}