1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:27:43 +00:00

Browser: Add console history to re-send old commands

The console has now enabled history in its input text box. Pretty
nice to not have to retype things all the time :^)
This commit is contained in:
FalseHonesty 2020-05-26 22:18:25 -04:00 committed by Andreas Kling
parent 139dbfd5b5
commit 08e2907210
2 changed files with 24 additions and 20 deletions

View file

@ -54,26 +54,29 @@ ConsoleWidget::ConsoleWidget()
html_element->append_child(head_element); html_element->append_child(head_element);
auto body_element = create_element(base_document, "body"); auto body_element = create_element(base_document, "body");
html_element->append_child(body_element); html_element->append_child(body_element);
m_console_output_container = body_element; m_output_container = body_element;
m_console_output_view = add<Web::HtmlView>(); m_output_view = add<Web::HtmlView>();
m_console_output_view->set_document(base_document); m_output_view->set_document(base_document);
m_console_input = add<GUI::TextBox>(); m_input = add<GUI::TextBox>();
m_console_input->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>()); m_input->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
m_console_input->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); m_input->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_console_input->set_preferred_size(0, 22); m_input->set_preferred_size(0, 22);
// FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts. // FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
m_console_input->set_font(Gfx::Font::default_fixed_width_font()); m_input->set_font(Gfx::Font::default_fixed_width_font());
m_input->set_history_enabled(true);
m_console_input->on_return_pressed = [this] { m_input->on_return_pressed = [this] {
auto js_source = m_console_input->text(); auto js_source = m_input->text();
// FIXME: An is_blank check to check if there is only whitespace would probably be preferable. // FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
if (js_source.is_empty()) if (js_source.is_empty())
return; return;
m_console_input->clear(); m_input->add_current_text_to_history();
m_input->clear();
print_source_line(js_source); print_source_line(js_source);
auto parser = JS::Parser(JS::Lexer(js_source)); auto parser = JS::Parser(JS::Lexer(js_source));
@ -133,19 +136,19 @@ void ConsoleWidget::print_source_line(const StringView& source)
void ConsoleWidget::print_html(const StringView& line) void ConsoleWidget::print_html(const StringView& line)
{ {
auto paragraph = create_element(m_console_output_container->document(), "p"); auto paragraph = create_element(m_output_container->document(), "p");
paragraph->set_inner_html(line); paragraph->set_inner_html(line);
m_console_output_container->append_child(paragraph); m_output_container->append_child(paragraph);
m_console_output_container->document().invalidate_layout(); m_output_container->document().invalidate_layout();
m_console_output_container->document().update_layout(); m_output_container->document().update_layout();
m_console_output_view->scroll_to_bottom(); m_output_view->scroll_to_bottom();
} }
void ConsoleWidget::clear_output() void ConsoleWidget::clear_output()
{ {
const_cast<Web::HTMLBodyElement*>(m_console_output_view->document()->body())->remove_all_children(); const_cast<Web::HTMLBodyElement*>(m_output_view->document()->body())->remove_all_children();
} }
} }

View file

@ -26,6 +26,7 @@
#pragma once #pragma once
#include "BrowserConsoleClient.h" #include "BrowserConsoleClient.h"
#include "History.h"
#include <LibGUI/Widget.h> #include <LibGUI/Widget.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibWeb/HtmlView.h> #include <LibWeb/HtmlView.h>
@ -45,9 +46,9 @@ public:
private: private:
ConsoleWidget(); ConsoleWidget();
RefPtr<GUI::TextBox> m_console_input; RefPtr<GUI::TextBox> m_input;
RefPtr<Web::HtmlView> m_console_output_view; RefPtr<Web::HtmlView> m_output_view;
RefPtr<Web::Element> m_console_output_container; RefPtr<Web::Element> m_output_container;
WeakPtr<JS::Interpreter> m_interpreter; WeakPtr<JS::Interpreter> m_interpreter;
OwnPtr<BrowserConsoleClient> m_console_client; OwnPtr<BrowserConsoleClient> m_console_client;
}; };