XtermSuggestionDisplay.cpp 7.4 KB

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