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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
|
// 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::app::expand_home;
use crate::bibiman::entries::EntryTableColumn;
use crate::bibiman::{bibisetup::*, search::BibiSearch};
use crate::cliargs::CLIArgs;
use crate::config::BibiConfig;
use crate::tui::popup::{PopupArea, PopupKind};
use crate::tui::Tui;
use crate::{app, cliargs};
use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList};
use arboard::Clipboard;
use color_eyre::eyre::{Error, Result};
use crossterm::event::KeyCode;
use editor_command::EditorBuilder;
use ratatui::widgets::ScrollbarState;
use regex::Regex;
use std::ffi::OsStr;
use std::fs::{self, read_to_string};
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use std::result::Result::Ok;
use tui_input::Input;
pub mod bibisetup;
pub mod entries;
pub mod keywords;
pub mod search;
// Areas in which actions are possible
#[derive(Debug)]
pub enum CurrentArea {
EntryArea,
TagArea,
SearchArea,
PopupArea,
}
// Check which area was active when popup set active
#[derive(Debug)]
pub enum FormerArea {
EntryArea,
TagArea,
SearchArea,
}
// Application.
#[derive(Debug)]
pub struct Bibiman {
// main bib file
pub main_bibfiles: Vec<PathBuf>,
// main bibliography
pub main_biblio: BibiSetup,
// search struct:
pub search_struct: BibiSearch,
// 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>,
// active popup
pub popup_area: PopupArea,
}
impl Bibiman {
// Constructs a new instance of [`App`].
pub fn new(args: &mut CLIArgs, cfg: &mut BibiConfig) -> Result<Self> {
let mut main_bibfiles: Vec<PathBuf> = args.pos_args.clone();
if cfg.general.bibfiles.is_some() {
main_bibfiles.append(cfg.general.bibfiles.as_mut().unwrap())
};
let main_bibfiles = cliargs::parse_files(main_bibfiles);
let main_biblio = BibiSetup::new(&main_bibfiles, cfg);
let tag_list = TagList::new(main_biblio.keyword_list.clone());
let search_struct = BibiSearch::default();
let entry_table = EntryTable::new(main_biblio.entry_list.clone());
let current_area = CurrentArea::EntryArea;
Ok(Self {
main_bibfiles,
main_biblio,
tag_list,
search_struct,
entry_table,
scroll_info: 0,
current_area,
former_area: None,
popup_area: PopupArea::default(),
})
}
pub fn show_help(&mut self) {
if let CurrentArea::EntryArea = self.current_area {
self.former_area = Some(FormerArea::EntryArea);
} else if let CurrentArea::TagArea = self.current_area {
self.former_area = Some(FormerArea::TagArea);
}
self.popup_area.is_popup = true;
self.current_area = CurrentArea::PopupArea;
self.popup_area.popup_kind = Some(PopupKind::Help);
}
/// Close all current popups and select former tab of main app
pub fn close_popup(&mut self) {
// Reset all popup fields to default values
self.popup_area = PopupArea::default();
// Go back to previously selected area
if let Some(FormerArea::EntryArea) = self.former_area {
self.current_area = CurrentArea::EntryArea
} else if let Some(FormerArea::TagArea) = self.former_area {
self.current_area = CurrentArea::TagArea
}
// Clear former_area field
self.former_area = None;
}
/// Open a popup
///
/// Necessary arguments are:
///
/// - `popup_kind`: a valid value of the `PopupKind` `enum`. This determines the
/// further behaviour of the popup.
/// - `message`: A message shown in the popup. This is needed for the `PopupKind`
/// values `MessageConfirm` and `MessageError`. If not needed, set it to `None`.
/// - `object`: An object passed as `&str` which might explain the current popup
/// action. Its not needed, but very useful. Can be used with the `PopupKind`
/// values `MessageConfirm`, `MessageError` and `YankItem`. If not needed, set it
/// to `None`.
/// - `items`: A vector of items which are needed if a selectable list is rendered.
/// The vector consists of tuples including a pair of `String`. The second item of
/// the tuple is considered kind of an object which can be used e.g. to open
/// the given filepath etc. If not needed, set it to `None`.
///
/// The function will panic if a needed argument for the particular `PopupKind`
/// is missing
pub fn open_popup(
&mut self,
popup_kind: PopupKind,
message: Option<&str>,
object: Option<&str>,
items: Option<Vec<(String, String)>>,
) -> Result<()> {
if let CurrentArea::EntryArea = self.current_area {
self.former_area = Some(FormerArea::EntryArea);
} else if let CurrentArea::TagArea = self.current_area {
self.former_area = Some(FormerArea::TagArea);
}
self.popup_area.is_popup = true;
self.current_area = CurrentArea::PopupArea;
match popup_kind {
PopupKind::Help => {
self.popup_area.popup_kind = Some(PopupKind::Help);
Ok(())
}
PopupKind::MessageConfirm => {
self.popup_area.popup_kind = Some(PopupKind::MessageConfirm);
if object.is_some() && message.is_some() {
self.popup_area.popup_message = message.unwrap().to_owned() + object.unwrap();
Ok(())
} else if object.is_none() && message.is_some() {
self.popup_area.popup_message = message.unwrap().to_owned();
Ok(())
} else {
Err(Error::msg("You need to past at least a message via Some(&str) to create a message popup"))
}
}
PopupKind::MessageError => {
self.popup_area.popup_kind = Some(PopupKind::MessageError);
if object.is_some() && message.is_some() {
self.popup_area.popup_message = message.unwrap().to_owned() + object.unwrap();
Ok(())
} else if object.is_none() && message.is_some() {
self.popup_area.popup_message = message.unwrap().to_owned();
Ok(())
} else {
Err(Error::msg("You need to past at least a message via Some(&str) to create a message popup"))
}
}
PopupKind::OpenRes => {
if items.is_some() {
self.popup_area.popup_kind = Some(PopupKind::OpenRes);
self.popup_area.popup_selection(items.unwrap());
self.popup_area.popup_state.select(Some(0));
Ok(())
} else {
Err(Error::msg(
"No Vec<(String, String)> passed as argument to generate the items list",
))
}
}
PopupKind::AppendToFile => {
if items.is_some() {
self.popup_area.popup_kind = Some(PopupKind::AppendToFile);
Ok(())
} else {
Err(Error::msg(
"No Vec<(String, String)> passed as argument to generate the items list",
))
}
}
PopupKind::AddEntry => {
self.popup_area.popup_kind = Some(PopupKind::AddEntry);
Ok(())
}
PopupKind::YankItem => {
if items.is_some() {
self.popup_area.popup_kind = Some(PopupKind::YankItem);
self.popup_area.popup_selection(items.unwrap());
self.popup_area.popup_state.select(Some(0));
Ok(())
} else {
Err(Error::msg(
"No Vec<(String, String)> passed as argument to generate the items list",
))
}
}
}
}
pub fn update_lists(&mut self, cfg: &BibiConfig) {
self.main_biblio = BibiSetup::new(&self.main_bibfiles, cfg);
self.tag_list = TagList::new(self.main_biblio.keyword_list.clone());
self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone());
}
/// Toggle moveable list between entries and tags
pub fn toggle_area(&mut self) {
if let CurrentArea::EntryArea = self.current_area {
self.entry_table.entry_scroll_state = self.entry_table.entry_scroll_state.position(0);
self.current_area = CurrentArea::TagArea;
self.tag_list.tag_list_state.select(Some(0));
self.tag_list.tag_scroll_state = self
.tag_list
.tag_scroll_state
.position(self.tag_list.tag_list_state.selected().unwrap());
} else if let CurrentArea::TagArea = self.current_area {
self.current_area = CurrentArea::EntryArea;
self.tag_list.tag_list_state.select(None);
self.entry_table.entry_scroll_state = self
.entry_table
.entry_scroll_state
.position(self.entry_table.entry_table_state.selected().unwrap());
}
}
pub fn reset_current_list(&mut self) {
self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone());
self.tag_list = TagList::new(self.main_biblio.keyword_list.clone());
if let CurrentArea::TagArea = self.current_area {
self.tag_list.tag_list_state.select(Some(0))
}
self.entry_table.entry_table_at_search_start.clear();
self.search_struct.filtered_tag_list.clear();
self.search_struct.inner_search = false;
self.former_area = None
}
/// 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();
}
pub fn scroll_info_down(&mut self) {
self.entry_table.entry_info_scroll = self.entry_table.entry_info_scroll.saturating_add(1);
self.entry_table.entry_info_scroll_state = self
.entry_table
.entry_info_scroll_state
.position(self.entry_table.entry_info_scroll.into());
}
pub fn scroll_info_up(&mut self) {
self.entry_table.entry_info_scroll = self.entry_table.entry_info_scroll.saturating_sub(1);
self.entry_table.entry_info_scroll_state = self
.entry_table
.entry_info_scroll_state
.position(self.entry_table.entry_info_scroll.into());
}
}
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(); // Does not work properly after upgrading to ratatui 0.29.0
self.entry_table
.entry_table_state
.select(Some(self.entry_table.entry_table_items.len() - 1));
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) {
if self
.entry_table
.entry_table_state
.selected_column()
.unwrap()
== 4
{
self.entry_table.entry_table_state.select_column(Some(1));
} else {
self.entry_table.entry_table_state.select_next_column();
}
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) {
if self
.entry_table
.entry_table_state
.selected_column()
.unwrap()
== 1
{
self.entry_table.entry_table_state.select_last_column();
} else {
self.entry_table.entry_table_state.select_previous_column();
}
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;
}
}
}
pub fn select_entry_by_citekey(&mut self, citekey: &str) {
// Search for entry by matching citekeys
let mut idx_count = 0;
loop {
if idx_count == self.entry_table.entry_table_items.len() {
idx_count = 0;
break;
} else if self.entry_table.entry_table_items[idx_count]
.citekey
.contains(citekey)
{
break;
}
idx_count += 1
}
// Set selected entry to vec-index of match
self.entry_table.entry_table_state.select(Some(idx_count));
}
pub fn run_editor(&mut self, cfg: &BibiConfig, tui: &mut Tui) -> Result<()> {
// get filecontent and citekey for calculating line number
let citekey: &str = &self.entry_table.entry_table_items
[self.entry_table.entry_table_state.selected().unwrap()]
.citekey
.clone();
// Add comma as suffix that only
// main citekeys are matched, not other fields like crossref
let citekey_pattern: String = format!("{},", citekey);
// Check if multiple files were passed to bibiman and
// return the correct file path
let filepath = if self.main_bibfiles.len() == 1 {
self.main_bibfiles.first().unwrap().as_os_str()
} else {
let mut idx = 0;
for f in &self.main_bibfiles {
if search::search_pattern_in_file(&citekey_pattern, &f).is_some() {
break;
}
idx += 1;
}
self.main_bibfiles[idx].as_os_str()
};
let filecontent = fs::read_to_string(&filepath).unwrap();
// Search the line number to place the cursor at
let mut line_count = 0;
for line in filecontent.lines() {
line_count += 1;
// if reaching the citekey break the loop
// if reaching end of lines without match, reset to 0
if line.contains(&citekey_pattern) {
break;
} else if line_count == filecontent.len() {
eprintln!(
"Citekey {} not found, opening file {} at line 1",
citekey,
filepath.to_string_lossy()
);
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()
.source(cfg.general.editor.as_ref())
.environment()
.source(Some("vi"))
.build()
.unwrap();
// Prepare arguments to open file at specific line
let status = cmd.arg(format!("+{}", line_count)).arg(filepath).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(self, cfg);
// Select entry which was selected before entering editor
self.select_entry_by_citekey(citekey);
Ok(())
}
pub fn open_connected_note(
&mut self,
cfg: &BibiConfig,
tui: &mut Tui,
file: &OsStr,
) -> Result<()> {
// get filecontent and citekey for calculating line number
let citekey: &str = &self.entry_table.entry_table_items
[self.entry_table.entry_table_state.selected().unwrap()]
.citekey
.clone();
// Exit TUI to enter editor
tui.exit()?;
// Use VISUAL or EDITOR. Set "vi" as last fallback
let mut cmd: Command = EditorBuilder::new()
.source(cfg.general.editor.as_ref())
.environment()
.source(Some("vi"))
.build()
.unwrap();
// Prepare arguments to open file at specific line
let status = cmd.arg(file).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(self, cfg);
// Select entry which was selected before entering editor
self.select_entry_by_citekey(citekey);
Ok(())
}
pub fn add_entry(&mut self) {
if let CurrentArea::EntryArea = self.current_area {
self.former_area = Some(FormerArea::EntryArea);
}
self.popup_area.is_popup = true;
self.current_area = CurrentArea::PopupArea;
self.popup_area.popup_kind = Some(PopupKind::AddEntry);
}
///Try to resolve entered DOI. If successfull, choose file where to append
///the new entry via `append_to_file()` function. If not, show error popup
///
///The method needs two arguments: the CLIArgs struct and the `str` containing the DOI
pub fn handle_new_entry_submission(&mut self, doi_string: &str) -> Result<()> {
let doi_string = if doi_string.starts_with("10.") {
"https://doi.org/".to_string() + doi_string
} else {
doi_string.to_owned()
};
// Send GET request to doi resolver
let doi_entry = ureq::get(&doi_string)
.set("Accept", "application/x-bibtex")
.call();
if let Ok(entry) = doi_entry {
// Save generated bibtex entry in structs field
let entry = entry
.into_string()
.expect("Couldn't parse fetched entry into string");
self.popup_area.popup_sel_item = entry;
self.popup_area.popup_kind = Some(PopupKind::AppendToFile);
self.append_to_file();
self.former_area = Some(FormerArea::EntryArea);
self.current_area = CurrentArea::PopupArea;
self.popup_area.popup_state.select(Some(0))
} else {
self.open_popup(
PopupKind::MessageError,
Some("Can't find DOI: "),
Some(&doi_string),
None,
)?;
// self.popup_area
// .popup_message("Can't find DOI: ", &doi_string, false);
}
Ok(())
}
pub fn append_to_file(&mut self) {
let mut items = vec![("Create new file".to_owned(), "".to_string())];
if self.main_bibfiles.len() > 1 {
for f in self.main_bibfiles.clone() {
items.push(("File: ".into(), f.to_str().unwrap().to_owned()));
}
} else {
items.push((
"File: ".into(),
self.main_bibfiles
.first()
.unwrap()
.to_str()
.unwrap()
.to_owned(),
));
}
self.popup_area.popup_selection(items);
}
pub fn append_entry_to_file(&mut self, cfg: &BibiConfig) -> Result<()> {
// Index of selected popup field
let popup_idx = self.popup_area.popup_state.selected().unwrap();
// regex pattern to match citekey in fetched bibtexstring
let pattern = Regex::new(r"\{([^\{\},]*),").unwrap();
let citekey = pattern
.captures(&self.popup_area.popup_sel_item)
.unwrap()
.get(1)
.unwrap()
.as_str()
.to_string();
// Check if new file or existing file was choosen
let mut file = if self.popup_area.popup_list[popup_idx]
.0
.contains("Create new file")
{
let citekey = PathBuf::from(&citekey);
// Get path of current files
let path: PathBuf = if self.main_bibfiles[0].is_file() {
self.main_bibfiles[0].parent().unwrap().to_owned()
} else {
dirs::home_dir().unwrap() // home dir as fallback
};
let citekey = citekey.with_extension("bib");
let newfile = path.join(citekey);
self.main_bibfiles.push(newfile.clone());
File::create_new(newfile).unwrap()
} else {
let file_path = &self.main_bibfiles[popup_idx - 1];
// Check if similar citekey already exists
let file_string = read_to_string(&file_path).unwrap();
// If choosen file contains entry with fetched citekey, append an
// char to the citekey so no dublettes are created
if file_string.contains(&citekey) {
let mut new_citekey = String::new();
// Loop over ASCII alpabetic chars and check again if citekey with
// appended char exists. If yes, move to next char and test again.
// If the citekey is free, use it and break the loop
for c in b'a'..=b'z' {
let append_char = (c as char).to_string();
new_citekey = citekey.clone() + &append_char;
if !file_string.contains(&new_citekey) {
break;
}
}
let new_entry_string_clone = self.popup_area.popup_sel_item.clone();
// Replace the double citekey with newly created
self.popup_area.popup_sel_item = pattern
.replace(&new_entry_string_clone, format!("{{{},", &new_citekey))
.to_string();
}
OpenOptions::new().append(true).open(file_path).unwrap()
};
// Optionally, add a newline before the content
file.write_all(b"\n")?;
// Write content to file
file.write_all(self.popup_area.popup_sel_item.as_bytes())?;
// Update the database and the lists to reflect the new content
self.update_lists(cfg);
self.close_popup();
// Select newly created entry
self.select_entry_by_citekey(&citekey);
Ok(())
}
pub fn open_connected_res(&mut self, cfg: &BibiConfig, tui: &mut Tui) -> Result<()> {
// Index of selected entry
let entry_idx = self.entry_table.entry_table_state.selected().unwrap();
// Index of selected popup field
let popup_idx = self.popup_area.popup_state.selected().unwrap();
let popup_entry = self.popup_area.popup_list[popup_idx].1.clone();
// Choose ressource depending an selected popup field
if self.popup_area.popup_list[popup_idx].0.contains("Weblink") {
let object = self.entry_table.entry_table_items[entry_idx].doi_url();
let url = app::prepare_weblink(object);
app::open_connected_link(cfg, &url)?;
self.close_popup();
} else if self.popup_area.popup_list[popup_idx].0.contains("File") {
// TODO: Selection for multiple files
// let object = self.entry_table.entry_table_items[entry_idx].filepath()[0];
let file = expand_home(&PathBuf::from(popup_entry.clone()));
// let object: OsString = popup_entry.into();
if file.is_file() {
app::open_connected_file(cfg, &file.into_os_string())?;
self.close_popup();
} else {
self.open_popup(
PopupKind::MessageError,
Some("No valid file path: "),
Some(file.to_str().unwrap()),
None,
)?;
}
} else if self.popup_area.popup_list[popup_idx].0.contains("Note") {
let file = expand_home(&PathBuf::from(popup_entry.clone()));
// let object: OsString = popup_entry.into();
if file.is_file() {
self.open_connected_note(cfg, tui, &file.into_os_string())?;
self.close_popup();
} else {
self.open_popup(
PopupKind::MessageError,
Some("No valid file path: "),
Some(file.to_str().unwrap()),
None,
)?;
}
} else {
eprintln!("Unable to find ressource to open");
};
// run command to open file/Url
Ok(())
}
pub fn yank_entry_field(&mut self) -> Result<()> {
// Index of selected popup field
let popup_idx = self.popup_area.popup_state.selected().unwrap();
let popup_entry = self.popup_area.popup_list[popup_idx].1.clone();
let kind = self.popup_area.popup_list[popup_idx]
.0
.to_lowercase()
.split(":")
.next()
.unwrap()
.to_owned();
let msg = format!("Yanked {} to clipboard: ", &kind);
Bibiman::yank_text(&popup_entry);
self.open_popup(
PopupKind::MessageConfirm,
Some(&msg),
Some(&popup_entry),
None,
)?;
Ok(())
}
/// Fast opening/yanking of file/link or citekey through simple keypress in
/// the particular popup mode:
///
/// **Opening popup**
///
/// `o` -> opens the first file of the `filepath` `Vec` for the current entry
/// `l` -> opens the link of the current entry
/// `n` -> opens the first note
///
/// **Yanking popup**
///
/// `y` -> yanks the citekey for the current entry
pub fn fast_selection(
&mut self,
cfg: &BibiConfig,
tui: &mut Tui,
key_code: KeyCode,
) -> Result<()> {
if let CurrentArea::PopupArea = self.current_area {
let entry_idx = self.entry_table.entry_table_state.selected().unwrap();
match self.popup_area.popup_kind {
Some(PopupKind::OpenRes) => match key_code {
KeyCode::Char('o') => {
let file = self.entry_table.entry_table_items[entry_idx]
.filepath
.clone();
if file.is_some() {
let file = expand_home(&PathBuf::from(file.unwrap()[0].clone()));
// let object: OsString = popup_entry.into();
if file.is_file() {
app::open_connected_file(cfg, &file.into_os_string())?;
self.close_popup();
} else {
self.open_popup(
PopupKind::MessageError,
Some("No valid file path: "),
Some(file.to_str().unwrap()),
None,
)?;
}
}
}
KeyCode::Char('n') => {
let file = self.entry_table.entry_table_items[entry_idx].notes.clone();
if file.is_some() {
let file = expand_home(&PathBuf::from(file.unwrap()[0].clone()));
// let object: OsString = popup_entry.into();
if file.is_file() {
self.open_connected_note(cfg, tui, &file.into_os_string())?;
self.close_popup();
} else {
self.open_popup(
PopupKind::MessageError,
Some("No valid file path: "),
Some(file.to_str().unwrap()),
None,
)?;
}
}
}
KeyCode::Char('l') => {
if self.entry_table.entry_table_items[entry_idx]
.doi_url
.is_some()
{
let object = self.entry_table.entry_table_items[entry_idx].doi_url();
let url = app::prepare_weblink(object);
app::open_connected_link(cfg, &url)?;
self.close_popup();
}
}
_ => {}
},
Some(PopupKind::YankItem) => match key_code {
KeyCode::Char('y') => {
let key = self.entry_table.entry_table_items[entry_idx]
.citekey
.clone();
Bibiman::yank_text(&key);
self.open_popup(
PopupKind::MessageConfirm,
Some("Yanked citekey to clipboard: "),
Some(&key),
None,
)?;
}
_ => {}
},
_ => {}
}
}
Ok(())
}
/// Formats a raw BibTeX entry string for better readability.
pub fn format_bibtex_entry(entry: &str, file_path: &str) -> String {
let mut formatted = String::new();
// Find the position of the first '{'
if let Some(start_brace_pos) = entry.find('{') {
// Extract the preamble (e.g., '@article{')
let preamble = &entry[..start_brace_pos + 1];
let preamble = preamble.trim_start();
formatted.push_str(preamble);
// formatted.push('\n'); // Add newline
// Get the content inside the braces
let rest = &entry[start_brace_pos + 1..];
// Remove the last '}' at the end, if present
let rest = rest.trim_end();
let rest = if rest.ends_with('}') {
&rest[..rest.len() - 1]
} else {
rest
};
// Parse the fields, considering braces and quotes
let mut fields = Vec::new();
let mut current_field = String::new();
let mut brace_level = 0;
let mut in_quotes = false;
for c in rest.chars() {
match c {
'{' if !in_quotes => {
brace_level += 1;
current_field.push(c);
}
'}' if !in_quotes => {
brace_level -= 1;
current_field.push(c);
}
'"' => {
in_quotes = !in_quotes;
current_field.push(c);
}
',' if brace_level == 0 && !in_quotes => {
// Outside of braces and quotes, comma separates fields
fields.push(current_field.trim().to_string());
current_field.clear();
}
_ => {
current_field.push(c);
}
}
}
// Add the last field
if !current_field.trim().is_empty() {
fields.push(current_field.trim().to_string());
}
// **Conditionally Clean the Citation Key**
if let Some(citation_key) = fields.get_mut(0) {
// Check if the citation key contains any non-alphanumerical characters except underscores
let needs_cleaning = citation_key
.chars()
.any(|c| !c.is_alphanumeric() && c != '_');
if needs_cleaning {
// Retain only alphanumerical characters and underscores
let cleaned_key: String = citation_key
.chars()
.filter(|c| c.is_alphanumeric() || *c == '_')
.collect();
// If the cleaned key is longer than 14 characters, retain only the last 14
let limited_key = if cleaned_key.len() > 14 {
cleaned_key
.chars()
.rev()
.take(14)
.collect::<String>()
.chars()
.rev()
.collect()
} else {
cleaned_key
};
// Replace the original citation key with the cleaned and possibly limited key
*citation_key = limited_key;
}
}
// Add the new 'file' field
let file_field = format!("file = {{{}}}", file_path);
fields.push(file_field);
// Reconstruct the entry with proper indentation
for (i, field) in fields.iter().enumerate() {
formatted.push_str(" ");
formatted.push_str(field);
// Add a comma if it's not the last field
if i < fields.len() - 1 {
formatted.push(',');
}
formatted.push('\n');
}
formatted.push('}'); // Close the entry
formatted
} else {
// No opening brace found, return the entry as is
entry.to_string()
}
}
// 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(&self.search_struct.search_string, orig_list.clone());
self.entry_table.entry_table_items = filtered_list;
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(),
);
}
}
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(); // Doesn't work properly after upgrade to ratatui v.0.29
self.tag_list
.tag_list_state
.select(Some(self.tag_list.tag_list_items.len() - 1));
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();
&self.tag_list.tag_list_items[idx]
}
pub fn search_tags(&mut self) {
let orig_list = &self.tag_list.tag_list_at_search_start;
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_key(|a| a.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);
}
}
impl Bibiman {
// Search Area
// Enter the search area
pub fn enter_search_area(&mut self) {
if let CurrentArea::EntryArea = self.current_area {
if let Some(FormerArea::TagArea) = self.former_area {
self.search_struct.inner_search = true
}
self.entry_table.entry_table_at_search_start =
self.entry_table.entry_table_items.clone();
self.former_area = Some(FormerArea::EntryArea)
} else if let CurrentArea::TagArea = self.current_area {
self.tag_list.tag_list_at_search_start = self.tag_list.tag_list_items.clone();
self.former_area = Some(FormerArea::TagArea)
}
self.current_area = CurrentArea::SearchArea
}
// Confirm search: Search former list by pattern
pub fn confirm_search(&mut self) {
if let Some(FormerArea::EntryArea) = self.former_area {
self.current_area = CurrentArea::EntryArea;
self.entry_table.entry_table_state.select(Some(0));
self.entry_table.entry_table_at_search_start.clear();
} else if let Some(FormerArea::TagArea) = self.former_area {
self.current_area = CurrentArea::TagArea;
self.tag_list.tag_list_state.select(Some(0));
self.tag_list.tag_list_at_search_start.clear();
}
self.former_area = Some(FormerArea::SearchArea);
self.search_struct.search_string.clear();
}
// Break search: leave search area without filtering list
pub fn break_search(&mut self) {
if let Some(FormerArea::EntryArea) = self.former_area {
self.current_area = CurrentArea::EntryArea;
self.entry_table.entry_table_state.select(Some(0));
self.entry_table.entry_table_at_search_start.clear();
} else if let Some(FormerArea::TagArea) = self.former_area {
self.current_area = CurrentArea::TagArea;
self.tag_list.tag_list_state.select(Some(0));
self.tag_list.tag_list_at_search_start.clear();
}
// But keep filtering by tag if applied before entering search area
if !self.search_struct.inner_search {
self.reset_current_list();
}
self.former_area = None;
// If search is canceled, reset default status of struct
self.search_struct.search_string.clear();
}
// Remove last char from search pattern and filter list immidiately
pub fn search_pattern_pop(&mut self) {
self.search_struct.search_string.pop();
if let Some(FormerArea::EntryArea) = self.former_area {
self.search_entries();
self.filter_tags_by_entries();
} else if let Some(FormerArea::TagArea) = self.former_area {
self.search_tags();
}
}
// Add current char to search pattern and filter list immidiatley
pub fn search_pattern_push(&mut self, search_pattern: char) {
self.search_struct.search_string.push(search_pattern);
if let Some(FormerArea::EntryArea) = self.former_area {
self.search_entries();
self.filter_tags_by_entries();
} else if let Some(FormerArea::TagArea) = self.former_area {
self.search_tags();
}
}
pub fn search_list_by_pattern(&mut self, searchpattern: &Input) {
self.search_struct.search_string = searchpattern.value().to_string();
if let Some(FormerArea::EntryArea) = self.former_area {
self.search_entries();
self.filter_tags_by_entries();
} else if let Some(FormerArea::TagArea) = self.former_area {
self.search_tags();
}
}
}
#[cfg(test)]
mod tests {
use regex::Captures;
use super::*;
#[test]
fn citekey_pattern() {
let citekey = format!("{{{},", "a_key_2001");
assert_eq!(citekey, "{a_key_2001,")
}
#[test]
fn regex_capture_citekey() {
let re = Regex::new(r"\{([^\{\},]*),").unwrap();
let bibstring = String::from("@article{citekey77_2001:!?, author = {Hanks, Tom}, title = {A great book}, year = {2001}}");
let citekey = re.captures(&bibstring).unwrap().get(1).unwrap().as_str();
assert_eq!(citekey, "citekey77_2001:!?");
if bibstring.contains(&citekey) {
let append_char = "a";
let new_entry_string_clone = bibstring.clone();
let updated_bibstring = re
.replace(&new_entry_string_clone, |caps: &Captures| {
format!("{{{}{},", &caps[1], &append_char)
})
.to_string();
assert_eq!(updated_bibstring, "@article{citekey77_2001:!?a, author = {Hanks, Tom}, title = {A great book}, year = {2001}}")
}
}
}
|