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
//! Native Rust implementation of _describe
//!
//! _describe adds completions with descriptions to the completion system.
//! It's called by most command-specific completion functions.
use crate::compcore::CompletionState;
use crate::completion::{Completion, CompletionFlags};
/// Options for _describe
#[derive(Clone, Debug, Default)]
pub struct DescribeOpts {
/// Tag for this set of completions (-t)
pub tag: Option<String>,
/// Matcher spec (-M)
pub matcher: Option<String>,
/// Group name (-V or -J)
pub group: Option<String>,
/// Sorted group (-J)
pub sorted: bool,
/// Don't quote completions (-Q)
pub no_quote: bool,
/// Prefix (-P)
pub prefix: Option<String>,
/// Suffix (-S)
pub suffix: Option<String>,
/// Remove suffix on certain chars (-r)
pub remove_suffix: Option<String>,
}
impl DescribeOpts {
pub fn new() -> Self {
Self::default()
}
/// Parse _describe arguments
/// Format: _describe [-t tag] description array ...
pub fn parse(args: &[String]) -> (Self, String, Vec<String>) {
let mut opts = Self::new();
let mut i = 0;
let mut description = String::new();
let mut items = Vec::new();
// Parse options
while i < args.len() {
match args[i].as_str() {
"-t" => {
if i + 1 < args.len() {
opts.tag = Some(args[i + 1].clone());
i += 2;
continue;
}
}
"-M" => {
if i + 1 < args.len() {
opts.matcher = Some(args[i + 1].clone());
i += 2;
continue;
}
}
"-V" => {
if i + 1 < args.len() {
opts.group = Some(args[i + 1].clone());
opts.sorted = false;
i += 2;
continue;
}
}
"-J" => {
if i + 1 < args.len() {
opts.group = Some(args[i + 1].clone());
opts.sorted = true;
i += 2;
continue;
}
}
"-P" => {
if i + 1 < args.len() {
opts.prefix = Some(args[i + 1].clone());
i += 2;
continue;
}
}
"-S" => {
if i + 1 < args.len() {
opts.suffix = Some(args[i + 1].clone());
i += 2;
continue;
}
}
"-Q" => {
opts.no_quote = true;
i += 1;
continue;
}
"-r" => {
if i + 1 < args.len() {
opts.remove_suffix = Some(args[i + 1].clone());
i += 2;
continue;
}
}
arg if !arg.starts_with('-') => {
// First non-option is description
if description.is_empty() {
description = arg.to_string();
} else {
// Rest are item arrays
items.push(arg.to_string());
}
}
_ => {}
}
i += 1;
}
(opts, description, items)
}
}
/// An item with optional description for _describe
#[derive(Clone, Debug)]
pub struct DescribeItem {
/// The completion string
pub value: String,
/// Optional description
pub description: String,
}
impl DescribeItem {
/// Parse "value:description" format
pub fn parse(s: &str) -> Self {
if let Some(pos) = s.find(':') {
Self {
value: s[..pos].to_string(),
description: s[pos + 1..].to_string(),
}
} else {
Self {
value: s.to_string(),
description: String::new(),
}
}
}
/// Parse from escaped format "value\:with\:colons:description"
pub fn parse_escaped(s: &str) -> Self {
let mut value = String::new();
let mut description = String::new();
let mut in_value = true;
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\' {
if let Some(&next) = chars.peek() {
if next == ':' {
value.push(':');
chars.next();
continue;
}
}
if in_value {
value.push(c);
} else {
description.push(c);
}
} else if c == ':' && in_value {
in_value = false;
} else if in_value {
value.push(c);
} else {
description.push(c);
}
}
Self { value, description }
}
}
/// Execute _describe completion
pub fn describe_execute(
state: &mut CompletionState,
opts: &DescribeOpts,
description: &str,
items: &[DescribeItem],
) -> bool {
let prefix = state.params.prefix.clone();
let group_name = opts
.group
.as_deref()
.or(opts.tag.as_deref())
.unwrap_or("default");
state.begin_group(group_name, opts.sorted);
if !description.is_empty() {
state.add_explanation(description.to_string(), Some(group_name));
}
let mut added = false;
for item in items {
// Check if matches prefix
if !item.value.starts_with(&prefix) {
continue;
}
let mut comp_str = item.value.clone();
// Add prefix/suffix
if let Some(ref pfx) = opts.prefix {
comp_str = format!("{}{}", pfx, comp_str);
}
if let Some(ref sfx) = opts.suffix {
comp_str.push_str(sfx);
}
let mut comp = Completion::new(&comp_str);
// Set display with description
if !item.description.is_empty() {
comp.disp = Some(format!("{} -- {}", item.value, item.description));
}
if opts.no_quote {
comp.flags |= CompletionFlags::NOQUOTE;
}
state.add_match(comp, Some(group_name));
added = true;
}
state.end_group();
added
}
/// Parse items from string array (for use with shell arrays)
pub fn parse_items(specs: &[String]) -> Vec<DescribeItem> {
specs
.iter()
.map(|s| DescribeItem::parse_escaped(s))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_item() {
let item = DescribeItem::parse("foo:description of foo");
assert_eq!(item.value, "foo");
assert_eq!(item.description, "description of foo");
}
#[test]
fn test_parse_item_no_desc() {
let item = DescribeItem::parse("foo");
assert_eq!(item.value, "foo");
assert_eq!(item.description, "");
}
#[test]
fn test_parse_escaped() {
let item = DescribeItem::parse_escaped(r"foo\:bar:description");
assert_eq!(item.value, "foo:bar");
assert_eq!(item.description, "description");
}
#[test]
fn test_parse_opts() {
let (opts, desc, items) = DescribeOpts::parse(&[
"-t".to_string(),
"commands".to_string(),
"-J".to_string(),
"git commands".to_string(),
"command".to_string(),
"items_array".to_string(),
]);
assert_eq!(opts.tag, Some("commands".to_string()));
assert_eq!(opts.group, Some("git commands".to_string()));
assert!(opts.sorted);
assert_eq!(desc, "command");
assert_eq!(items, vec!["items_array"]);
}
}