Skip to content

Commit 5e6ff2e

Browse files
cli : cleanup auto-completion code (ggml-org#21745)
1 parent e6e24ff commit 5e6ff2e

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

common/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,11 @@ inline bool string_starts_with(std::string_view str, std::string_view prefix) {
746746
str.compare(0, prefix.size(), prefix) == 0;
747747
}
748748

749+
// remove when moving to c++20
750+
inline bool string_starts_with(std::string_view str, char prefix) {
751+
return !str.empty() && str.front() == prefix;
752+
}
753+
749754
// remove when moving to c++20
750755
inline bool string_ends_with(std::string_view str, std::string_view suffix) {
751756
return str.size() >= suffix.size() &&

tools/cli/cli.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ struct cli_context {
228228
};
229229

230230
// TODO?: Make this reusable, enums, docs
231-
static const std::array<const std::string, 7> cmds = {
231+
static const std::array<std::string_view, 7> cmds = {
232232
"/audio ",
233233
"/clear",
234234
"/exit",
@@ -242,19 +242,19 @@ static std::vector<std::pair<std::string, size_t>> auto_completion_callback(std:
242242
std::vector<std::pair<std::string, size_t>> matches;
243243
std::string cmd;
244244

245-
if (line.length() > 1 && line[0] == '/' && !std::any_of(cmds.begin(), cmds.end(), [line](const std::string & prefix) {
245+
if (line.length() > 1 && line.front() == '/' && !std::any_of(cmds.begin(), cmds.end(), [line](std::string_view prefix) {
246246
return string_starts_with(line, prefix);
247247
})) {
248248
auto it = cmds.begin();
249249

250-
while ((it = std::find_if(it, cmds.end(), [line](const std::string & cmd_line) {
250+
while ((it = std::find_if(it, cmds.end(), [line](std::string_view cmd_line) {
251251
return string_starts_with(cmd_line, line);
252252
})) != cmds.end()) {
253-
matches.emplace_back(*it, (*it).length());
253+
matches.emplace_back(*it, it->length());
254254
++it;
255255
}
256256
} else {
257-
auto it = std::find_if(cmds.begin(), cmds.end(), [line](const std::string & prefix) {
257+
auto it = std::find_if(cmds.begin(), cmds.end(), [line](std::string_view prefix) {
258258
return prefix.back() == ' ' && string_starts_with(line, prefix);
259259
});
260260

@@ -271,18 +271,18 @@ static std::vector<std::pair<std::string, size_t>> auto_completion_callback(std:
271271
std::string expanded_prefix = path_prefix;
272272

273273
#if !defined(_WIN32)
274-
if (string_starts_with(path_prefix, "~")) {
274+
if (string_starts_with(path_prefix, '~')) {
275275
const char * home = std::getenv("HOME");
276276
if (home && home[0]) {
277-
expanded_prefix = std::string(home) + path_prefix.substr(1);
277+
expanded_prefix = home + path_prefix.substr(1);
278278
}
279279
}
280-
if (string_starts_with(expanded_prefix, "/")) {
280+
if (string_starts_with(expanded_prefix, '/')) {
281281
#else
282282
if (std::isalpha(expanded_prefix[0]) && expanded_prefix.find(':') == 1) {
283283
#endif
284284
cur_dir = std::filesystem::path(expanded_prefix).parent_path();
285-
cur_dir_str = "";
285+
cur_dir_str.clear();
286286
} else if (!path_prefix.empty()) {
287287
cur_dir /= std::filesystem::path(path_prefix).parent_path();
288288
}
@@ -305,7 +305,7 @@ static std::vector<std::pair<std::string, size_t>> auto_completion_callback(std:
305305
}
306306

307307
if (expanded_prefix.empty() || string_starts_with(path_entry, expanded_prefix)) {
308-
std::string updated_line = cmd + path_entry;
308+
const std::string updated_line = cmd + path_entry;
309309
matches.emplace_back(updated_line + path_postfix, updated_line.length());
310310
}
311311

@@ -315,7 +315,7 @@ static std::vector<std::pair<std::string, size_t>> auto_completion_callback(std:
315315
}
316316

317317
if (matches.empty()) {
318-
std::string updated_line = cmd + path_prefix;
318+
const std::string updated_line = cmd + path_prefix;
319319
matches.emplace_back(updated_line + path_postfix, updated_line.length());
320320
}
321321

@@ -332,7 +332,7 @@ static std::vector<std::pair<std::string, size_t>> auto_completion_callback(std:
332332
len = std::min(len, static_cast<size_t>(cmp.first - match0.begin()));
333333
}
334334

335-
std::string updated_line = std::string(match0.substr(0, len));
335+
const std::string updated_line = std::string(match0.substr(0, len));
336336
matches.emplace_back(updated_line + path_postfix, updated_line.length());
337337
}
338338

@@ -569,10 +569,10 @@ int main(int argc, char ** argv) {
569569
if (endpath != std::string::npos) {
570570
std::string rel_pattern = pattern.substr(0, endpath);
571571
#if !defined(_WIN32)
572-
if (string_starts_with(rel_pattern, "~")) {
572+
if (string_starts_with(rel_pattern, '~')) {
573573
const char * home = std::getenv("HOME");
574574
if (home && home[0]) {
575-
rel_pattern = std::string(home) + rel_pattern.substr(1);
575+
rel_pattern = home + rel_pattern.substr(1);
576576
}
577577
}
578578
#endif

0 commit comments

Comments
 (0)