1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:47:34 +00:00

LibLine: Port most functions to Core::Stream

This commit is contained in:
Tim Schumacher 2023-01-10 12:21:17 +01:00 committed by Sam Atkins
parent 0ab29cffd2
commit f9f1e1dd49
5 changed files with 102 additions and 101 deletions

View file

@ -5,7 +5,6 @@
*/
#include <AK/BinarySearch.h>
#include <AK/FileStream.h>
#include <AK/Function.h>
#include <AK/StringBuilder.h>
#include <LibLine/SuggestionDisplay.h>
@ -18,7 +17,7 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
{
did_display();
OutputFileStream stderr_stream { stderr };
auto stderr_stream = Core::Stream::File::standard_error().release_value_but_fixme_should_propagate_errors();
size_t longest_suggestion_length = 0;
size_t longest_suggestion_byte_length = 0;
@ -35,9 +34,9 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
size_t num_printed = 0;
size_t lines_used = 1;
VT::save_cursor(stderr_stream);
VT::clear_lines(0, m_lines_used_for_last_suggestions, stderr_stream);
VT::restore_cursor(stderr_stream);
VT::save_cursor(*stderr_stream);
VT::clear_lines(0, m_lines_used_for_last_suggestions, *stderr_stream);
VT::restore_cursor(*stderr_stream);
auto spans_entire_line { false };
Vector<StringMetrics::LineMetrics> lines;
@ -51,12 +50,12 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
// the suggestion list to fit in the prompt line.
auto start = max_line_count - m_prompt_lines_at_suggestion_initiation;
for (size_t i = start; i < max_line_count; ++i)
stderr_stream.write("\n"sv.bytes());
stderr_stream->write("\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
lines_used += max_line_count;
longest_suggestion_length = 0;
}
VT::move_absolute(max_line_count + m_origin_row, 1, stderr_stream);
VT::move_absolute(max_line_count + m_origin_row, 1, *stderr_stream);
if (m_pages.is_empty()) {
size_t num_printed = 0;
@ -99,7 +98,7 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
if (next_column > m_num_columns) {
auto lines = (suggestion.text_view.length() + m_num_columns - 1) / m_num_columns;
lines_used += lines;
stderr_stream.write("\n"sv.bytes());
stderr_stream->write("\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
num_printed = 0;
}
@ -110,21 +109,21 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
// Only apply color to the selection if something is *actually* added to the buffer.
if (manager.is_current_suggestion_complete() && index == manager.next_index()) {
VT::apply_style({ Style::Foreground(Style::XtermColor::Blue) }, stderr_stream);
VT::apply_style({ Style::Foreground(Style::XtermColor::Blue) }, *stderr_stream);
}
if (spans_entire_line) {
num_printed += m_num_columns;
stderr_stream.write(suggestion.text_string.bytes());
stderr_stream.write(suggestion.display_trivia_string.bytes());
stderr_stream->write(suggestion.text_string.bytes()).release_value_but_fixme_should_propagate_errors();
stderr_stream->write(suggestion.display_trivia_string.bytes()).release_value_but_fixme_should_propagate_errors();
} else {
auto field = DeprecatedString::formatted("{: <{}} {}", suggestion.text_string, longest_suggestion_byte_length_without_trivia, suggestion.display_trivia_string);
stderr_stream.write(DeprecatedString::formatted("{: <{}}", field, longest_suggestion_byte_length + 2).bytes());
stderr_stream->write(DeprecatedString::formatted("{: <{}}", field, longest_suggestion_byte_length + 2).bytes()).release_value_but_fixme_should_propagate_errors();
num_printed += longest_suggestion_length + 2;
}
if (manager.is_current_suggestion_complete() && index == manager.next_index())
VT::apply_style(Style::reset_style(), stderr_stream);
VT::apply_style(Style::reset_style(), *stderr_stream);
return IterationDecision::Continue;
});
@ -148,10 +147,10 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
return;
}
VT::move_absolute(m_origin_row + lines_used, m_num_columns - string.length() - 1, stderr_stream);
VT::apply_style({ Style::Background(Style::XtermColor::Green) }, stderr_stream);
stderr_stream.write(string.bytes());
VT::apply_style(Style::reset_style(), stderr_stream);
VT::move_absolute(m_origin_row + lines_used, m_num_columns - string.length() - 1, *stderr_stream);
VT::apply_style({ Style::Background(Style::XtermColor::Green) }, *stderr_stream);
stderr_stream->write(string.bytes()).release_value_but_fixme_should_propagate_errors();
VT::apply_style(Style::reset_style(), *stderr_stream);
}
}
@ -160,8 +159,8 @@ bool XtermSuggestionDisplay::cleanup()
did_cleanup();
if (m_lines_used_for_last_suggestions) {
OutputFileStream stderr_stream { stderr };
VT::clear_lines(0, m_lines_used_for_last_suggestions, stderr_stream);
auto stderr_stream = Core::Stream::File::standard_error().release_value_but_fixme_should_propagate_errors();
VT::clear_lines(0, m_lines_used_for_last_suggestions, *stderr_stream);
m_lines_used_for_last_suggestions = 0;
return true;
}