From d3f089dc268931e670ea6a00ac8bcb06ed9822e6 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 26 Jul 2024 00:36:15 +0200 Subject: [PATCH] Utilities/js: Make it possible to exit via two consecutive ^C's Apparently this is common in the js repl world. Fixes #743. --- Userland/Libraries/LibLine/Editor.cpp | 4 ---- Userland/Libraries/LibLine/Editor.h | 2 ++ Userland/Libraries/LibLine/InternalFunctions.cpp | 4 ---- .../Libraries/LibLine/KeyCallbackMachine.cpp | 4 ---- Userland/Utilities/js.cpp | 16 ++++++++++++++++ 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp index a94f8e985fb..f6314585c3f 100644 --- a/Userland/Libraries/LibLine/Editor.cpp +++ b/Userland/Libraries/LibLine/Editor.cpp @@ -31,10 +31,6 @@ #include #include -namespace { -constexpr u32 ctrl(char c) { return c & 0x3f; } -} - namespace Line { Configuration Configuration::from_config(StringView libname) diff --git a/Userland/Libraries/LibLine/Editor.h b/Userland/Libraries/LibLine/Editor.h index 91158513be9..857783c6ab4 100644 --- a/Userland/Libraries/LibLine/Editor.h +++ b/Userland/Libraries/LibLine/Editor.h @@ -36,6 +36,8 @@ namespace Line { +static constexpr u32 ctrl(char c) { return c & 0x3f; } + struct KeyBinding { Vector keys; enum class Kind { diff --git a/Userland/Libraries/LibLine/InternalFunctions.cpp b/Userland/Libraries/LibLine/InternalFunctions.cpp index c86403a5762..94884fdb84d 100644 --- a/Userland/Libraries/LibLine/InternalFunctions.cpp +++ b/Userland/Libraries/LibLine/InternalFunctions.cpp @@ -15,10 +15,6 @@ #include #include -namespace { -constexpr u32 ctrl(char c) { return c & 0x3f; } -} - namespace Line { Function Editor::find_internal_function(StringView name) diff --git a/Userland/Libraries/LibLine/KeyCallbackMachine.cpp b/Userland/Libraries/LibLine/KeyCallbackMachine.cpp index 3cc4ed3c061..b76cf401d5b 100644 --- a/Userland/Libraries/LibLine/KeyCallbackMachine.cpp +++ b/Userland/Libraries/LibLine/KeyCallbackMachine.cpp @@ -7,10 +7,6 @@ #include #include -namespace { -constexpr u32 ctrl(char c) { return c & 0x3f; } -} - namespace Line { void KeyCallbackMachine::register_key_input_callback(Vector keys, Function callback) diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 0e76e219a7f..bea906da810 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -101,10 +101,13 @@ static ErrorOr print(JS::Value value, PrintTarget target = PrintTarget::St return print(value, *stream); } +static size_t s_ctrl_c_hit_count = 0; static ErrorOr prompt_for_level(int level) { static StringBuilder prompt_builder; prompt_builder.clear(); + if (s_ctrl_c_hit_count > 0) + prompt_builder.append("(Use Ctrl+C again to exit)\n"sv); prompt_builder.append("> "sv); for (auto i = 0; i < level; ++i) @@ -122,6 +125,7 @@ static ErrorOr read_next_piece() do { auto line_result = s_editor->get_line(TRY(prompt_for_level(s_repl_line_level)).to_byte_string()); + s_ctrl_c_hit_count = 0; line_level_delta_for_next_line = 0; if (line_result.is_error()) { @@ -600,6 +604,18 @@ ErrorOr serenity_main(Main::Arguments arguments) s_editor->save_history(s_history_path.to_byte_string()); }); + s_editor->register_key_input_callback(Line::ctrl('C'), [](Line::Editor& editor) -> bool { + if (editor.buffer_view().length() == 0 || s_ctrl_c_hit_count > 0) { + if (++s_ctrl_c_hit_count == 2) { + s_keep_running_repl = false; + editor.finish_edit(); + return false; + } + } + + return true; + }); + s_editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) { auto stylize = [&](Line::Span span, Line::Style styles) { if (syntax_highlight)