Serenity Operating System
at master 195 lines 7.7 kB view raw
1/* 2 * Copyright (c) 2020-2021, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/BinarySearch.h> 8#include <AK/Function.h> 9#include <AK/StringBuilder.h> 10#include <LibCore/File.h> 11#include <LibLine/SuggestionDisplay.h> 12#include <LibLine/VT.h> 13#include <stdio.h> 14 15namespace Line { 16 17ErrorOr<void> XtermSuggestionDisplay::display(SuggestionManager const& manager) 18{ 19 did_display(); 20 21 auto stderr_stream = TRY(Core::File::standard_error()); 22 23 size_t longest_suggestion_length = 0; 24 size_t longest_suggestion_byte_length = 0; 25 size_t longest_suggestion_byte_length_without_trivia = 0; 26 27 manager.set_start_index(0); 28 TRY(manager.for_each_suggestion([&](auto& suggestion, auto) { 29 longest_suggestion_length = max(longest_suggestion_length, suggestion.text_view.length() + suggestion.display_trivia_view.length()); 30 longest_suggestion_byte_length = max(longest_suggestion_byte_length, suggestion.text_string.length() + suggestion.display_trivia_string.length()); 31 longest_suggestion_byte_length_without_trivia = max(longest_suggestion_byte_length_without_trivia, suggestion.text_string.length()); 32 return IterationDecision::Continue; 33 })); 34 35 size_t num_printed = 0; 36 size_t lines_used = 1; 37 38 TRY(VT::save_cursor(*stderr_stream)); 39 TRY(VT::clear_lines(0, m_lines_used_for_last_suggestions, *stderr_stream)); 40 TRY(VT::restore_cursor(*stderr_stream)); 41 42 auto spans_entire_line { false }; 43 Vector<StringMetrics::LineMetrics> lines; 44 for (size_t i = 0; i < m_prompt_lines_at_suggestion_initiation - 1; ++i) 45 lines.append({ {}, 0 }); 46 lines.append({ {}, longest_suggestion_length }); 47 auto max_line_count = StringMetrics { move(lines) }.lines_with_addition({ { { {}, 0 } } }, m_num_columns); 48 if (longest_suggestion_length >= m_num_columns - 2) { 49 spans_entire_line = true; 50 // We should make enough space for the biggest entry in 51 // the suggestion list to fit in the prompt line. 52 auto start = max_line_count - m_prompt_lines_at_suggestion_initiation; 53 for (size_t i = start; i < max_line_count; ++i) 54 TRY(stderr_stream->write_until_depleted("\n"sv.bytes())); 55 lines_used += max_line_count; 56 longest_suggestion_length = 0; 57 } 58 59 TRY(VT::move_absolute(max_line_count + m_origin_row, 1, *stderr_stream)); 60 61 if (m_pages.is_empty()) { 62 size_t num_printed = 0; 63 size_t lines_used = 1; 64 // Cache the pages. 65 manager.set_start_index(0); 66 size_t page_start = 0; 67 TRY(manager.for_each_suggestion([&](auto& suggestion, auto index) { 68 size_t next_column = num_printed + suggestion.text_view.length() + longest_suggestion_length + 2; 69 if (next_column > m_num_columns) { 70 auto lines = (suggestion.text_view.length() + m_num_columns - 1) / m_num_columns; 71 lines_used += lines; 72 num_printed = 0; 73 } 74 75 if (lines_used + m_prompt_lines_at_suggestion_initiation >= m_num_lines) { 76 m_pages.append({ page_start, index }); 77 page_start = index; 78 lines_used = 1; 79 num_printed = 0; 80 } 81 82 if (spans_entire_line) 83 num_printed += m_num_columns; 84 else 85 num_printed += longest_suggestion_length + 2; 86 87 return IterationDecision::Continue; 88 })); 89 // Append the last page. 90 m_pages.append({ page_start, manager.count() }); 91 } 92 93 auto page_index = fit_to_page_boundary(manager.next_index()); 94 95 manager.set_start_index(m_pages[page_index].start); 96 TRY(manager.for_each_suggestion([&](auto& suggestion, auto index) -> ErrorOr<IterationDecision> { 97 size_t next_column = num_printed + suggestion.text_view.length() + longest_suggestion_length + 2; 98 99 if (next_column > m_num_columns) { 100 auto lines = (suggestion.text_view.length() + m_num_columns - 1) / m_num_columns; 101 lines_used += lines; 102 TRY(stderr_stream->write_until_depleted("\n"sv.bytes())); 103 num_printed = 0; 104 } 105 106 // Show just enough suggestions to fill up the screen 107 // without moving the prompt out of view. 108 if (lines_used + m_prompt_lines_at_suggestion_initiation >= m_num_lines) 109 return IterationDecision::Break; 110 111 // Only apply color to the selection if something is *actually* added to the buffer. 112 if (manager.is_current_suggestion_complete() && index == manager.next_index()) { 113 TRY(VT::apply_style({ Style::Foreground(Style::XtermColor::Blue) }, *stderr_stream)); 114 } 115 116 if (spans_entire_line) { 117 num_printed += m_num_columns; 118 TRY(stderr_stream->write_until_depleted(suggestion.text_string.bytes())); 119 TRY(stderr_stream->write_until_depleted(suggestion.display_trivia_string.bytes())); 120 } else { 121 auto field = DeprecatedString::formatted("{: <{}} {}", suggestion.text_string, longest_suggestion_byte_length_without_trivia, suggestion.display_trivia_string); 122 TRY(stderr_stream->write_until_depleted(DeprecatedString::formatted("{: <{}}", field, longest_suggestion_byte_length + 2).bytes())); 123 num_printed += longest_suggestion_length + 2; 124 } 125 126 if (manager.is_current_suggestion_complete() && index == manager.next_index()) 127 TRY(VT::apply_style(Style::reset_style(), *stderr_stream)); 128 return IterationDecision::Continue; 129 })); 130 131 m_lines_used_for_last_suggestions = lines_used; 132 133 // The last line of the prompt is the same line as the first line of the buffer, so we need to subtract one here. 134 lines_used += m_prompt_lines_at_suggestion_initiation - 1; 135 136 // If we filled the screen, move back the origin. 137 if (m_origin_row + lines_used >= m_num_lines) { 138 m_origin_row = m_num_lines - lines_used; 139 } 140 141 if (m_pages.size() > 1) { 142 auto left_arrow = page_index > 0 ? '<' : ' '; 143 auto right_arrow = page_index < m_pages.size() - 1 ? '>' : ' '; 144 auto string = DeprecatedString::formatted("{:c} page {} of {} {:c}", left_arrow, page_index + 1, m_pages.size(), right_arrow); 145 146 if (string.length() > m_num_columns - 1) { 147 // This would overflow into the next line, so just don't print an indicator. 148 return {}; 149 } 150 151 TRY(VT::move_absolute(m_origin_row + lines_used, m_num_columns - string.length() - 1, *stderr_stream)); 152 TRY(VT::apply_style({ Style::Background(Style::XtermColor::Green) }, *stderr_stream)); 153 TRY(stderr_stream->write_until_depleted(string.bytes())); 154 TRY(VT::apply_style(Style::reset_style(), *stderr_stream)); 155 } 156 157 return {}; 158} 159 160ErrorOr<bool> XtermSuggestionDisplay::cleanup() 161{ 162 did_cleanup(); 163 164 if (m_lines_used_for_last_suggestions) { 165 auto stderr_stream = TRY(Core::File::standard_error()); 166 TRY(VT::clear_lines(0, m_lines_used_for_last_suggestions, *stderr_stream)); 167 m_lines_used_for_last_suggestions = 0; 168 return true; 169 } 170 171 return false; 172} 173 174size_t XtermSuggestionDisplay::fit_to_page_boundary(size_t selection_index) 175{ 176 VERIFY(m_pages.size() > 0); 177 size_t index = 0; 178 179 auto* match = binary_search( 180 m_pages.span(), 181 PageRange { selection_index, selection_index }, 182 &index, 183 [](auto& a, auto& b) -> int { 184 if (a.start >= b.start && a.start < b.end) 185 return 0; 186 return a.start - b.start; 187 }); 188 189 if (!match) 190 return m_pages.size() - 1; 191 192 return index; 193} 194 195}