XtermSuggestionDisplay.cpp 7.5 KB

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