summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibLine/Editor.cpp
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-06-07 02:02:44 +0430
committerAli Mohammad Pur <ali.mpfard@gmail.com>2021-06-07 02:08:17 +0430
commit4d5cdcc89394259019828f123039104d6f4039e2 (patch)
tree84d10a028b5f03d952a6711dfaf2fb4ea8e905c6 /Userland/Libraries/LibLine/Editor.cpp
parent64754ba985d322219c13a16c0a1f2fb5e2dbfbd8 (diff)
downloadserenity-4d5cdcc89394259019828f123039104d6f4039e2.zip
LibLine: Partially revert d8c5eec and remove unrelated code
This is a partial revert of d8c5eeceabbeaedc29823f12cc8a9cfac6f84686 as it contained unrelated code that was committed accidentally, which broke history on LibLine.
Diffstat (limited to 'Userland/Libraries/LibLine/Editor.cpp')
-rw-r--r--Userland/Libraries/LibLine/Editor.cpp115
1 files changed, 58 insertions, 57 deletions
diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp
index 82171a7d02..1e328f2a41 100644
--- a/Userland/Libraries/LibLine/Editor.cpp
+++ b/Userland/Libraries/LibLine/Editor.cpp
@@ -588,8 +588,10 @@ void Editor::interrupted()
m_is_editing = false;
restore();
m_notifier->set_enabled(false);
- m_notifier = nullptr;
- Core::EventLoop::current().quit(Retry);
+ deferred_invoke([this](auto&) {
+ m_notifier = nullptr;
+ Core::EventLoop::current().quit(Retry);
+ });
}
void Editor::resized()
@@ -623,81 +625,80 @@ void Editor::really_quit_event_loop()
m_returned_line = string;
m_notifier->set_enabled(false);
- m_notifier = nullptr;
- Core::EventLoop::current().quit(Exit);
+ deferred_invoke([this](auto&) {
+ m_notifier = nullptr;
+ Core::EventLoop::current().quit(Exit);
+ });
}
auto Editor::get_line(const String& prompt) -> Result<String, Editor::Error>
{
- OwnPtr<Core::EventLoop> event_loop;
- do {
- initialize();
- m_is_editing = true;
-
- if (m_configuration.operation_mode == Configuration::NoEscapeSequences || m_configuration.operation_mode == Configuration::NonInteractive) {
- // Do not use escape sequences, instead, use LibC's getline.
- size_t size = 0;
- char* line = nullptr;
- // Show the prompt only on interactive mode (NoEscapeSequences in this case).
- if (m_configuration.operation_mode != Configuration::NonInteractive)
- fputs(prompt.characters(), stderr);
- auto line_length = getline(&line, &size, stdin);
- // getline() returns -1 and sets errno=0 on EOF.
- if (line_length == -1) {
- if (line)
- free(line);
- if (errno == 0)
- return Error::Eof;
-
- return Error::ReadFailure;
- }
- restore();
- if (line) {
- String result { line, (size_t)line_length, Chomp };
+ initialize();
+ m_is_editing = true;
+
+ if (m_configuration.operation_mode == Configuration::NoEscapeSequences || m_configuration.operation_mode == Configuration::NonInteractive) {
+ // Do not use escape sequences, instead, use LibC's getline.
+ size_t size = 0;
+ char* line = nullptr;
+ // Show the prompt only on interactive mode (NoEscapeSequences in this case).
+ if (m_configuration.operation_mode != Configuration::NonInteractive)
+ fputs(prompt.characters(), stderr);
+ auto line_length = getline(&line, &size, stdin);
+ // getline() returns -1 and sets errno=0 on EOF.
+ if (line_length == -1) {
+ if (line)
free(line);
- return result;
- }
+ if (errno == 0)
+ return Error::Eof;
return Error::ReadFailure;
}
+ restore();
+ if (line) {
+ String result { line, (size_t)line_length, Chomp };
+ free(line);
+ return result;
+ }
+
+ return Error::ReadFailure;
+ }
- auto old_cols = m_num_columns;
- auto old_lines = m_num_lines;
- get_terminal_size();
+ auto old_cols = m_num_columns;
+ auto old_lines = m_num_lines;
+ get_terminal_size();
- if (m_configuration.enable_bracketed_paste)
- fprintf(stderr, "\x1b[?2004h");
+ if (m_configuration.enable_bracketed_paste)
+ fprintf(stderr, "\x1b[?2004h");
- if (m_num_columns != old_cols || m_num_lines != old_lines)
- m_refresh_needed = true;
+ if (m_num_columns != old_cols || m_num_lines != old_lines)
+ m_refresh_needed = true;
- set_prompt(prompt);
- reset();
- strip_styles(true);
+ set_prompt(prompt);
+ reset();
+ strip_styles(true);
- auto prompt_lines = max(current_prompt_metrics().line_metrics.size(), 1ul) - 1;
- for (size_t i = 0; i < prompt_lines; ++i)
- putc('\n', stderr);
+ auto prompt_lines = max(current_prompt_metrics().line_metrics.size(), 1ul) - 1;
+ for (size_t i = 0; i < prompt_lines; ++i)
+ putc('\n', stderr);
- VT::move_relative(-prompt_lines, 0);
+ VT::move_relative(-prompt_lines, 0);
- set_origin();
+ set_origin();
- m_history_cursor = m_history.size();
+ m_history_cursor = m_history.size();
- refresh_display();
+ refresh_display();
- if (!event_loop)
- event_loop = make<Core::EventLoop>();
+ Core::EventLoop loop;
- if (!m_notifier) {
- m_notifier = Core::Notifier::construct(STDIN_FILENO, Core::Notifier::Read);
- }
+ m_notifier = Core::Notifier::construct(STDIN_FILENO, Core::Notifier::Read);
+
+ m_notifier->on_ready_to_read = [&] { try_update_once(); };
+ if (!m_incomplete_data.is_empty())
+ deferred_invoke([&](auto&) { try_update_once(); });
- m_notifier->on_ready_to_read = [&] { try_update_once(); };
- if (!m_incomplete_data.is_empty())
- deferred_invoke([&](auto&) { try_update_once(); });
- } while (event_loop->exec() == Retry);
+ if (loop.exec() == Retry)
+ return get_line(prompt);
return m_input_error.has_value() ? Result<String, Editor::Error> { m_input_error.value() } : Result<String, Editor::Error> { m_returned_line };
}