diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-01-11 13:04:59 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-15 19:13:03 +0100 |
commit | 239472ba699c1e29f8dcb3380529637afeb95f21 (patch) | |
tree | 0c4c87c9063d160e130a53707299b14241a0781b /Userland/Shell/Shell.cpp | |
parent | 15fde85b214910e76677f86efaed22a4133ed1f6 (diff) | |
download | serenity-239472ba699c1e29f8dcb3380529637afeb95f21.zip |
Shell: Add (basic) support for history event designators
Closes #4888
Diffstat (limited to 'Userland/Shell/Shell.cpp')
-rw-r--r-- | Userland/Shell/Shell.cpp | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp index 34ca31c0f2..2fb4c86b65 100644 --- a/Userland/Shell/Shell.cpp +++ b/Userland/Shell/Shell.cpp @@ -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("'\\'"); + builder.append("\"'\""); + started_single_quote = false; + continue; + default: + builder.append(c); + if (!started_single_quote) { + started_single_quote = true; + builder.append("'"); + } break; + } + } + + 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(c); } + 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; } |