1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-21 15:55:07 +00:00

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 :^)
This commit is contained in:
AnotherTest 2020-09-16 05:18:33 +04:30 committed by Andreas Kling
parent e94ee08eca
commit fa03a2848f
2 changed files with 18 additions and 0 deletions

View file

@ -73,6 +73,9 @@ public:
constexpr static auto local_init_file_path = "~/.shellrc"; constexpr static auto local_init_file_path = "~/.shellrc";
constexpr static auto global_init_file_path = "/etc/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(); void setup_signals();
int run_command(const StringView&); int run_command(const StringView&);
@ -243,6 +246,8 @@ private:
HashMap<String, String> m_aliases; HashMap<String, String> m_aliases;
bool m_is_interactive { true }; bool m_is_interactive { true };
bool m_is_subshell { false }; bool m_is_subshell { false };
bool m_should_format_live { false };
}; };
static constexpr bool is_word_character(char c) static constexpr bool is_word_character(char c)

View file

@ -190,6 +190,14 @@ int main(int argc, char** argv)
editor->on_display_refresh = [&](auto& editor) { editor->on_display_refresh = [&](auto& editor) {
editor.strip_styles(); 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); shell->highlight(editor);
}; };
editor->on_tab_complete = [&](const Line::Editor& editor) { editor->on_tab_complete = [&](const Line::Editor& editor) {
@ -201,16 +209,20 @@ int main(int argc, char** argv)
Vector<const char*> script_args; Vector<const char*> script_args;
bool skip_rc_files = false; bool skip_rc_files = false;
const char* format = nullptr; const char* format = nullptr;
bool should_format_live = false;
Core::ArgsParser parser; Core::ArgsParser parser;
parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string"); 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(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
parser.add_option(format, "File to format", "format", 0, "file"); 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(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.add_positional_argument(script_args, "Extra argumets to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
parser.parse(argc, argv); parser.parse(argc, argv);
shell->set_live_formatting(should_format_live);
if (format) { if (format) {
auto file = Core::File::open(format, Core::IODevice::ReadOnly); auto file = Core::File::open(format, Core::IODevice::ReadOnly);
if (file.is_error()) { if (file.is_error()) {
@ -228,6 +240,7 @@ int main(int argc, char** argv)
perror("setsid"); perror("setsid");
// Let's just hope that it's ok. // Let's just hope that it's ok.
} }
}
shell->current_script = argv[0]; shell->current_script = argv[0];