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
use std::fmt;
/// the verb and its arguments, making the invocation.
/// When coming from parsing, the args is Some as soon
/// as there's a separator (i.e. it's "" in "cp ")
#[derive(Clone, Debug, PartialEq)]
pub struct VerbInvocation {
pub name: String,
pub args: Option<String>,
pub bang: bool,
}
impl fmt::Display for VerbInvocation {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
write!(f, ":")?;
if self.bang {
write!(f, "!")?;
}
write!(f, "{}", &self.name)?;
if let Some(args) = &self.args {
write!(f, " {}", &args)?;
}
Ok(())
}
}
impl VerbInvocation {
pub fn new<T: Into<String>>(
name: T,
args: Option<T>,
bang: bool,
) -> Self {
Self {
name: name.into(),
args: args.map(|s| s.into()),
bang,
}
}
pub fn is_empty(&self) -> bool {
self.name.is_empty()
}
/// build a new String
pub fn complete_name(&self) -> String {
if self.bang {
format!("{}_tab", &self.name)
} else {
self.name.clone()
}
}
/// basically return the invocation but allow another name (the shortcut
/// or a variant)
pub fn to_string_for_name(
&self,
name: &str,
) -> String {
let mut s = String::new();
if self.bang {
s.push('!');
}
s.push_str(name);
if let Some(args) = &self.args {
s.push(' ');
s.push_str(args);
}
s
}
}
impl From<&str> for VerbInvocation {
/// Parse a string being or describing the invocation of a verb with its
/// arguments and optional bang. The leading space or colon must
/// have been stripped before.
///
/// Examples:
/// "mv" -> name: "mv"
/// "!mv" -> name: "mv", bang
/// "mv a b" -> name: "mv", args: "a b"
/// "mv!a b" -> name: "mv", args: "a b", bang
/// "a-b c" -> name: "a-b", args: "c", bang
/// "-sp" -> name: "-", args: "sp"
/// "-a b" -> name: "-", args: "a b"
/// "-a b" -> name: "-", args: "a b"
/// "--a" -> name: "--", args: "a"
///
/// Notes:
/// 1. A name is either "special" (only made of non alpha characters)
/// or normal (starting with an alpha character). Special names don't
/// need a space afterwards, as the first alpha character will start
/// the args.
/// 2. The space or colon after the name is optional if there's a bang
/// after the name: the bang is the separator.
/// 3. Duplicate separators before args are ignored (they're usually typos)
/// 4. An opening parenthesis starts args
fn from(invocation: &str) -> Self {
let mut bang_before = false;
let mut name = String::new();
let mut bang_after = false;
let mut args: Option<String> = None;
let mut name_is_special = false;
for c in invocation.chars() {
if let Some(args) = args.as_mut() {
if args.is_empty() && (c == ' ' || c == ':') {
// we don't want args starting with a space just because
// they're doubled or are optional after a special name
} else {
args.push(c);
}
continue;
}
if c == ' ' || c == ':' {
args = Some(String::new());
continue;
}
if c == '(' {
args = Some(c.to_string());
continue;
}
if c == '!' {
if !name.is_empty() {
bang_after = true;
args = Some(String::new());
} else {
bang_before = true;
}
continue;
}
if name.is_empty() {
name.push(c);
if !c.is_alphabetic() {
name_is_special = true;
}
continue;
}
if c.is_alphabetic() && name_is_special {
// this isn't part of the name anymore, it's part of the args
args = Some(c.to_string());
continue;
}
name.push(c);
}
let bang = bang_before || bang_after;
VerbInvocation { name, args, bang }
}
}
#[cfg(test)]
mod verb_invocation_tests {
use super::*;
#[test]
fn check_special_chars() {
assert_eq!(
VerbInvocation::from("-sdp"),
VerbInvocation::new("-", Some("sdp"), false),
);
assert_eq!(
VerbInvocation::from("!-sdp"),
VerbInvocation::new("-", Some("sdp"), true),
);
assert_eq!(
VerbInvocation::from("-!sdp"),
VerbInvocation::new("-", Some("sdp"), true),
);
assert_eq!(
VerbInvocation::from("-! sdp"),
VerbInvocation::new("-", Some("sdp"), true),
);
assert_eq!(
VerbInvocation::from("!@a b"),
VerbInvocation::new("@", Some("a b"), true),
);
assert_eq!(
VerbInvocation::from("!@%a b"),
VerbInvocation::new("@%", Some("a b"), true),
);
assert_eq!(
VerbInvocation::from("22a b"),
VerbInvocation::new("22", Some("a b"), false),
);
assert_eq!(
VerbInvocation::from("22!a b"),
VerbInvocation::new("22", Some("a b"), true),
);
assert_eq!(
VerbInvocation::from("22 !a b"),
VerbInvocation::new("22", Some("!a b"), false),
);
assert_eq!(
VerbInvocation::from("a$b4!r"),
VerbInvocation::new("a$b4", Some("r"), true),
);
assert_eq!(
VerbInvocation::from("a-b c"),
VerbInvocation::new("a-b", Some("c"), false),
);
}
#[test]
fn check_verb_invocation_parsing_empty_arg() {
// those tests focus mainly on the distinction between
// None and Some("") for the args, distinction which matters
// for inline help
assert_eq!(
VerbInvocation::from("!mv"),
VerbInvocation::new("mv", None, true),
);
assert_eq!(
VerbInvocation::from("mva!"),
VerbInvocation::new("mva", Some(""), true),
);
assert_eq!(
VerbInvocation::from("cp "),
VerbInvocation::new("cp", Some(""), false),
);
assert_eq!(
VerbInvocation::from("cp ../"),
VerbInvocation::new("cp", Some("../"), false),
);
}
#[test]
fn check_verb_invocation_parsing_post_bang() {
// ignoring post_bang (see issue #326)
assert_eq!(
VerbInvocation::from("mva!a"),
VerbInvocation::new("mva", Some("a"), true),
);
assert_eq!(
VerbInvocation::from("!!!"),
VerbInvocation::new("", None, true),
);
}
#[test]
fn check_verb_invocation_parsing_empty_verb() {
// there's currently no meaning for the empty verb, it's "reserved"
// and will probably not be used as it may need a distinction between
// one and two initial spaces in the input
assert_eq!(
VerbInvocation::from(""),
VerbInvocation::new("", None, false),
);
assert_eq!(
VerbInvocation::from("!"),
VerbInvocation::new("", None, true),
);
assert_eq!(
VerbInvocation::from("!! "),
VerbInvocation::new("", Some(""), true),
);
assert_eq!(
VerbInvocation::from("!! a"),
VerbInvocation::new("", Some("a"), true),
);
}
#[test]
fn check_verb_invocation_parsing_oddities() {
// checking some corner cases
assert_eq!(
VerbInvocation::from("!!a"), // the second bang is ignored
VerbInvocation::new("a", None, true),
);
assert_eq!(
VerbInvocation::from("!!"), // the second bang is ignored
VerbInvocation::new("", None, true),
);
assert_eq!(
VerbInvocation::from("a ! !"),
VerbInvocation::new("a", Some("! !"), false),
);
assert_eq!(
VerbInvocation::from("!a !a"),
VerbInvocation::new("a", Some("!a"), true),
);
assert_eq!(
VerbInvocation::from("a! ! //"),
VerbInvocation::new("a", Some("! //"), true),
);
assert_eq!(
VerbInvocation::from(".. .."),
VerbInvocation::new("..", Some(".."), false),
);
}
}