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
use rustyline::Context;
use rustyline::{
completion::{Completer, Pair},
hint::Hinter,
};
use rustyline_derive::{Helper, Highlighter, Validator};
use std::collections::BTreeMap;
#[derive(Helper, Validator, Highlighter)]
pub(crate) struct BofhHelper<'a> {
pub(crate) commands: &'a BTreeMap<String, bofh::CommandGroup>,
}
impl BofhHelper<'_> {
pub(crate) fn command_candidates(&self, prefix: &str) -> Vec<&str> {
self.commands
.keys()
.filter_map(|command| {
if command.starts_with(prefix) {
Some(command.as_str())
} else {
None
}
})
.collect()
}
pub(crate) fn subcommand_candidates(&self, command: &str, prefix: &str) -> Vec<&str> {
if let Some(command) = self.commands.get(command) {
command
.commands
.keys()
.filter_map(|command| {
if command.starts_with(prefix) {
Some(command.as_str())
} else {
None
}
})
.collect()
} else {
vec![]
}
}
}
impl Hinter for BofhHelper<'_> {
type Hint = String;
fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<String> {
let words: Vec<&str> = line.split_whitespace().collect();
if words.is_empty() || pos < line.len() {
return None;
}
let spaces = line.matches(char::is_whitespace).count();
let mut word_pos = pos - spaces;
let command_candidates = self.command_candidates(words[0]);
let subcommand_candidates = if words.len() > 1 {
self.subcommand_candidates(command_candidates[0], words[1])
} else {
vec![]
};
// Hint arguments
if words.len() >= 2 {
// We can only hint arguments if we know the command and subcommand
if command_candidates.len() == 1 && subcommand_candidates.len() == 1 {
let command = self.commands.get(command_candidates[0]).unwrap();
let subcommand = command.commands.get(subcommand_candidates[0]).unwrap();
// Hint arguments if subcommand is complete or unambiguously partial
if words[1] == subcommand.name || line.ends_with(char::is_whitespace) {
// TODO reduce this:
if words.len() >= 2 {
return Some(format!(
"{}{}",
if line.ends_with(char::is_whitespace) {
""
} else {
" "
},
subcommand.args[words.len() - 2..]
.iter()
.filter_map(|arg| arg.arg_type.clone())
.collect::<Vec<String>>()
.join(" ")
));
}
}
}
};
// If we're not hinting arguments, and the line ends in a whitespace, we shouldn't hint.
// This fixes a bug where inserting spaces when a hint has appeared will push the hint towards the right.
//
// TODO In the unlikely scenario that the server only supports one command, or it has a command
// TODO which only supports one subcommand, this will erroneously cause that (sub)command not to
// TODO be hinted! Should probably be fixed in a better way, just in case.
if line.ends_with(char::is_whitespace) {
return None;
}
// Hint commands
let candidates: Vec<&str> = if words.len() == 1 {
// Complete command group
command_candidates
.iter()
.filter_map(|&command| {
if command == words[0] {
None
} else {
Some(command)
}
})
.collect()
} else if words.len() == 2 {
word_pos -= words[0].len();
if command_candidates.len() == 1 {
subcommand_candidates
.iter()
.filter_map(|&command| {
if command == words[1] {
None
} else {
Some(command)
}
})
.collect()
} else {
vec![]
}
} else {
return None;
};
// We only give unambiguous hints, ie. if there is one and only one hint
if candidates.len() == 1 {
Some(candidates[0][word_pos..].to_owned())
} else {
None
}
}
}
impl Completer for BofhHelper<'_> {
type Candidate = Pair;
fn complete(
&self,
line: &str,
pos: usize,
_ctx: &Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
let words: Vec<&str> = line.split_whitespace().collect();
let spaces = line.matches(char::is_whitespace).count();
let mut word_pos = pos - spaces;
// Complete commands
let candidates: Vec<&str> = if words.is_empty() {
// Completing on an empty line shows all command groups
self.commands.keys().map(String::as_str).collect()
} else if words.len() == 1 {
if line.ends_with(char::is_whitespace) {
// Complete subcommands
if let Some(command_group) = self.commands.get(words[0]) {
word_pos -= words[0].len();
command_group.commands.keys().map(String::as_str).collect()
} else {
vec![]
}
} else {
// Complete command group
self.command_candidates(words[0])
}
} else if words.len() == 2 && !line.ends_with(char::is_whitespace) {
word_pos -= words[0].len();
let command_candidates = self.command_candidates(words[0]);
// Complete subcommand
if command_candidates.len() == 1 {
self.subcommand_candidates(command_candidates[0], words[1])
} else {
vec![]
}
} else {
vec![]
};
Ok((
pos,
candidates
.iter()
.map(|&candidate| Pair {
display: candidate.to_owned(),
replacement: if candidates.len() == 1 {
format!("{} ", &candidate[word_pos..])
} else {
candidate[word_pos..].to_owned()
},
})
.collect(),
))
}
}