1mod cache;
2mod os_command;
3mod parser;
4mod prelude;
5mod process;
6mod reserved_keywords;
7pub use cache::get_default_cache;
8pub use process::process_command;
9use strum::EnumCount;
10
11pub mod constants {
12 pub const PUT: &str = "put";
13 pub const ALIAS: &str = "alias";
14 pub const GET: &str = "get";
15 pub const CLIPPY: &str = "clippy";
16 pub const CLIPPY_ALT: &str = "clip";
17 pub const DESCRIBE: &str = "describe";
18 pub const DESCRIBE_ALT: &str = "ds";
19 pub const LIST_CACHE: &str = "listns";
20 pub const LIST_CACHE_ALT: &str = "lsns";
21 pub const CURR_CACHE: &str = "currns";
22 pub const CURR_CACHE_ALT: &str = "currentns";
23 pub const RESTORE: &str = "restore";
24 pub const BACKUP: &str = "backup";
25 pub const BACKUP_ALT: &str = "bckp";
26 pub const FLUSH: &str = "flush";
27 pub const DEL_CACHE: &str = "delns";
28 pub const DEL_CACHE_ALT: &str = "deletens";
29 pub const MERGE_CACHE: &str = "merge";
30 pub const MERGE_CACHE_ALT: &str = "mergens";
31 pub const DEL: &str = "del";
32 pub const DEL_ALT: &str = "delete";
33 pub const EXEC: &str = "exec";
34 pub const CD: &str = "cd";
35 pub const USE: &str = "use";
36 pub const DUMP: &str = "dump";
37 pub const CLEAR: &str = "clear";
38 pub const CLEAR_ALT: &str = "cls";
39 pub const PRINT_SCRIPT_CONTEXT: &str = "print_script_ctx";
40 pub const PRINT_SCRIPT_CONTEXT_ALT: &str = "script_ctx";
41 pub const STORE_SCRIPT_CONTEXT: &str = "store_script_ctx";
42 pub const LOAD_SCRIPT_CONTEXT: &str = "load_script_ctx";
43 pub const PRINT_AST: &str = "ast";
44 pub const HELP: &str = "help";
45}
46
47pub use constants::*;
48
49#[derive(Debug, EnumCount)]
50pub enum CacheCommand<'a> {
51 Put { aliases: Vec<&'a str>, value: &'a str },
52 Describe(Option<&'a str>),
53 ListCache,
54 CurrentCache,
55 Backup,
56 Flush,
57 Restore,
58 DeleteCache(Option<&'a str>),
59 Merge(&'a str),
60 Alias((&'a str, &'a str)),
61 Del(&'a str),
62 Get(&'a str),
63 Clip(&'a str),
64 Exec { key: &'a str, args: Option<&'a str> },
65 Cd(ChangeDirectoryType<'a>),
66 Using(&'a str),
67 Dump(Option<&'a str>),
68 Clear,
69 PrintScriptContext,
70 StoreScriptContext(Option<&'a str>),
71 LoadScriptContext(Option<&'a str>),
72 PrintAst(&'a str),
73 Help,
74}
75#[derive(Debug)]
76pub enum ChangeDirectoryType<'a> {
77 HomeDirectory(Option<&'a str>),
78 Path(&'a str),
79 Previous,
80}
81
82impl CacheCommand<'_> {
83 pub const fn doc() -> &'static [(&'static [&'static str], &'static str)] {
84 if CacheCommand::COUNT != 23 {
85 panic!("CacheCommand::doc() no longer valid!");
86 }
87 &[
88 (&[PUT], "Put a new value to current namespace. can have multiple aliases with option '-a'. e.g `put -a drc -a drcomp docker-compose`"),
89 (&[DESCRIBE,DESCRIBE_ALT], "List values within the current namespace. Optional parameter for searching e.g `ds ssh`"),
90 (&[LIST_CACHE, LIST_CACHE_ALT], "List available namespaces."),
91 (&[CURR_CACHE, CURR_CACHE_ALT], "Print current namespace."),
92 (&[BACKUP, BACKUP_ALT], "Backup the database of namespaces to the current directory"),
93 (&[FLUSH], "Flush to database."),
94 (&[RESTORE], "Restore the database from current directory"),
95 (&[ALIAS], "Alias a key with another. e.g alias commit gc"),
96 (&[DEL_CACHE,DEL_CACHE_ALT], "Delete namespace or clear current namespace values."),
97 (&[MERGE_CACHE,MERGE_CACHE_ALT], "Merge current with a given namespace"),
98 (&[DEL,DEL_ALT], "Remove value from namespace. e.g `del drc`"),
99 (&[GET], "Get value from namespace. e.g `get drc`"),
100 (&[CLIPPY, CLIPPY_ALT], "Get value from namespace and copy it to clipboard. e.g `clip drc`"),
101 (&[EXEC], "Run a value from the namespace as an OS command. e.g `exec drc`"),
102 (&[CD], "Navigate to a directory"),
103 (&[USE], "Switch to another namespace. default ns is DEFAULT. e.g `use linux`"),
104 (&[DUMP], "Dump namespace(s) as json. Take an optional parameter, the namespace name. e.g `dump linux`"),
105 (&[CLEAR, CLEAR_ALT], "Clear the terminal."),
106 (&[PRINT_SCRIPT_CONTEXT, PRINT_SCRIPT_CONTEXT_ALT], "Print script context"),
107 (&[STORE_SCRIPT_CONTEXT], "Store script context (name optional), e.g store_script_ctx 12022023"),
108 (&[LOAD_SCRIPT_CONTEXT], "Load script context (name optional), e.g load_script_ctx 12022023"),
109 (&[PRINT_AST], "print ast for script code."),
110 (&[HELP], "Display Help."),
111 ]
112 }
113}
114
115pub fn clear_terminal() {
116 if cfg!(unix) {
117 let _ = std::process::Command::new("clear").status();
118 } else if cfg!(windows) {
119 let _ = std::process::Command::new("cls").status();
120 } else {
121 eprintln!("cannot clear the terminal for the target os");
122 };
123}