Shell: Tab completes every word of a command, and * and ? stand for several files (#67)
CI / build (pull_request) Successful in 1m38s
Site / build (pull_request) Successful in 9s

Tab used to complete a command's first word only. It now follows the help
text word by word: `lora st` gives `lora status`, `gnss track ` lists
`start  stop`. The words are read from the help text as written, so a new
command completes with no table to keep; the Shell's own words are added in
the same notation.

`*` and `?` in the last part of a path, for ls, du, rm, cp and mv, from the
Shell and both consoles. The command runs once for each name matched, lined
up and run by the main loop as each finishes; `cancel` empties the line-up.
64 matches at most, refused whole past that. In the Shell, rm with a pattern
asks once, with the count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-07 12:47:10 +02:00
co-authored by Claude Opus 5.5
parent d17d10948d
commit 0fdbb5b1ed
16 changed files with 368 additions and 112 deletions
+63 -40
View File
@@ -36,56 +36,79 @@ void ShellLog::clear() {
revision_++;
}
std::vector<std::string> commandWords(const char* helpText) {
std::vector<std::string> words;
std::string text = helpText ? helpText : "";
size_t lineStart = 0;
while (lineStart < text.size()) {
size_t lineEnd = text.find('\n', lineStart);
if (lineEnd == std::string::npos) lineEnd = text.size();
std::string line = text.substr(lineStart, lineEnd - lineStart);
// The commands stop where the description starts: three spaces.
size_t gap = line.find(" ");
std::string commands = line.substr(0, gap);
size_t at = 0;
while (at <= commands.size()) {
size_t bar = commands.find(" | ", at);
std::string one = commands.substr(at, bar == std::string::npos ? std::string::npos : bar - at);
size_t start = one.find_first_not_of(' ');
if (start != std::string::npos) {
size_t end = one.find(' ', start);
std::string word = one.substr(start, end == std::string::npos ? std::string::npos : end - start);
// Small letters, and for an App's name a capital first.
bool plain = !word.empty() && std::all_of(word.begin() + 1, word.end(), [](char ch) { return ch >= 'a' && ch <= 'z'; }) &&
((word[0] >= 'a' && word[0] <= 'z') || (word[0] >= 'A' && word[0] <= 'Z'));
if (plain && std::find(words.begin(), words.end(), word) == words.end()) words.push_back(word);
}
if (bar == std::string::npos) break;
at = bar + 3;
}
lineStart = lineEnd + 1;
namespace {
bool isWord(const std::string& w) {
if (w.empty()) return false;
for (size_t i = 0; i < w.size(); i++) {
bool small = w[i] >= 'a' && w[i] <= 'z', capital = w[i] >= 'A' && w[i] <= 'Z';
if (!(small || (i == 0 && capital))) return false;
}
return words;
return true;
}
std::string completeCommand(const std::string& typed, const std::vector<std::string>& words, std::vector<std::string>& matches) {
matches.clear();
if (typed.empty() || typed.find(' ') != std::string::npos) return typed;
for (auto& w : words)
if (w.rfind(typed, 0) == 0) matches.push_back(w);
if (matches.empty()) return typed;
if (matches.size() == 1) {
std::string only = matches[0] + " ";
matches.clear();
return only;
std::vector<std::string> split(const std::string& text, const std::string& by) {
std::vector<std::string> out;
size_t at = 0;
for (;;) {
size_t next = text.find(by, at);
out.push_back(text.substr(at, next == std::string::npos ? std::string::npos : next - at));
if (next == std::string::npos) return out;
at = next + by.size();
}
}
// The words a command can have at `index`, given the `index` words before it: added to `out`.
void nextWords(const std::string& command, const std::vector<std::string>& before, size_t index, std::vector<std::string>& out) {
std::vector<std::string> tokens;
for (auto& t : split(command, " "))
if (!t.empty()) tokens.push_back(t);
for (size_t i = 0; i < tokens.size(); i++) {
std::vector<std::string> either = split(tokens[i], "|"); // on|off: either of them
bool words = true;
for (auto& w : either) words = words && isWord(w);
if (!words) return; // an <argument>, an [option], "...": the command's words end here
if (i == index) {
for (auto& w : either)
if (std::find(out.begin(), out.end(), w) == out.end()) out.push_back(w);
return;
}
if (std::find(either.begin(), either.end(), before[i]) == either.end()) return; // another command
}
}
} // namespace
std::string completeWords(const std::string& typed, const char* helpText, std::vector<std::string>& matches) {
matches.clear();
std::vector<std::string> words = split(typed, " ");
for (size_t i = 0; i + 1 < words.size(); i++)
if (words[i].empty()) return typed; // two spaces: not ours to guess
const std::string last = words.back();
words.pop_back();
if (words.empty() && last.empty()) return typed;
std::vector<std::string> next;
for (auto& line : split(helpText ? helpText : "", "\n")) {
std::string commands = line.substr(0, line.find(" ")); // the description starts at three spaces
for (auto& command : split(commands, " | ")) nextWords(command, words, words.size(), next);
}
for (auto& w : next)
if (w.rfind(last, 0) == 0) matches.push_back(w);
if (matches.empty()) return typed;
std::string common = matches[0];
for (auto& m : matches) {
size_t n = 0;
while (n < common.size() && n < m.size() && common[n] == m[n]) n++;
common.resize(n);
}
return common;
std::string head = typed.substr(0, typed.size() - last.size());
if (matches.size() == 1) {
matches.clear();
return head + common + " ";
}
return head + common;
}
namespace {
+11 -8
View File
@@ -34,14 +34,17 @@ class ShellLog {
uint32_t revision_ = 0;
};
// The commands `help` lists: the first word of each (`ls [folder] | du <path>` gives ls and du),
// each once, in the order they appear.
std::vector<std::string> commandWords(const char* helpText);
// Tab: `typed` with its first word completed as far as the commands agree. Unchanged when the cursor
// is past the first word, or when nothing starts with it. `matches` gets what it could become, when
// there is more than one.
std::string completeCommand(const std::string& typed, const std::vector<std::string>& words, std::vector<std::string>& matches);
// Tab on a command (issue #67): every word of it, read from the firmware's `help` text each time it
// is asked, so nothing is kept in memory for it. A line of that text is commands, three spaces, then
// what they do; commands are separated by " | ", a word like on|off is either of them, and a command
// stops being words at its first <argument>, [option] or "...":
// lora rx on|off | lora preset <name> the radio
// gives "lora rx on", "lora rx off" and "lora preset". An App's name starts with a capital.
//
// The line with its last word completed as far as the commands that fit agree; a word completed
// whole gets a space after it. `matches` gets the candidates when there are several. Unchanged, with
// no matches, when no command goes on that way: then it may be a path (below).
std::string completeWords(const std::string& typed, const char* helpText, std::vector<std::string>& matches);
// Tab on a later word: a path on the SD card (issue #67). What is being completed, taken apart:
// `rm -r /notes/sh` is head "rm -r ", folder "/notes/", prefix "sh". False when the cursor is still
+1
View File
@@ -327,6 +327,7 @@ inline constexpr KeyHelp kNotesName[] = {
inline constexpr KeyHelp kShell[] = {
{"Enter", "run the line"},
{"Tab", "complete: a command, a path"},
{"* ?", "several files: /notes/*.txt"},
{"Fn ; .", "lines you typed before"},
{"Alt ; .", "scroll back, forward"},
{"Ctrl b", "your replies only, or all"},
+29
View File
@@ -115,4 +115,33 @@ RmArgs parseRm(const std::string& args) {
return out;
}
bool hasGlob(const std::string& text) { return text.find_first_of("*?") != std::string::npos; }
bool globMatch(const std::string& pattern, const std::string& name) {
auto lower = [](char c) { return c >= 'A' && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c; };
size_t p = 0, n = 0, star = std::string::npos, mark = 0;
while (n < name.size()) {
if (p < pattern.size() && (pattern[p] == '?' || lower(pattern[p]) == lower(name[n]))) {
p++;
n++;
} else if (p < pattern.size() && pattern[p] == '*') {
star = p++; // try it as nothing first; come back here to let it take one more
mark = n;
} else if (star != std::string::npos) {
p = star + 1;
n = ++mark;
} else return false;
}
while (p < pattern.size() && pattern[p] == '*') p++;
return p == pattern.size();
}
bool splitGlob(const std::string& path, std::string& folder, std::string& pattern) {
size_t slash = path.rfind('/');
if (slash == std::string::npos) return false;
pattern = path.substr(slash + 1);
folder = slash == 0 ? "/" : path.substr(0, slash);
return hasGlob(pattern) && !hasGlob(folder);
}
} // namespace roro::files
+8
View File
@@ -47,4 +47,12 @@ struct RmArgs {
};
RmArgs parseRm(const std::string& args);
// Patterns in a path (issue #67): * for any run of characters, ? for one, in the last part of the
// path only (/notes/*.txt, not /*/a.txt). Cases aren't told apart, as on the card.
bool hasGlob(const std::string& text);
bool globMatch(const std::string& pattern, const std::string& name);
// "/notes/*.txt" taken apart: the folder ("/notes", or "/" at the top) and the pattern ("*.txt").
// False if there's no pattern in it, or if the folder has one too.
bool splitGlob(const std::string& path, std::string& folder, std::string& pattern);
} // namespace roro::files