summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-04-29 01:46:19 +0430
committerAndreas Kling <kling@serenityos.org>2020-04-28 23:29:07 +0200
commit9473733d7aacf5fdde1bb608dbb5b6d23ad8506b (patch)
tree39585f8bb675c1d38efb5010d75ee20877b18c2a /Libraries
parent7ecf29f206eb8e471a9efc77b3356f4881fd635c (diff)
downloadserenity-9473733d7aacf5fdde1bb608dbb5b6d23ad8506b.zip
LibLine: Handle initialize() internally
This patch makes initialize() transparent to the users, but exposes it publicly, as the users might need a copy of the default termios (i.e. Shell)
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibLine/Editor.cpp5
-rw-r--r--Libraries/LibLine/Editor.h15
2 files changed, 15 insertions, 5 deletions
diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp
index 28b236716d..57ff046e09 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<Editor>(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 4569e95c79..815c184680 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<String>& 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;