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)
This commit is contained in:
AnotherTest 2020-04-29 01:46:19 +04:30 committed by Andreas Kling
parent 7ecf29f206
commit 9473733d7a
Notes: sideshowbarker 2024-07-19 07:12:29 +09:00
4 changed files with 15 additions and 8 deletions

View File

@ -186,8 +186,6 @@ int main(int argc, char** argv)
if (argc == 1) if (argc == 1)
return usage(); return usage();
editor.initialize();
StringBuilder command; StringBuilder command;
command.append(argv[1]); command.append(argv[1]);
for (int i = 2; i < argc; ++i) { for (int i = 2; i < argc; ++i) {

View File

@ -50,7 +50,7 @@ Editor::Editor(bool always_refresh)
Editor::~Editor() Editor::~Editor()
{ {
if (m_initialized) if (m_initialized)
tcsetattr(0, TCSANOW, &m_default_termios); restore();
} }
void Editor::add_to_history(const String& line) 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) String Editor::get_line(const String& prompt)
{ {
initialize();
m_is_editing = true; m_is_editing = true;
set_prompt(prompt); set_prompt(prompt);
@ -142,6 +143,7 @@ String Editor::get_line(const String& prompt)
auto string = String::copy(m_buffer); auto string = String::copy(m_buffer);
m_buffer.clear(); m_buffer.clear();
m_is_editing = false; m_is_editing = false;
restore();
return string; return string;
} }
char keybuf[16]; char keybuf[16];
@ -648,7 +650,6 @@ String Editor::get_line(const String& prompt)
m_pre_search_buffer.append(ch); m_pre_search_buffer.append(ch);
m_pre_search_cursor = m_cursor; m_pre_search_cursor = m_cursor;
m_search_editor = make<Editor>(true); // Has anyone seen 'Inception'? m_search_editor = make<Editor>(true); // Has anyone seen 'Inception'?
m_search_editor->initialize();
m_search_editor->on_display_refresh = [this](Editor& search_editor) { m_search_editor->on_display_refresh = [this](Editor& search_editor) {
search(StringView { search_editor.buffer().data(), search_editor.buffer().size() }); search(StringView { search_editor.buffer().data(), search_editor.buffer().size() });
refresh_display(); refresh_display();

View File

@ -80,9 +80,13 @@ public:
explicit Editor(bool always_refresh = false); explicit Editor(bool always_refresh = false);
~Editor(); ~Editor();
String get_line(const String& prompt);
void initialize() void initialize()
{ {
ASSERT(!m_initialized); if (m_initialized)
return;
struct termios termios; struct termios termios;
tcgetattr(0, &termios); tcgetattr(0, &termios);
m_default_termios = termios; // grab a copy to restore m_default_termios = termios; // grab a copy to restore
@ -94,8 +98,6 @@ public:
m_initialized = true; m_initialized = true;
} }
String get_line(const String& prompt);
void add_to_history(const String&); void add_to_history(const String&);
const Vector<String>& history() const { return m_history; } const Vector<String>& history() const { return m_history; }
@ -194,6 +196,13 @@ private:
void refresh_display(); void refresh_display();
void cleanup(); void cleanup();
void restore()
{
ASSERT(m_initialized);
tcsetattr(0, TCSANOW, &m_default_termios);
m_initialized = false;
}
size_t current_prompt_length() const size_t current_prompt_length() const
{ {
return m_cached_prompt_valid ? m_cached_prompt_length : m_old_prompt_length; return m_cached_prompt_valid ? m_cached_prompt_length : m_old_prompt_length;

View File

@ -430,7 +430,6 @@ int main(int argc, char** argv)
s_editor->resized(); s_editor->resized();
}); });
s_editor->initialize();
s_editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) { s_editor->on_display_refresh = [syntax_highlight](Line::Editor& editor) {
auto stylize = [&](Line::Span span, Line::Style styles) { auto stylize = [&](Line::Span span, Line::Style styles) {
if (syntax_highlight) if (syntax_highlight)