hlg 1.0.1

HLG, or HI, Let's Go!, is a powerful bookmark manager to keep track of your favorites both on the filesystem and the web.
Documentation
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
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
use crate::bookmark::Handler;
use crate::bookmark::HandlerClass;
use crate::bookmark::Location;
use crate::bookmark::LocationType;
use crate::environment::LinkVariable;
use bookmark::Bookmark;
use clap::{ArgGroup, Parser};
use edit::edit;
use environment::{Category, Configurations};
use prompted::input;
use std::{collections::HashSet, fs};

mod bookmark;
mod data_provider;
mod environment;

#[derive(Parser)]
#[command(version, about)]
#[clap(group(ArgGroup::new("info").required(false).args(&["keyglobal", "keycategory", "name", "edit", "summary", "config", "delete", "rename"])))]
struct Cli {
    /// The bookmark name to launch
    name: Option<Vec<String>>,

    /// The category to use for this session
    #[arg(short, long, group = "cats")]
    category: Option<String>,

    ///  Get a quick summary of hlg special bookmark
    #[arg(short = '0', long = "summary", default_value_t = false)]
    summary: bool,

    /// Edit the bookmark file
    #[arg(short, long = "edit-bookmarks", default_value_t = false)]
    edit: bool,

    /// Edit the configuration file
    #[arg(short = 'E', long = "edit-configuration", default_value_t = false)]
    config: bool,

    /// List global shortcut keys
    #[arg(short = 'K', long = "list-global-keys", default_value_t = false)]
    keyglobal: bool,

    /// List category keys
    #[arg(short, long = "list-category-keys", default_value_t = false)]
    keycategory: bool,

    /// Add new category
    #[arg(short, long = "new-category", group = "cats", value_name = "CATEGORY")]
    new: Option<String>,

    /// Delete category from database
    #[arg(
        short,
        long = "delete-category",
        group = "cats",
        value_name = "CATEGORY"
    )]
    delete: Option<String>,

    /// Rename category
    #[arg(
        short,
        long = "rename-category",
        value_delimiter = ' ',
        num_args = 2,
        group = "cats",
        value_names = ["OLD_NAME", "NEW_NAME"]
    )]
    rename: Option<Vec<String>>,
}

#[derive(Clone)]
enum PadType {
    ByKey(char),
    ByName(String),
    BySearch(SearchService),
    BySelector(Selector),
}

#[derive(Clone)]
struct CategoryManager {
    root: String,
    name: String,
    categories: Vec<String>,
}

impl CategoryManager {
    fn new() -> Self {
        let name = Configurations::get_setting()
            .default_category
            .unwrap_or(Configurations::default_settings().default_category.unwrap());
        let root = Configurations::category_home();
        let f_name = format!("{root}/{name}");
        if !Configurations::check_path(&f_name) {
            // The default category might have been manually deleted, so tell the user and quit:
            eprintln!(
                "Hi! It seems the set default-category '{name}' in your configuration file is missing."
            );
            eprintln!(
                "Please correct this by either changing the default-category to some other available category or re-create the '{name}' category and run this program again."
            );
            eprintln!(
                "This message does not appear for a new installation, which means you might have deleted the default bookmark file and forgot to change the default category in the process."
            );
            eprintln!(
                "It also means yu do not have 'create-missing-category' set to true, which prevents hlg from recreating the default bookmark file."
            );
            std::process::exit(22);
        }
        let categories = Self::get_category_list();
        Self {
            name,
            categories,
            root,
        }
    }

    //Set category to global:
    fn set_global(&self) -> Category {
        Category::new(Scope::Global)
    }

    // Set the category from the name of the passed in argument:
    pub fn by_name<T: AsRef<str> + ToString>(mut self, category_name: T) -> Self {
        self.name = category_name.to_string();
        self
    }

    fn refresh_category_headers() {
        let checkfile = format!("{}/categories", Configurations::get_records());
        if Configurations::check_path(&checkfile) {
            fs::remove_file(checkfile).expect("Could not remove the old category file");
        }
        Self::create_category_file();
    }

    fn create_new(&self, category_name: String) -> std::io::Result<()> {
        if self.categories.contains(&category_name) {
            eprintln!(
                "Error: {category_name} category already exists in your bookmarks database. Choose another name, or simply use the {category_name} category to perform any bookmark management you wished to perform with this new category."
            );
            std::process::exit(25);
        } else {
            let data = format!(
                r"# This is the category header for the {category_name} category.
* <@access-computing.com/>"
            );
            let newcategory = format!("{}/{category_name}", self.root);
            fs::write(newcategory, data.as_bytes())?;
            println!(
                "{category_name} was successfully created and added to database. Feel fre to add bookmarks to it, or even set it as your default category in the configuration file."
            );

            Self::refresh_category_headers();
        }
        Ok(())
    }

    fn safe_category(&self) -> String {
        let mut home = Configurations::get_homepage();
        home = if home.contains('.') {
            let pos = home.find('.').unwrap();
            let (dish, _) = home.split_at(pos);
            dish.to_string()
        } else {
            self.name.clone()
        };
        home
    }

    fn delete_category(&self, name: &String) -> std::io::Result<()> {
        let safe_category = self.safe_category();
        if *name == self.name || *name == safe_category {
            eprintln!(
                "Error: cannot delete default or home-linked category. This option can onnly be called with the name of the category to be deleted, other than the default or home-linked category."
            );
            std::process::exit(20);
        }
        if !self.categories.contains(name) {
            eprintln!("Error: No category '{name}' found in database.");
            std::process::exit(21);
        }
        let filename = format!("{}/{name}", self.root);
        println!(
            "are you sure that you would like to delete {name} from the bookmarks database? This action cannot be undone."
        );
        let confirm = input!("Enter y to confirm:");
        if confirm.trim().to_lowercase() == "y" {
            fs::remove_file(filename)?;
            println!(
                "{name} category deleted successfully. You will no longer be able to perform any operation that involves this category unless you recreate it"
            );
            Self::refresh_category_headers();
        } else {
            println!(
                "{name} category not deleted, and is still available for any category management operations. Bye!"
            );
        }

        Ok(())
    }

    fn rename_category(&self, old_name: &String, new_name: &String) -> std::io::Result<()> {
        // Do some validation checks first:
        if !self.categories.contains(&old_name) {
            eprintln!("Sorry, this name {old_name} not found in database.");
            eprintln!("You can simply create a new category with the name, '{new_name}' instead.");
            std::process::exit(21);
        }
        // Protect safe categories:
        let safe_category = self.safe_category();
        if *old_name == safe_category || *old_name == self.name {
            eprintln!(
                "Hi, what are you trying to do? You cannot rename a default category or a home-linked category to a new name while they are still in use."
            );
            eprintln!(
                "Consider changing the default category or a category linked by the main home bookmark in the configuration file before attempting this operation."
            );
            std::process::exit(20);
        }
        // Done with old_name checks, what about the proposed name?
        if self.categories.contains(&new_name) {
            eprintln!("Error: This category '{new_name}' is already in the bookmarks database.");
            eprintln!("Just use it instead.");
            std::process::exit(25);
        }
        // Okay at this point, let us rename:
        let old_file = format!("{}/{old_name}", self.root);
        let new_file = format!("{}/{new_name}", self.root);
        println!(
            "Are you sure that you would like to rename the category '{old_name}' to '{new_name}'?"
        );
        let confirm = input!("Enter Y to confirm:");
        if confirm.trim().to_lowercase() == "y" {
            fs::rename(old_file, new_file)?;
            Self::refresh_category_headers();
            println!("Success! The category {old_name} was renamed to {new_name}.");
            println!(
                "Remember to use this name in any category management activities rather than '{old_name}'."
            );
        } else {
            println!(
                "Okay, the rename operation was cancelled. Your category is still referred to as '{old_name}' and not '{new_name}'."
            );
        }
        Ok(())
    }

    fn check_name_availability(&self) {
        let create_missing_category = Configurations::get_setting()
            .create_missing_category
            .unwrap_or(
                Configurations::default_settings()
                    .create_missing_category
                    .unwrap(),
            );
        if !self.categories.contains(&self.name) && !create_missing_category {
            eprintln!("Error: the {} category could not be found.", self.name);
            eprintln!(
                "As your default setting is not to create missing categories on the fly, consider creating a file called {} with your bookmarks and place it in the 'bookmarks' directory.",
                self.name
            );
            eprintln!(
                "Otherwise, just change the create-missing-category option to 'yes' in your configuration file. Now exiting.."
            );
            std::process::exit(26);
        }
    }

    fn load_categories() -> Vec<(String, String)> {
        let mut loader = Vec::new();
        let category_file = format!("{}/categories", Configurations::get_records());
        if !Configurations::check_path(&category_file) {
            Self::create_category_file();
        }
        let list =
            fs::read_to_string(category_file).expect("Could not load category file for reading");
        Self::shortcut_keycheck(&list);
        for line in list.lines() {
            if line.contains('(') {
                let pos = line.find('(').unwrap();
                let (category_name, comment) = line.split_at(pos);
                loader.push((format!("{category_name}"), format!("{}", &comment[1..])));
            } else {
                let tokens: Vec<&str> = line.split_whitespace().collect();
                loader.push((
                    format!("{}", tokens.get(0).unwrap()),
                    format!("<NONE> {}", &tokens[1..].join(" ")),
                ));
            }
        }
        loader
    }

    fn create_category_file() {
        let filelist = Self::file_list();
        if filelist.len() > 0 {
            let mut raw_string = String::new();
            for file in filelist {
                let fname = format!("{}/bookmarks/{file}", Configurations::get_app_home());
                let header = Self::find_category_line(&fname);
                if header.is_some() {
                    raw_string = format!("{raw_string}{file}{}\n", header.unwrap());
                } else {
                    raw_string = format!("{raw_string}{file}\n");
                }
            }
            if !Configurations::check_path(&Configurations::get_records()) {
                fs::create_dir(Configurations::get_records())
                    .expect("Could not create the records directory");
            }
            let category_file = format!("{}/categories", Configurations::get_records());
            fs::write(category_file.as_str(), raw_string.as_bytes())
                .expect("Could not save category details");
        } else {
            eprintln!("Error: no categories were found in the database.");
            eprintln!("Please add one and try again.");
            std::process::exit(36);
        }
    }

    fn get_category_list() -> Vec<String> {
        let categories = Self::load_categories();
        let mut names = Vec::new();
        for name in categories {
            names.push(name.0.to_string());
        }
        names
    }

    // Find the category with this assigned key:
    fn get_name_by_key(key: char) -> Option<String> {
        let mut category_name = None;
        let category_list = Self::load_categories();
        for candidate in category_list {
            let name = candidate.0;
            let header = candidate.1;
            let words: Vec<&str> = header.split_whitespace().collect();
            if words[0] != "<NONE>" {
                let letter = words.get(0).unwrap();
                let psb = letter.chars().nth(0).unwrap();
                if psb == key {
                    category_name = Some(name.to_string())
                }
            }
        }
        category_name
    }

    fn file_list() -> Vec<String> {
        let working_dir = Configurations::category_home();
        let mut categories = Vec::new();
        if Configurations::check_path(&working_dir) {
            for entry in fs::read_dir(&working_dir)
                .unwrap()
                .into_iter()
                .filter_map(Result::ok)
            {
                let f_name = String::from(entry.file_name().to_string_lossy());
                categories.push(f_name);
            }
            categories.sort();
            categories.dedup();
        }
        categories
    }

    /// Is this category tagged?
    fn find_category_line(fs_name: &String) -> Option<String> {
        let lines = fs::read_to_string(fs_name).expect("Could not load category header line");
        let lines: Vec<&str> = lines.lines().collect();
        let lines = lines
            .into_iter()
            .filter(|line| !line.is_empty())
            .map(|ln| ln.to_string())
            .collect::<Vec<String>>();
        let header = lines.get(0).unwrap();
        if header.starts_with("#!") {
            let header = &header[2..];
            Self::validate_header(header, fs_name);
            let header_fields: Vec<&str> = header.split(';').collect();
            if header_fields.len() > 1 {
                let shortcut = &header_fields[1].trim();
                let comment = if header_fields.len() > 2 {
                    &header_fields[2].trim()
                } else {
                    ""
                };
                if shortcut.is_empty() {
                    Some(format!(" {comment}"))
                } else {
                    Some(format!("({shortcut}) {comment}"))
                }
            } else {
                None
            }
        } else {
            None
        }
    }

    fn shortcut_keycheck(rawlist: &String) {
        let category_list: Vec<&str> = rawlist.lines().collect();
        let mut keys = Vec::new();
        let mut names = Vec::new();
        for category in category_list {
            if category.contains('(') {
                let divider = category.find('(').unwrap();
                let (_, key) = category.split_at(divider);
                let key = key.chars().nth(1).unwrap();
                keys.push(key);
                names.push(category.to_string());
            }
        }
        for key in keys {
            let mut list = Vec::new();
            for name in &names {
                let pos = name.find('(').unwrap();
                let (category, sym) = name.split_at(pos);
                let sym = sym.chars().nth(1).unwrap();
                if sym == key {
                    list.push(category.to_string());
                }
            }
            if list.len() > 1 {
                eprintln!("Error: Category key shortcut duplication detected!");
                eprintln!(
                    "This shortcut, '{key}', is assigned to {} categories, namely {} and {}",
                    list.len(),
                    &list[..list.len() - 1].join(", "),
                    &list[list.len() - 1]
                );
                eprintln!(
                    "To fix this error, just change the shortcut keys of the other categories and use {key} on only one category."
                );
                std::process::exit(19);
            }
        }
    }

    fn validate_header(header: &str, fname: &String) {
        let fname = {
            let pos = fname.rfind('/').unwrap();
            let (_, name) = fname.split_at(pos);
            name.strip_prefix('/').unwrap()
        };
        let tokens: Vec<&str> = header.split(';').collect();
        if tokens.len() > 3 {
            eprintln!(
                "Error: A category header cannot have more than three fields: the bookmark field, the shortcut key field and the descripption field."
            );
            eprintln!(
                "Please remove extra fields in the category header of the {fname} category to fix this error."
            );
            eprintln!(
                "Fields are created by semicolons, so remove any extra semicolon characters after the description field."
            );
            std::process::exit(19);
        }
        if tokens.len() == 1 {
            // We only have the bookmark field:
            let home = tokens.get(0).unwrap().trim();
            Configurations::check_header_bookmark(home, fname);
        } else if tokens.len() == 2 {
            let home = tokens.get(0).unwrap().trim();
            let token = tokens.get(1).unwrap().trim();
            Configurations::check_header_bookmark(home, fname);
            if !token.is_empty() {
                Configurations::check_header_key(&token, fname);
            }
            if home.is_empty() && token.is_empty() {
                eprintln!("Error: you cannot have an empty category header.");
                eprintln!(
                    "This category header of the {fname} category has got two optional fields, but both of them are empty."
                );
                eprintln!(
                    "If you do not like a category header, you can just leave it out of the bookmark file altogether."
                );
                std::process::exit(19);
            }
        } else {
            let bookmark = tokens.get(0).unwrap().trim();
            let key = tokens.get(1).unwrap().trim();
            let comment = tokens.get(2).unwrap().trim();
            if bookmark.is_empty() && key.is_empty() && comment.is_empty() {
                eprintln!("Error: empty category header in the category {fname}!");
                eprintln!(
                    "You must not have an empty category header. If you do not like a category header altogether, just do not add a line that starts with '#!' in the bookmark file."
                );
                eprintln!(
                    "Once you add a category header, you have to add either a bookmark field, a shortcut key, a description or all of these fields, but not to leave them out altogether."
                );
                std::process::exit(19);
            }
            if !bookmark.is_empty() {
                Configurations::check_header_bookmark(bookmark, fname);
            }
            if !key.is_empty() {
                Configurations::check_header_key(key, fname);
            }
        }
    }

    fn validate_category(&self) {
        if self.name.contains(" ") {
            eprintln!(
                "Error: A Category name must not have embedded spaces in it. Use dashes or underscores to separate words"
            );
            std::process::exit(27);
        }
        if self.name.contains('.') {
            eprintln!("Error: Hlg only supports one level of category at this time.");
            eprintln!(
                "Remove all period characters and replace them with either dashes or underscores to separate words."
            );
            std::process::exit(27);
        }
    }

    fn preferred_category(self) -> Category {
        self.validate_category();
        self.check_name_availability();
        Category::new(Scope::Local(self.name.clone()))
    }
}

struct LaunchPad {
    category: Category,
    pad_type: PadType,
    bookmark: Bookmark,
}

impl LaunchPad {
    fn new(target: Target) -> Self {
        let pad_type = target.get_pad();
        let category = target.scope.apply();
        let bookmark = Bookmark::default();
        Self {
            pad_type,
            category,
            bookmark,
        }
    }

    fn get_starred(&mut self) {
        let mut starred_bookmarks = Vec::new();
        for chosen in self.category.entries() {
            if chosen.info.is_favorite {
                starred_bookmarks.push(chosen.clone());
            }
        }
        let category = if self.category.name == "<global>" {
            "the global scope".to_string()
        } else {
            format!("the {} category", self.category.name)
        };
        if starred_bookmarks.len() > 0 {
            let autoselect = Configurations::get_setting()
                .autoselect
                .unwrap_or(Configurations::default_settings().autoselect.unwrap());
            if starred_bookmarks.len() == 1 && autoselect {
                self.bookmark = starred_bookmarks.get(0).unwrap().clone();
            } else {
                println!("Starred bookmarks in {category}");
                println!("* * * * * * *");
                println!("[Menu position]\tBookmark name:\t\tDescription");
                starred_bookmarks.sort();
                starred_bookmarks.dedup();
                self.bookmark = Self::make_choice(&starred_bookmarks);
            }
        } else {
            eprintln!("Error: You have no starred bookmarks in {category}.");
            eprintln!(
                "To star a bookmark, just add a trailing asterisk or star symbol, '*' on the bookmark name."
            );
            std::process::exit(30);
        }
    }

    fn which_key(&self, key: char) -> Option<Bookmark> {
        let mut shortcut = None;
        for fast_one in self.category.entries() {
            if fast_one.info.key.is_some() {
                // Check to see if it corresponds to the one we got:
                if fast_one.info.get_key() == key {
                    shortcut = Some(fast_one.clone());
                    break;
                }
            }
        }
        shortcut
    }

    fn get_by_key(&mut self, key: char) {
        self.bookmark = if let Some(thing) = self.which_key(key) {
            thing
        } else {
            let scope = self.category.name.clone();
            let scope = if scope == "<global>" {
                format!("the global scope")
            } else {
                format!("the {scope} category")
            };
            eprintln!(
                "Error: this shortcut key '{key}' is not yet assigned  for any bookmark in {scope}."
            );
            eprintln!(
                "Please check the case of your shortcuts when invoking hlg, as the case used is the same you used during saving the appropriate bookmark."
            );
            std::process::exit(35);
        };
    }

    fn by_alias(&mut self, phrase: String) {
        let key = phrase.to_lowercase();
        let mut found = Vec::new();
        for candidate in &self.category.entries() {
            if candidate.info.tags.is_some() {
                for marble in candidate.info.get_tags() {
                    if marble.to_lowercase() == key {
                        found.push(candidate.clone());
                    }
                }
            }
        }
        if found.len() > 0 {
            let autoselect = Configurations::get_setting()
                .autoselect
                .unwrap_or(Configurations::default_settings().autoselect.unwrap());
            if found.len() == 1 && autoselect {
                self.bookmark = found.get(0).unwrap().clone();
            } else {
                let number = if found.len() == 1 {
                    "is only one"
                } else {
                    "are more than one"
                };
                println!("There {number} bookmark with the alias '{phrase}'");
                println!("Please select one from the list below:");
                println!("[Choice]\tBookmark\t\tDescription");
                found.sort();
                found.dedup();
                self.bookmark = Self::make_choice(&found);
            }
        } else {
            let cat = if self.category.name == "<global>" {
                "global scope".to_string()
            } else {
                format!("{} category", self.category.name)
            };
            eprintln!("Sorry, no bookmark with the alias  {phrase} was found in the {cat}.");
            eprintln!(
                "Just add an alias to a bookmark by enclosing its name inside square brackets on the line before the bookmark entry"
            );
            std::process::exit(34);
        }
    }

    fn select(&mut self, selector: Selector) {
        match selector {
            Selector::Starred => self.get_starred(),
            Selector::Aliases(alias) => self.by_alias(alias),
        }
    }

    fn web_search(&mut self, service: SearchService) {
        let key;
        let address: String;
        match service {
            SearchService::Engine(ref engine, ref search_phrase) => {
                address = format!("search?q={search_phrase}");
                key = engine;
            }
            SearchService::Wiki(ref site, ref topic) => {
                address = format!("{topic}");
                key = site;
            }
        }
        let binding = self.category.entries();
        let provisional_bookmark = if key.len() == 1 {
            self.which_key(key.chars().nth(0).unwrap())
        } else {
            binding.get(key.as_str()).cloned()
        };
        let bookmark = if provisional_bookmark.is_some() {
            let chosen = provisional_bookmark.unwrap();
            if !chosen.info.is_search && service.is_search() {
                eprintln!(
                    "Error: while this bookmark {chosen} is a valid entry in the database, it has no search flag set on it."
                );
                eprintln!(
                    "To set a search flag, just add a trailing question mark on {chosen} like this: '{chosen}?'"
                );
                std::process::exit(65);
            }
            if !chosen.info.is_wiki && service.is_wiki() {
                eprintln!("Error: this bookmark '{chosen}' has no wiki flag set on it.");
                eprintln!(
                    "If {chosen} points to a wiki site, flag it by adding a trailing plus sign like this: '{chosen}+'"
                );
                std::process::exit(65);
            }
            let location = chosen.location.clone();
            let path = LocationType::Link(format!("{}/{address}", location.get()));
            let location = Location { path, ..location };
            Bookmark { location, ..chosen }
        } else {
            eprintln!("Error: the {key} service bookmark could not be found in database.");
            eprintln!(
                "If you are sure that it exists, consider placing an appropriate search or wiki flag on {key} and try again."
            );
            std::process::exit(65);
        };
        self.bookmark = bookmark;
    }

    fn get_by_name(&mut self, name: String) {
        let start = name.chars().nth(0).unwrap();
        let end = name.chars().nth(name.len() - 1).unwrap();
        if !start.is_alphanumeric() || !end.is_alphanumeric() {
            eprintln!(
                "Error: there is an error with this name, '{name}' as it either starts or ends with a non-alphanumeric character."
            );
            eprintln!(
                "Symbols used to flag a bookmark name in the database do not form part of the bookmark name. So try to remove the invalid characters and try again."
            );
            std::process::exit(34);
        }
        let bookmark = if let Some(chawada) = self.category.entries().get(name.as_str()) {
            chawada.clone()
        } else if name == "CATEGORY_HOME" {
            self.category
                .entries()
                .get(
                    Configurations::default_settings()
                        .special_pages
                        .unwrap()
                        .home
                        .unwrap()
                        .as_str(),
                )
                .unwrap()
                .clone()
        } else {
            let ref_category = if self.category.name == "<global>" {
                "global scope".to_string()
            } else {
                format!("{} category", self.category.name)
            };
            // We have no bookmark by that name:
            eprintln!(
                "Error, {name} bookmark could not be found in the {ref_category}. Could it be that {name} is an alias,  or in another category, or is  meant to be used as a search phrase?"
            );
            eprintln!("If this is an alias, add a leading forward slash to {name}");
            eprintln!(
                "Otherwise, if you meant it to  be a search phrase, then you have to add a leading question mark for a search engine like this, '?{name}', or as a topic for a wiki site like this  '+{name}'"
            );
            eprintln!(
                "You can only leave out the leading symbol when you are passing more than one argument to hlg. A single argument you passed in is always taken for a bookmark name."
            );
            eprintln!("Bye!");
            std::process::exit(35);
        };
        self.bookmark = bookmark;
    }

    fn start(&mut self) {
        match &self.pad_type {
            PadType::ByKey(letter) => self.get_by_key(*letter),
            PadType::ByName(bkname) => self.get_by_name(bkname.clone()),
            PadType::BySelector(selector) => self.select(selector.clone()),
            PadType::BySearch(service) => self.web_search(service.clone()),
        }
        self.run();
    }

    fn link_resolver(&self) -> Bookmark {
        let info = self.bookmark.info.clone();
        let link = LinkVariable::new(
            self.bookmark.location.get(),
            self.category.clone(),
            self.bookmark.info.name.clone(),
        )
        .expand();
        let path = LocationType::new(link);
        let handler = if self.bookmark.location.handler.name.contains(":hapana:") {
            Handler::new().name_from_protocol(HandlerClass::new(&path))
        } else {
            self.bookmark.location.handler.clone()
        };
        Bookmark::new(
            info,
            Location {
                path,
                handler,
                has_variable: false,
            },
        )
    }

    fn run(&self) {
        // If this method runs, we are nearly done:
        let bookmark = self.bookmark.clone();
        let bookmark = if bookmark.location.has_variable {
            self.link_resolver()
        } else {
            bookmark
        };
        if bookmark.info.is_deactivated {
            eprintln!(
                "Unfortunately, the {} bookmark is deactivated so cannot be launched. Please reactivate it by removing the trailing dash after its name and try again. Now exiting.",
                self.bookmark
            );
            std::process::exit(36);
        } else {
            Configurations::record(format!("{}.{}", self.category.name, bookmark));
            bookmark.launch();
        }
    }
    fn make_choice(found: &Vec<Bookmark>) -> Bookmark {
        for (number, bookmark) in found.iter().enumerate() {
            println!(
                "[{}]\t{}\t\t{}",
                number + 1,
                bookmark,
                if bookmark.info.comment.is_some() {
                    bookmark.info.get_comment()
                } else {
                    String::from("(No description)")
                }
            );
        }
        let number = input!("Your choice:");
        let number: usize = number
            .trim()
            .parse()
            .unwrap_or_else(|_| found.len() - found.len());
        let number = if number < 1 {
            println!("{number} below the minimum entry. Changing it to 1");
            1
        } else if number > found.len() {
            println!(
                "{number} out of bounds for these choices. Changing it to {}",
                found.len()
            );
            found.len()
        } else {
            number
        };
        found.get(number - 1).unwrap().clone()
    }
}

#[derive(Clone, Copy)]
pub enum Statistics {
    Keys,
    Summary,
}

#[derive(Clone)]
pub enum DoWhat {
    Add(String),
    Remove(String),
    Rename(Vec<String>),
}

#[derive(Clone)]
enum TargetAction<'a> {
    Activity(DoWhat),
    File(&'a str),
}

#[derive(Clone)]
pub struct Editor<'a> {
    manager: CategoryManager,
    target_action: TargetAction<'a>,
}

impl<'a> Editor<'a> {
    fn new(target_action: TargetAction<'a>) -> Self {
        let manager = CategoryManager::new();
        Self {
            manager,
            target_action,
        }
    }

    fn with_scope(mut self, scope: Scope) -> Self {
        let scope = if scope.get_name() == "<global>" {
            Scope::Local(CategoryManager::new().name)
        } else {
            scope
        };
        self.manager = scope.get_manager();
        self
    }
    fn edit(&self) {
        match &self.target_action {
            TargetAction::Activity(action) => self.modify_category(action.clone()),
            TargetAction::File(filename) => self
                .edit_config_file(filename)
                .expect("Could not edit configuration file"),
        }
    }

    fn modify_category(&self, action: DoWhat) {
        match action {
            DoWhat::Add(category_name) => self
                .manager
                .create_new(category_name)
                .expect("Could not create new category"),
            DoWhat::Remove(fname) => self
                .manager
                .delete_category(&fname)
                .expect("Could not delete category from database"),
            DoWhat::Rename(names) => {
                let old_name = names.get(0).unwrap();
                let new_name = names.get(1).unwrap();
                self.manager
                    .rename_category(old_name, new_name)
                    .expect("Could not rename category");
            }
        }
    }

    fn edit_config_file(&self, config_file: &'a str) -> std::io::Result<()> {
        let mut needs_refresh = false;
        let mut category_name = String::new();
        let filename = match config_file {
            "bookmarks" => {
                let chosen_category = if Cli::parse().category.is_some() {
                    Cli::parse().category.unwrap()
                } else {
                    self.manager.name.clone()
                };
                category_name = chosen_category.clone();
                let filename = format!("{}/{chosen_category}", self.manager.root);
                if !Configurations::check_path(&filename) {
                    eprintln!(
                        "Error: the category which you want to edit, '{chosen_category}', cannot be found in database.",
                    );
                    eprintln!(
                        "Consider creating it with 'hlg --new-category {chosen_category}' before performing any editing operations on it."
                    );
                    std::process::exit(24);
                }
                needs_refresh = true;
                filename
            }
            "config" => Configurations::config_file(),
            _ => {
                eprintln!("No file with that name exists here");
                std::process::exit(1);
            }
        };
        let template = fs::read_to_string(&filename)?;
        let edited = edit(template)?;
        if needs_refresh {
            // Then this is a category file, so:
            let lines: Vec<&str> = edited.lines().collect();
            let header = lines.get(0).unwrap();
            if header.starts_with("#!") {
                CategoryManager::validate_header(&header[2..], &filename);
                // Okay, the header is fine but what about key allocation?
                let fields: Vec<&str> = header.split(';').collect();
                if fields.len() > 1 {
                    let key = &fields[1].trim();
                    if !key.is_empty()
                        && CategoryManager::get_name_by_key(key.chars().nth(0).unwrap()).is_some()
                    {
                        let key = key.chars().nth(0).unwrap();
                        let refcat = CategoryManager::get_name_by_key(key).unwrap();
                        if refcat != category_name {
                            eprintln!(
                                "Error: this key '{key}' you are trying to assign to the {category_name} category is already assigned to the {refcat} category."
                            );
                            eprintln!(
                                "No two categories in the database can use one shortcut key."
                            );
                            std::process::exit(19);
                        }
                    }
                }
            }
            let save_with_shortcuts = Configurations::get_setting().save_with_shortcuts.unwrap_or(
                Configurations::default_settings()
                    .save_with_shortcuts
                    .unwrap(),
            );
            let edited = if save_with_shortcuts {
                let mut bookmarks = String::new();
                for line in lines {
                    let newline = if line.contains("<http") || line.contains("<file:") {
                        Self::use_link_shortcuts(line)
                    } else {
                        line.to_string()
                    };
                    bookmarks = format!("{bookmarks}{newline}\n");
                }
                bookmarks
            } else {
                edited
            };
            fs::write(&filename, edited)?;
            CategoryManager::refresh_category_headers();
        } else {
            fs::write(&filename, edited)?;
        }
        Ok(())
    }

    fn use_link_shortcuts(line: &str) -> String {
        line.replace("www.", "=")
            .replace("http://localhost:", "@#")
            .replace("http://", "@!")
            .replace("https://", "@")
            .replace("file://", "@+")
    }
}

pub enum Action<'a> {
    Launch(Target),
    Listing(Statistics),
    Edit(Editor<'a>),
}

impl Action<'_> {
    pub fn new() -> Self {
        Self::check_program_configs();
        let choice = Cli::parse();
        if choice.edit
            || choice.config
            || choice.rename.is_some()
            || choice.delete.is_some()
            || choice.new.is_some()
        {
            let target = {
                if choice.edit {
                    TargetAction::File("bookmarks")
                } else if choice.config {
                    TargetAction::File("config")
                } else {
                    let activity = if choice.delete.is_some() {
                        DoWhat::Remove(choice.delete.unwrap())
                    } else if choice.new.is_some() {
                        DoWhat::Add(choice.new.unwrap().clone())
                    } else {
                        DoWhat::Rename(choice.rename.unwrap().clone())
                    };
                    TargetAction::Activity(activity)
                }
            };
            let editor = Editor::new(target);
            Self::Edit(editor)
        } else if choice.keycategory || choice.keyglobal || choice.summary {
            let lister = if choice.keycategory || choice.keyglobal {
                Statistics::Keys
            } else {
                Statistics::Summary
            };
            Self::Listing(lister)
        } else {
            Self::Launch(Target::new(choice))
        }
    }

    // Set program defaults:
    fn check_program_configs() {
        if !Configurations::check_path(&Configurations::get_app_home()) {
            Configurations::initialize_defaults().expect(
                "Could not create program configuration files. Please check file permissions",
            );
        }
    }

    pub fn on(&self) {
        let mut scope = Scope::Local(
            Configurations::get_setting()
                .default_category
                .unwrap_or(Configurations::default_settings().default_category.unwrap()),
        );
        match self {
            Self::Edit(editor) => editor.clone().with_scope(scope).edit(),
            Self::Launch(target) => LaunchPad::new(target.clone()).start(),
            Self::Listing(lister) => {
                scope = if Cli::parse().keyglobal {
                    Scope::Global
                } else {
                    scope
                };
                scope.get_stats(*lister);
            }
        }
    }
}

#[derive(Clone)]
pub enum Scope {
    Local(String),
    Global,
}

impl Scope {
    fn get_manager(&self) -> CategoryManager {
        if self.is_local() {
            CategoryManager::new().by_name(self.get_name().clone())
        } else {
            CategoryManager::new()
        }
    }

    pub fn get_name(&self) -> String {
        match self {
            Self::Local(category_name) => category_name.to_string(),
            _ => "<global>".to_string(),
        }
    }

    pub fn is_local(&self) -> bool {
        match self {
            Self::Global => false,
            _ => true,
        }
    }

    fn apply(&self) -> Category {
        let manager = CategoryManager::new();
        match self {
            Self::Global => manager.set_global(),
            Self::Local(category_name) => manager.by_name(category_name).preferred_category(),
        }
    }

    /// List available shortcut keys:
    fn list_keys(&self) {
        let category = self.apply();
        let mut keymap: Vec<Bookmark> = category
            .entries()
            .into_iter()
            .filter(|bookmark| bookmark.info.key.is_some())
            .collect();
        let display_text = match self {
            Self::Global => "global scope".to_string(),
            Self::Local(name) => "local category ".to_string() + name,
        };
        if keymap.len() > 0 {
            println!("Key Listing for the {display_text}");
            println!("* * * * * *");
            println!(
                "There is  a total of {} bookmarks in the scope, of which {} of them have got shortcuts.",
                category.entries().len(),
                keymap.len()
            );
            println!("These are:");
            println!("Key:\tBookmark");
            keymap.sort_by(|a, b| {
                a.info
                    .get_key()
                    .to_lowercase()
                    .cmp(b.info.get_key().to_lowercase())
            });
            for bookmark in keymap {
                println!("{}:\t{}", bookmark.info.get_key(), bookmark);
            }
        } else {
            println!(
                "Hi, seems you currently have no {display_text} shortcut keys assigned to your bookmarks."
            );
            println!(
                "Assign shortcut keys  by either placing an underscore before the bookmark name or else append a pair of parentheses to the bookmark name and place the desired shortcut key inside the parentheses."
            );
            println!("Bye!");
        }
    }

    fn summary(&self) {
        if Configurations::check_path(&Configurations::config_file()) {
            println!("hlg Special Bookmark Summary:");
            println!("* * * * * * * *");
            println!(
                "1. Your default category is currently set to '{}'",
                Configurations::get_setting()
                    .default_category
                    .unwrap_or(Configurations::default_settings().default_category.unwrap())
            );
            let home = Configurations::get_homepage();
            let home = if home.contains(".") {
                let (category, bookmark) = home.split_at(home.find('.').unwrap());
                let category = if category.is_empty() {
                    "default"
                } else {
                    category
                };
                let bookmark = if bookmark.is_empty() {
                    "category home"
                } else {
                    bookmark
                };
                format!(
                    "the '{}' bookmark which is in the {category} category",
                    bookmark.strip_prefix('.').unwrap()
                )
            } else {
                format!(" the {home} bookmark in the global scope")
            };
            println!("2. Your main home bookmark is set to {home}");

            let last_visited = format!("{}/last", Configurations::get_records());
            if Configurations::check_path(&last_visited) {
                let last_bookmark = Configurations::get_last_visit();
                let position = last_bookmark.find('.').unwrap();
                let (category, name) = last_bookmark.split_at(position);
                let category = if category == "<global>" {
                    "the global scope".to_string()
                } else {
                    format!("the {category} category")
                };
                println!(
                    "3. Your last visited bookmark is '{}' which is in {category}",
                    name.strip_prefix('.').unwrap()
                );
            } else {
                println!(
                    "3. Currently we have no record of your last visited bookmak. Perhaps this is the first time running hlg on this computer."
                );
                println!(
                    "Run this program after making some visits to get the record of your last visited bookmark."
                );
            }
        } else {
            println!(
                "Hi, your configuration files are not yet set. Could this be your first run of this program on this computer?"
            );
            println!(
                "hlg can only provide a quick summary of your activities based on the setting in your configuration file."
            );
            println!("Bye.");
        }
    }

    fn get_stats(&self, lister: Statistics) {
        match lister {
            Statistics::Keys => self.list_keys(),
            Statistics::Summary => self.summary(),
        }
    }

    /// Extend bookmarks available in a scope by requesting:
    pub fn extend_with(category: &str) -> HashSet<Bookmark> {
        match category {
            "<global>" => CategoryManager::new().set_global().entries(),
            _ => CategoryManager::new()
                .by_name(category)
                .preferred_category()
                .entries(),
        }
    }
}

#[derive(Clone)]
enum Selector {
    Aliases(String),
    Starred,
}

#[derive(Clone)]
enum SearchService {
    Engine(String, String),
    Wiki(String, String),
}

impl SearchService {
    fn new(candidate: &str) -> Self {
        let (engine, phrase) = candidate.split_at(candidate.find(')').unwrap());
        let engine = engine.strip_prefix('(').unwrap().to_string();
        let phrase = phrase.strip_prefix(')').unwrap();
        if phrase.starts_with('+') {
            let topic = phrase.strip_prefix('+').unwrap();
            let topic = topic.replace(" ", "_");
            SearchService::Wiki(engine, topic)
        } else {
            let keywords = phrase.strip_prefix('?').unwrap();
            let keywords = keywords.replace(" ", "+");
            SearchService::Engine(engine, keywords)
        }
    }

    fn is_wiki(&self) -> bool {
        match self {
            Self::Wiki(_, _) => true,
            _ => false,
        }
    }

    fn is_search(&self) -> bool {
        match self {
            Self::Engine(_, _) => true,
            _ => false,
        }
    }
}

#[derive(Clone)]
pub struct Target {
    name: String,
    scope: Scope,
}

impl Target {
    fn new(choice: Cli) -> Self {
        let candidate = if choice.category.is_none() && choice.name.is_none() {
            let home = Configurations::get_homepage();
            if !home.contains('.') {
                format!("<global>.{home}")
            } else {
                home
            }
        } else if choice.name.is_none() && choice.category.is_some() {
            format!("{}.CATEGORY_HOME", choice.category.unwrap())
        } else if choice.name.is_some() && choice.category.is_none() {
            let values = choice.name.clone().unwrap();
            let tg = values.get(0).unwrap();
            if (tg.starts_with(':') && values.len() > 1)
                || tg.starts_with(":?")
                || tg.starts_with(":+")
            {
                eprintln!(
                    "Error: Already searches are done in the global scope. So there is no need to qualify an expression with the global scope indicator."
                );
                eprintln!("Simply remove the leading colon to fix this error.");
                std::process::exit(54);
            }
            if tg.starts_with('=') && values.len() > 1 {
                eprintln!(
                    "Error: The equality trigger is only used with category and bookmark names in the database and cannot be used in search phrases."
                );
                std::process::exit(52);
            }
            let key = tg.chars().nth(0).unwrap();
            if tg.len() == 1 && values.len() == 1 && !key.is_alphanumeric() {
                let key = tg.chars().nth(0).unwrap();
                if key == '+' {
                    format!(
                        "{}./*",
                        Configurations::get_setting().default_category.unwrap_or(
                            Configurations::default_settings().default_category.unwrap()
                        )
                    )
                } else {
                    Self::special_keys(key)
                }
            } else if (tg.starts_with('?') || tg.starts_with('+') || tg.ends_with(':'))
                || values.len() > 1
            {
                Self::process_search(choice)
            } else if tg.starts_with('@') {
                Self::process_category_shortcuts(tg)
            } else if tg.starts_with('=') {
                Self::equality_argument_processor(tg)
            } else {
                Self::process_bookmark(tg)
            }
        } else {
            // Both are set:
            let values = choice.name.unwrap();
            let word = values.get(0).unwrap();
            // Do initial checks before processing:
            if word.starts_with('?')
                || word.starts_with('+')
                || word.starts_with(':')
                || word.starts_with('@')
            {
                eprintln!(
                    "Error: you cannot set the '--category' option while the global scope switch is oactive"
                );
                eprintln!(
                    "Searches and the colon symbol automatically switch to the global scope. So remove these if you want to use the category option."
                );
                eprintln!(
                    "Additionally, the at magic symbol is a complete instruction on its own which renders the category option unnecessary."
                );
                std::process::exit(28);
            }
            if values.len() > 1 {
                eprintln!(
                    "Error: Automatic searching for phrases only happens in the global scope. As you have selected  the category '{}', the search facility is no longer available.",
                    choice.category.unwrap()
                );
                eprintln!(
                    "Any argument passed to hlg should either be a bookmark name or an alias. So remove the extra arguments or the category option to fix this error."
                );
                std::process::exit(49);
            }
            if word.contains('.') {
                eprintln!(
                    "Error: category is set in two different places: the '--category' option has the argument of {} and the prefix to the {word} argument both set the category to be used.",
                    choice.category.unwrap()
                );
                eprintln!("To fix this error, just set the category in one option and try again.");
                std::process::exit(28);
            }
            let text = if word == "+" {
                "/*".to_string()
            } else {
                word.to_string()
            };
            let category = choice.category.unwrap();
            Self::check_spurious_qualifications(&category);
            format!("{category}.{text}")
        };
        let position = candidate.find('.').unwrap();
        let (category, name) = candidate.split_at(position);
        let scope = if category == "<global>" {
            Scope::Global
        } else {
            Scope::Local(category.to_string())
        };
        let name = format!("{}", &name[1..]);
        Self { scope, name }
    }

    fn process_search(choice: Cli) -> String {
        let phrases = choice.name.unwrap().clone();
        let length = phrases.len();
        let bookmark_candidate = phrases.get(0).unwrap();
        let mut first_word_is_service = false;
        let search_phrase = if (bookmark_candidate.starts_with('?')
            || bookmark_candidate.starts_with('+'))
            && length == 1
        {
            bookmark_candidate.clone()
        } else if (bookmark_candidate.starts_with('?') || bookmark_candidate.starts_with('+'))
            && length > 1
        {
            phrases.join(" ")
        } else if length > 1 {
            let terms = &phrases[1..];
            let guide = terms.get(0).unwrap();
            first_word_is_service = bookmark_candidate.ends_with(':');
            if first_word_is_service {
                if guide.starts_with('?') || guide.starts_with('+') {
                    terms.join(" ")
                } else {
                    format!("?{}", terms.join(" "))
                }
            } else {
                format!("?{}", phrases.join(" "))
            }
        } else {
            format!("?{}", phrases.join(" "))
        };
        let service = if first_word_is_service {
            format!("{}", bookmark_candidate.strip_suffix(':').unwrap())
        } else if search_phrase.starts_with('?') {
            Self::get_default_service("search")
        } else {
            Self::get_default_service("wiki")
        };
        format!("<global>.({service}){search_phrase}")
    }

    fn check_spurious_qualifications(category: &String) {
        let default_category = Configurations::get_setting()
            .default_category
            .unwrap_or(Configurations::default_settings().default_category.unwrap());
        if default_category == *category {
            println!("WARNING: spurious bookmark name qualification.");
            println!(
                "Your default category is already set to {category}, yet you are invoking its bookmark with the category name supplied."
            );
            println!(
                "Just call bookmarks in the default category without passing in the category name, whether as a shortcut or as a fully-qualified bookmark name to silence this warning."
            );
        }
    }

    fn process_category_shortcuts(token: &String) -> String {
        let key = token.chars().nth(1).unwrap();
        let target = &token[2..];
        let possible_category = CategoryManager::get_name_by_key(key);
        if possible_category.is_none() {
            eprintln!("Error: this shortcut key, '{key}' is not assigned to any category.");
            eprintln!(
                "If you want to assign it, create a category header which uses semicolon field separators, then in the second field add the key like this:  ';{key};' followed by any description of the category."
            );
            std::process::exit(37);
        }
        let category = possible_category.unwrap();
        if target.contains('.') {
            eprintln!("Error: Invalid syntax in calling category via a shortcut.");
            eprintln!(
                "A period is used to qualify a bookmark name so that it references its category. However, with a shortcut key, the period becomes unnecessary as you are fetching an identified category with that shortcut."
            );
            eprintln!(
                "Just pass in the name of your bookmark or the bookmark shortcut to activate a target."
            );
            std::process::exit(28);
        }
        let target = if target.is_empty() {
            "CATEGORY_HOME"
        } else if target == "+" {
            "/*"
        } else {
            target
        };
        Self::check_spurious_qualifications(&category);
        format!("{category}.{target}")
    }

    fn process_bookmark(target: &String) -> String {
        let category = Configurations::get_setting()
            .default_category
            .unwrap_or(Configurations::default_settings().default_category.unwrap());
        if target.starts_with(':') {
            format!("<global>.{}", target.strip_prefix(':').unwrap())
        } else if target.starts_with(".") {
            format!("{category}{target}")
        } else if target.ends_with(".") {
            Self::check_spurious_qualifications(&target.strip_suffix('.').unwrap().to_string());
            format!("{target}CATEGORY_HOME")
        } else if target.ends_with(".+") {
            Self::check_spurious_qualifications(&format!("{}", &target[..target.len() - 1]));
            format!("{}/*", &target[..target.len() - 1])
        } else if !target.contains(".") {
            format!("{category}.{target}")
        } else {
            let (cat, _) = target.split_at(target.find('.').unwrap());
            Self::check_spurious_qualifications(&cat.to_string());
            target.to_string()
        }
    }

    fn get_default_service(service_name: &str) -> String {
        let pages = Configurations::get_setting().special_pages;
        if pages.is_none() {
            if service_name == "search" {
                return "HLG_SEARCH".to_string();
            } else if service_name == "wiki" {
                return "HLG_WIKI".to_string();
            } else {
                eprintln!(
                    "Error: the 'special-pages' section is missing in your configuration file, so you do not have the '{service_name}' option entry set."
                );
                eprintln!(
                    "Please create this section and add the 'search' option along with a special bookmark with the search flag, before running this option."
                );
                std::process::exit(63);
            }
        }
        let page = pages.unwrap();
        match service_name {
            "search" => {
                if page.search.is_some() {
                    page.search.unwrap()
                } else {
                    String::from("HLG_SEARCH")
                }
            }
            "wiki" => {
                if page.wiki.is_some() {
                    page.wiki.unwrap()
                } else {
                    String::from("HLG_WIKI")
                }
            }
            _ => {
                eprintln!("Error: search service not recognized.");
                std::process::exit(1);
            }
        }
    }

    fn get_pad(&self) -> PadType {
        if self.name.len() == 1 {
            let char = self.name.chars().nth(0).unwrap();
            if !char.is_alphanumeric() {
                eprintln!(
                    "Error: only use alphanumeric characters to launch bookmarks with  shortcut keys."
                );
                eprintln!(
                    "Only special bookmarks can be launched by symbols, of which {char} is not one of them."
                );
                std::process::exit(32);
            }
            PadType::ByKey(char)
        } else if self.name.starts_with('/') {
            let phrase = &self.name.strip_prefix('/').unwrap();
            let selector = if phrase == &"*" {
                Selector::Starred
            } else {
                Selector::Aliases(phrase.to_string())
            };
            PadType::BySelector(selector)
        } else if self.name.starts_with('(') {
            let service = SearchService::new(&self.name);
            PadType::BySearch(service)
        } else {
            PadType::ByName(self.name.clone())
        }
    }

    fn special_keys(key: char) -> String {
        let category = Configurations::get_setting()
            .default_category
            .unwrap_or(Configurations::default_settings().default_category.unwrap());
        match key {
            '-' => Configurations::get_last_visit(),
            '.' => format!("{category}.CATEGORY_HOME"),
            '?' => format!("<global>.HLG_HELP"),
            _ => {
                eprintln!("Error: Unrecognized symbol to hlg.");
                eprintln!("This key, {key}, is not a special symbol for hlg.");
                std::process::exit(32);
            }
        }
    }

    fn equality_argument_processor(candidate: &String) -> String {
        if candidate.contains('.') {
            eprintln!(
                "Error: An equality trigger is already a fully-qualified name with both the category and the bookmark name using the same name."
            );
            eprintln!(
                "It is not necessary to add another qualifier with the period as you did here."
            );
            std::process::exit(52);
        }
        let candidate = candidate.strip_prefix('=').unwrap();
        if candidate.len() == 1 {
            Self::process_category_shortcuts(&format!("@{candidate}{candidate}"))
        } else {
            Self::process_bookmark(&format!("{candidate}.{candidate}"))
        }
    }
}