mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:17:44 +00:00
LibLine: Use Core::EventLoop for outer read loop
This commit changes LibLine's internal structure to work in an event loop, and as a result, also switches it to being a Core::Object.
This commit is contained in:
parent
8e6df3949d
commit
70a213a6ec
7 changed files with 583 additions and 517 deletions
|
@ -55,7 +55,7 @@
|
|||
// if we want to be more sh-like, we should do that some day
|
||||
static constexpr bool HighlightVariablesInsideStrings = false;
|
||||
static bool s_disable_hyperlinks = false;
|
||||
extern Line::Editor editor;
|
||||
extern RefPtr<Line::Editor> editor;
|
||||
|
||||
//#define SH_DEBUG
|
||||
|
||||
|
@ -126,7 +126,7 @@ String Shell::prompt() const
|
|||
};
|
||||
|
||||
auto the_prompt = build_prompt();
|
||||
auto prompt_length = editor.actual_rendered_string_length(the_prompt);
|
||||
auto prompt_length = editor->actual_rendered_string_length(the_prompt);
|
||||
|
||||
if (m_should_continue != ExitCodeOrContinuationRequest::Nothing) {
|
||||
const auto format_string = "\033[34m%.*-s\033[m";
|
||||
|
@ -511,8 +511,8 @@ int Shell::builtin_disown(int argc, const char** argv)
|
|||
|
||||
int Shell::builtin_history(int, const char**)
|
||||
{
|
||||
for (size_t i = 0; i < editor.history().size(); ++i) {
|
||||
printf("%6zu %s\n", i, editor.history()[i].characters());
|
||||
for (size_t i = 0; i < editor->history().size(); ++i) {
|
||||
printf("%6zu %s\n", i, editor->history()[i].characters());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -1385,7 +1385,7 @@ void Shell::load_history()
|
|||
while (history_file->can_read_line()) {
|
||||
auto b = history_file->read_line(1024);
|
||||
// skip the newline and terminating bytes
|
||||
editor.add_to_history(String(reinterpret_cast<const char*>(b.data()), b.size() - 2));
|
||||
editor->add_to_history(String(reinterpret_cast<const char*>(b.data()), b.size() - 2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1395,7 +1395,7 @@ void Shell::save_history()
|
|||
if (file_or_error.is_error())
|
||||
return;
|
||||
auto& file = *file_or_error.value();
|
||||
for (const auto& line : editor.history()) {
|
||||
for (const auto& line : editor->history()) {
|
||||
file.write(line);
|
||||
file.write("\n");
|
||||
}
|
||||
|
@ -1484,7 +1484,7 @@ void Shell::cache_path()
|
|||
quick_sort(cached_path);
|
||||
}
|
||||
|
||||
void Shell::highlight(Line::Editor&) const
|
||||
void Shell::highlight(Line::Editor& editor) const
|
||||
{
|
||||
StringBuilder builder;
|
||||
if (m_should_continue == ExitCodeOrContinuationRequest::DoubleQuotedString) {
|
||||
|
@ -1703,7 +1703,7 @@ Vector<Line::CompletionSuggestion> Shell::complete(const Line::Editor& editor)
|
|||
|
||||
bool Shell::read_single_line()
|
||||
{
|
||||
auto line_result = editor.get_line(prompt());
|
||||
auto line_result = editor->get_line(prompt());
|
||||
|
||||
if (line_result.is_error()) {
|
||||
m_complete_line_builder.clear();
|
||||
|
@ -1737,7 +1737,7 @@ bool Shell::read_single_line()
|
|||
if (!complete_or_exit_code.has_value())
|
||||
return true;
|
||||
|
||||
editor.add_to_history(m_complete_line_builder.build());
|
||||
editor->add_to_history(m_complete_line_builder.build());
|
||||
m_complete_line_builder.clear();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
Line::Editor editor { Line::Configuration { Line::Configuration::UnescapedSpaces } };
|
||||
RefPtr<Line::Editor> editor;
|
||||
Shell* s_shell;
|
||||
|
||||
void FileDescriptionCollector::collect()
|
||||
|
@ -58,13 +58,13 @@ void FileDescriptionCollector::add(int fd)
|
|||
int main(int argc, char** argv)
|
||||
{
|
||||
Core::EventLoop loop;
|
||||
|
||||
|
||||
signal(SIGINT, [](int) {
|
||||
editor.interrupted();
|
||||
editor->interrupted();
|
||||
});
|
||||
|
||||
signal(SIGWINCH, [](int) {
|
||||
editor.resized();
|
||||
editor->resized();
|
||||
});
|
||||
|
||||
signal(SIGHUP, [](int) {
|
||||
|
@ -92,18 +92,20 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
editor = Line::Editor::construct(Line::Configuration { Line::Configuration::UnescapedSpaces });
|
||||
|
||||
auto shell = Shell::construct();
|
||||
s_shell = shell.ptr();
|
||||
|
||||
editor.initialize();
|
||||
shell->termios = editor.termios();
|
||||
shell->default_termios = editor.default_termios();
|
||||
editor->initialize();
|
||||
shell->termios = editor->termios();
|
||||
shell->default_termios = editor->default_termios();
|
||||
|
||||
editor.on_display_refresh = [&](auto& editor) {
|
||||
editor->on_display_refresh = [&](auto& editor) {
|
||||
editor.strip_styles();
|
||||
shell->highlight(editor);
|
||||
};
|
||||
editor.on_tab_complete = [&](const Line::Editor& editor) {
|
||||
editor->on_tab_complete = [&](const Line::Editor& editor) {
|
||||
return shell->complete(editor);
|
||||
};
|
||||
|
||||
|
@ -128,13 +130,15 @@ int main(int argc, char** argv)
|
|||
return 0;
|
||||
}
|
||||
|
||||
editor.on_interrupt_handled = [&] {
|
||||
editor->on_interrupt_handled = [&] {
|
||||
if (!shell->should_read_more()) {
|
||||
shell->finish_command();
|
||||
editor.finish();
|
||||
editor->finish();
|
||||
}
|
||||
};
|
||||
|
||||
shell->add_child(*editor);
|
||||
|
||||
Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::ShellEventType::ReadLine));
|
||||
|
||||
return loop.exec();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue