From fa03a2848f70fadd91a4eb0aa10cf19a044b33a1 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Wed, 16 Sep 2020 05:18:33 +0430 Subject: [PATCH] Shell: Add live formatting and take an option to enable it This patchset makes it possible for the shell to format the current buffer of the line editor live, with somewhat accurate cursor tracking. Since this feature is pretty goofy at best, let's keep it off by default for now :^) --- Shell/Shell.h | 5 +++++ Shell/main.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Shell/Shell.h b/Shell/Shell.h index 7ca6f580aa..fb8e410e36 100644 --- a/Shell/Shell.h +++ b/Shell/Shell.h @@ -73,6 +73,9 @@ public: constexpr static auto local_init_file_path = "~/.shellrc"; constexpr static auto global_init_file_path = "/etc/shellrc"; + bool should_format_live() const { return m_should_format_live; } + void set_live_formatting(bool value) { m_should_format_live = value; } + void setup_signals(); int run_command(const StringView&); @@ -243,6 +246,8 @@ private: HashMap m_aliases; bool m_is_interactive { true }; bool m_is_subshell { false }; + + bool m_should_format_live { false }; }; static constexpr bool is_word_character(char c) diff --git a/Shell/main.cpp b/Shell/main.cpp index f605730531..6572e317b2 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -190,6 +190,14 @@ int main(int argc, char** argv) editor->on_display_refresh = [&](auto& editor) { editor.strip_styles(); + if (shell->should_format_live()) { + auto line = editor.line(); + ssize_t cursor = editor.cursor(); + editor.clear_line(); + editor.insert(shell->format(line, cursor)); + if (cursor >= 0) + editor.set_cursor(cursor); + } shell->highlight(editor); }; editor->on_tab_complete = [&](const Line::Editor& editor) { @@ -201,16 +209,20 @@ int main(int argc, char** argv) Vector script_args; bool skip_rc_files = false; const char* format = nullptr; + bool should_format_live = false; Core::ArgsParser parser; parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string"); parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0); parser.add_option(format, "File to format", "format", 0, "file"); + parser.add_option(should_format_live, "Enable live formatting", "live-formatting", 'f'); parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No); parser.add_positional_argument(script_args, "Extra argumets to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No); parser.parse(argc, argv); + shell->set_live_formatting(should_format_live); + if (format) { auto file = Core::File::open(format, Core::IODevice::ReadOnly); if (file.is_error()) { @@ -228,6 +240,7 @@ int main(int argc, char** argv) perror("setsid"); // Let's just hope that it's ok. } + } shell->current_script = argv[0];