diff --git a/Applications/Debugger/main.cpp b/Applications/Debugger/main.cpp index 52c1abd1e35..3a9fca85ba9 100644 --- a/Applications/Debugger/main.cpp +++ b/Applications/Debugger/main.cpp @@ -186,8 +186,6 @@ int main(int argc, char** argv) if (argc == 1) return usage(); - editor.initialize(); - StringBuilder command; command.append(argv[1]); for (int i = 2; i < argc; ++i) { diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp index 28b236716d1..57ff046e095 100644 --- a/Libraries/LibLine/Editor.cpp +++ b/Libraries/LibLine/Editor.cpp @@ -50,7 +50,7 @@ Editor::Editor(bool always_refresh) Editor::~Editor() { if (m_initialized) - tcsetattr(0, TCSANOW, &m_default_termios); + restore(); } void Editor::add_to_history(const String& line) @@ -124,6 +124,7 @@ void Editor::stylize(const Span& span, const Style& style) String Editor::get_line(const String& prompt) { + initialize(); m_is_editing = true; set_prompt(prompt); @@ -142,6 +143,7 @@ String Editor::get_line(const String& prompt) auto string = String::copy(m_buffer); m_buffer.clear(); m_is_editing = false; + restore(); return string; } char keybuf[16]; @@ -648,7 +650,6 @@ String Editor::get_line(const String& prompt) m_pre_search_buffer.append(ch); m_pre_search_cursor = m_cursor; m_search_editor = make(true); // Has anyone seen 'Inception'? - m_search_editor->initialize(); m_search_editor->on_display_refresh = [this](Editor& search_editor) { search(StringView { search_editor.buffer().data(), search_editor.buffer().size() }); refresh_display(); diff --git a/Libraries/LibLine/Editor.h b/Libraries/LibLine/Editor.h index 4569e95c790..815c1846806 100644 --- a/Libraries/LibLine/Editor.h +++ b/Libraries/LibLine/Editor.h @@ -80,9 +80,13 @@ public: explicit Editor(bool always_refresh = false); ~Editor(); + String get_line(const String& prompt); + void initialize() { - ASSERT(!m_initialized); + if (m_initialized) + return; + struct termios termios; tcgetattr(0, &termios); m_default_termios = termios; // grab a copy to restore @@ -94,8 +98,6 @@ public: m_initialized = true; } - String get_line(const String& prompt); - void add_to_history(const String&); const Vector& history() const { return m_history; } @@ -194,6 +196,13 @@ private: void refresh_display(); void cleanup(); + void restore() + { + ASSERT(m_initialized); + tcsetattr(0, TCSANOW, &m_default_termios); + m_initialized = false; + } + size_t current_prompt_length() const { return m_cached_prompt_valid ? m_cached_prompt_length : m_old_prompt_length; diff --git a/Userland/js.cpp b/Userland/js.cpp index b20e12a4cf9..e2a4ed489c6 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -430,7 +430,6 @@ int main(int argc, char** argv) s_editor->resized(); }); - s_editor->initialize(); s_editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) { auto stylize = [&](Line::Span span, Line::Style styles) { if (syntax_highlight)