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
|
use crate::env_var;
use crate::finder::FinderChoice;
use crate::handler::func::Func;
use crate::handler::info::Info;
use crate::shell::Shell;
use clap::{crate_version, AppSettings, Clap};
use std::str::FromStr;
const FINDER_POSSIBLE_VALUES: &[&str] = &[&"fzf", &"skim"];
const WIDGET_POSSIBLE_VALUES: &[&str] = &[&"bash", &"zsh", &"fish"];
const FUNC_POSSIBLE_VALUES: &[&str] = &[&"url::open", &"welcome", &"widget::last_command", &"map::expand"];
const INFO_POSSIBLE_VALUES: &[&str] = &[&"cheats-path", "config-path"];
impl FromStr for Shell {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bash" => Ok(Shell::Bash),
"zsh" => Ok(Shell::Zsh),
"fish" => Ok(Shell::Fish),
_ => Err("no match"),
}
}
}
impl FromStr for Func {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"url::open" => Ok(Func::UrlOpen),
"welcome" => Ok(Func::Welcome),
"widget::last_command" => Ok(Func::WidgetLastCommand),
"map::expand" => Ok(Func::MapExpand),
_ => Err("no match"),
}
}
}
impl FromStr for Info {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"cheats-path" => Ok(Info::CheatsPath),
"config-path" => Ok(Info::ConfigPath),
_ => Err("no match"),
}
}
}
#[derive(Debug, Clap)]
#[clap(after_help = r#"MORE INFO:
Please refer to https://github.com/denisidoro/navi
MORE ENVIRONMENT VARIABLES:
NAVI_TAG_WIDTH # tag column width as window integer %
NAVI_COMMENT_WIDTH # comment column width as window integer %
NAVI_SHELL # shell used in shell outs
EXAMPLES:
navi # default behavior
navi --print # doesn't execute the snippet
navi --tldr docker # search for docker cheatsheets using tldr
navi --cheatsh docker # search for docker cheatsheets using cheatsh
navi --path '/some/dir:/other/dir' # use .cheat files from custom paths
navi --query git # filter results by "git"
navi --query 'create db' --best-match # autoselect the snippet that best matches a query
db=my navi --query 'create db' --best-match # same, but set the value for the <name> variable
navi repo add denisidoro/cheats # import cheats from a git repository
eval "$(navi widget zsh)" # load the zsh widget
navi --finder 'skim' # set skim as finder, instead of fzf
navi --fzf-overrides '--with-nth 1,2' # show only the comment and tag columns
navi --fzf-overrides '--no-select-1' # prevent autoselection in case of single line
navi --fzf-overrides-var '--no-select-1' # same, but for variable selection
navi --fzf-overrides '--nth 1,2' # only consider the first two columns for search
navi --fzf-overrides '--no-exact' # use looser search algorithm
NAVI_SHELL=dash navi # use dash in shell outs
NAVI_TAG_WIDTH=30 NAVI_COMMENT_WIDTH=40 navi # customize column widths
navi --tag-rules='git,!checkout' # show non-checkout git snippets only"#)]
#[clap(setting = AppSettings::ColorAuto)]
#[clap(setting = AppSettings::ColoredHelp)]
#[clap(setting = AppSettings::AllowLeadingHyphen)]
#[clap(version = crate_version!())]
pub(super) struct ClapConfig {
/// Colon-separated list of paths containing .cheat files
#[clap(short, long, env = env_var::PATH)]
pub path: Option<String>,
/// Instead of executing a snippet, prints it to stdout
#[clap(long)]
pub print: bool,
/// Returns the best match
#[clap(long)]
pub best_match: bool,
/// Search for cheatsheets using the tldr-pages repository
#[clap(long)]
pub tldr: Option<String>,
/// [Experimental] Comma-separated list that acts as filter for tags. Parts starting with ! represent negation
#[clap(long)]
pub tag_rules: Option<String>,
/// Search for cheatsheets using the cheat.sh repository
#[clap(long)]
pub cheatsh: Option<String>,
/// Query
#[clap(short, long)]
pub query: Option<String>,
/// Finder overrides for snippet selection
#[clap(long)]
pub fzf_overrides: Option<String>,
/// Finder overrides for variable selection
#[clap(long)]
pub fzf_overrides_var: Option<String>,
/// Finder application to use
#[clap(long, possible_values = FINDER_POSSIBLE_VALUES, case_insensitive = true)]
pub finder: Option<FinderChoice>,
#[clap(subcommand)]
pub cmd: Option<Command>,
}
impl ClapConfig {
pub fn new() -> Self {
Self::parse()
}
}
#[derive(Debug, Clap)]
pub enum Command {
/// [Experimental] Performs ad-hoc, internal functions provided by navi
Fn {
/// Function name (example: "url::open")
#[clap(possible_values = FUNC_POSSIBLE_VALUES, case_insensitive = true)]
func: Func,
/// List of arguments (example: "https://google.com")
args: Vec<String>,
},
/// Manages cheatsheet repositories
Repo {
#[clap(subcommand)]
cmd: RepoCommand,
},
/// Used for fzf's preview window when selecting snippets
#[clap(setting = AppSettings::Hidden)]
Preview {
/// Selection line
line: String,
},
/// Used for fzf's preview window when selecting variable suggestions
#[clap(setting = AppSettings::Hidden)]
PreviewVar {
/// Selection line
selection: String,
/// Query match
query: String,
/// Typed text
variable: String,
},
/// Used for fzf's preview window when selecting variable suggestions
#[clap(setting = AppSettings::Hidden)]
PreviewVarStdin,
/// Outputs shell widget source code
Widget {
#[clap(possible_values = WIDGET_POSSIBLE_VALUES, case_insensitive = true, default_value = "bash")]
shell: Shell,
},
/// Shows info
Info {
#[clap(possible_values = INFO_POSSIBLE_VALUES, case_insensitive = true)]
info: Info,
},
}
#[derive(Debug, Clap)]
pub enum RepoCommand {
/// Imports cheatsheets from a repo
Add {
/// A URI to a git repository containing .cheat files ("user/repo" will download cheats from github.com/user/repo)
uri: String,
},
/// Browses for featured cheatsheet repos
Browse,
}
pub enum Source {
Filesystem(Option<String>, Option<String>),
Tldr(String),
Cheats(String),
}
pub enum Action {
Print,
Execute,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_widget_possible_values() {
for v in WIDGET_POSSIBLE_VALUES {
assert_eq!(true, Shell::from_str(v).is_ok())
}
}
#[test]
fn test_info_possible_values() {
for v in INFO_POSSIBLE_VALUES {
assert_eq!(true, Info::from_str(v).is_ok())
}
}
#[test]
fn test_func_possible_values() {
for v in FUNC_POSSIBLE_VALUES {
assert_eq!(true, Func::from_str(v).is_ok())
}
}
#[test]
fn test_finder_possible_values() {
for v in FINDER_POSSIBLE_VALUES {
assert_eq!(true, FinderChoice::from_str(v).is_ok())
}
}
}
|