1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +00:00

Shell: Add (basic) support for history event designators

Closes #4888
This commit is contained in:
AnotherTest 2021-01-11 13:04:59 +03:30 committed by Andreas Kling
parent 15fde85b21
commit 239472ba69
9 changed files with 457 additions and 19 deletions

View file

@ -1099,19 +1099,57 @@ String Shell::get_history_path()
String Shell::escape_token_for_single_quotes(const String& token)
{
// `foo bar \n '` -> `'foo bar \n '"'"`
StringBuilder builder;
builder.append("'");
auto started_single_quote = true;
for (auto c : token) {
switch (c) {
case '\'':
builder.append("'\\'");
break;
builder.append("\"'\"");
started_single_quote = false;
continue;
default:
builder.append(c);
if (!started_single_quote) {
started_single_quote = true;
builder.append("'");
}
break;
}
builder.append(c);
}
if (started_single_quote)
builder.append("'");
return builder.build();
}
String Shell::escape_token_for_double_quotes(const String& token)
{
// `foo bar \n $x 'blah "hello` -> `"foo bar \\n $x 'blah \"hello"`
StringBuilder builder;
builder.append('"');
for (auto c : token) {
switch (c) {
case '\"':
builder.append("\\\"");
continue;
case '\\':
builder.append("\\\\");
continue;
default:
builder.append(c);
break;
}
}
builder.append('"');
return builder.build();
}
@ -1499,6 +1537,22 @@ void Shell::bring_cursor_to_beginning_of_a_line() const
putc('\r', stderr);
}
bool Shell::has_history_event(StringView source)
{
struct : public AST::NodeVisitor {
virtual void visit(const AST::HistoryEvent* node)
{
has_history_event = true;
AST::NodeVisitor::visit(node);
}
bool has_history_event { false };
} visitor;
Parser { source }.parse()->visit(visitor);
return visitor.has_history_event;
}
bool Shell::read_single_line()
{
restore_ios();
@ -1523,7 +1577,9 @@ bool Shell::read_single_line()
run_command(line);
m_editor->add_to_history(line);
if (!has_history_event(line))
m_editor->add_to_history(line);
return true;
}