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
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 Hinter for BofhHelper<'_> {
type Hint = String;
fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<String> {
if line.is_empty() || pos < line.len() {
return None;
}
let words: Vec<&str> = line.split_whitespace().collect();
let spaces = line.matches(char::is_whitespace).count();
let mut word_pos = pos - spaces;
// Hint arguments
if words.len() >= 2 {
if let Some(command) = self.commands.get(words[0]) {
if let Some(subcommand) = command.commands.get(words[1]) {
let args_to_hint = subcommand.args.len() - words.len() + 2;
if args_to_hint <= subcommand.args.len() {
return Some(format!(
"{}{}",
if line.ends_with(char::is_whitespace) {
""
} else {
" "
},
subcommand.args[subcommand.args.len() - args_to_hint..]
.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
self.commands
.keys()
.filter_map(|command| {
if command.starts_with(words[0]) && command != words[0] {
Some(command.as_str())
} else {
None
}
})
.collect()
} else if words.len() == 2 {
word_pos -= words[0].len();
if let Some(command) = self.commands.get(words[0]) {
command
.commands
.keys()
.filter_map(|command| {
if command.starts_with(words[1]) && command != words[1] {
Some(command.as_str())
} else {
None
}
})
.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 {
let candidates = 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.commands
.keys()
.filter_map(|command| {
if command.starts_with(words[0]) {
Some(command.as_str())
} else {
None
}
})
.collect()
};
candidates
} else if words.len() == 2 && !line.ends_with(char::is_whitespace) {
word_pos -= words[0].len();
// Complete subcommand
if let Some(command) = self.commands.get(words[0]) {
command
.commands
.keys()
.filter_map(|command| {
if command.starts_with(words[1]) {
Some(command.as_str())
} else {
None
}
})
.collect()
} 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(),
))
}
}