XtermSuggestionDisplay.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2020-2021, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/BinarySearch.h>
  27. #include <AK/Function.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibLine/SuggestionDisplay.h>
  30. #include <LibLine/VT.h>
  31. #include <stdio.h>
  32. namespace Line {
  33. void XtermSuggestionDisplay::display(const SuggestionManager& manager)
  34. {
  35. did_display();
  36. size_t longest_suggestion_length = 0;
  37. size_t longest_suggestion_byte_length = 0;
  38. manager.for_each_suggestion([&](auto& suggestion, auto) {
  39. longest_suggestion_length = max(longest_suggestion_length, suggestion.text_view.length());
  40. longest_suggestion_byte_length = max(longest_suggestion_byte_length, suggestion.text_string.length());
  41. return IterationDecision::Continue;
  42. });
  43. size_t num_printed = 0;
  44. size_t lines_used = 1;
  45. VT::save_cursor();
  46. VT::clear_lines(0, m_lines_used_for_last_suggestions);
  47. VT::restore_cursor();
  48. auto spans_entire_line { false };
  49. Vector<StringMetrics::LineMetrics> lines;
  50. for (size_t i = 0; i < m_prompt_lines_at_suggestion_initiation - 1; ++i)
  51. lines.append({ {}, 0 });
  52. lines.append({ {}, longest_suggestion_length });
  53. auto max_line_count = StringMetrics { move(lines) }.lines_with_addition({ { { {}, 0 } } }, m_num_columns);
  54. if (longest_suggestion_length >= m_num_columns - 2) {
  55. spans_entire_line = true;
  56. // We should make enough space for the biggest entry in
  57. // the suggestion list to fit in the prompt line.
  58. auto start = max_line_count - m_prompt_lines_at_suggestion_initiation;
  59. for (size_t i = start; i < max_line_count; ++i) {
  60. fputc('\n', stderr);
  61. }
  62. lines_used += max_line_count;
  63. longest_suggestion_length = 0;
  64. }
  65. VT::move_absolute(max_line_count + m_origin_row, 1);
  66. if (m_pages.is_empty()) {
  67. size_t num_printed = 0;
  68. size_t lines_used = 1;
  69. // Cache the pages.
  70. manager.set_start_index(0);
  71. size_t page_start = 0;
  72. manager.for_each_suggestion([&](auto& suggestion, auto index) {
  73. size_t next_column = num_printed + suggestion.text_view.length() + longest_suggestion_length + 2;
  74. if (next_column > m_num_columns) {
  75. auto lines = (suggestion.text_view.length() + m_num_columns - 1) / m_num_columns;
  76. lines_used += lines;
  77. num_printed = 0;
  78. }
  79. if (lines_used + m_prompt_lines_at_suggestion_initiation >= m_num_lines) {
  80. m_pages.append({ page_start, index });
  81. page_start = index;
  82. lines_used = 1;
  83. num_printed = 0;
  84. }
  85. if (spans_entire_line)
  86. num_printed += m_num_columns;
  87. else
  88. num_printed += longest_suggestion_length + 2;
  89. return IterationDecision::Continue;
  90. });
  91. // Append the last page.
  92. m_pages.append({ page_start, manager.count() });
  93. }
  94. auto page_index = fit_to_page_boundary(manager.next_index());
  95. manager.set_start_index(m_pages[page_index].start);
  96. manager.for_each_suggestion([&](auto& suggestion, auto index) {
  97. size_t next_column = num_printed + suggestion.text_view.length() + longest_suggestion_length + 2;
  98. if (next_column > m_num_columns) {
  99. auto lines = (suggestion.text_view.length() + m_num_columns - 1) / m_num_columns;
  100. lines_used += lines;
  101. fputc('\n', stderr);
  102. num_printed = 0;
  103. }
  104. // Show just enough suggestions to fill up the screen
  105. // without moving the prompt out of view.
  106. if (lines_used + m_prompt_lines_at_suggestion_initiation >= m_num_lines)
  107. return IterationDecision::Break;
  108. // Only apply color to the selection if something is *actually* added to the buffer.
  109. if (manager.is_current_suggestion_complete() && index == manager.next_index()) {
  110. VT::apply_style({ Style::Foreground(Style::XtermColor::Blue) });
  111. fflush(stderr);
  112. }
  113. if (spans_entire_line) {
  114. num_printed += m_num_columns;
  115. fprintf(stderr, "%s", suggestion.text_string.characters());
  116. } else {
  117. fprintf(stderr, "%-*s", static_cast<int>(longest_suggestion_byte_length) + 2, suggestion.text_string.characters());
  118. num_printed += longest_suggestion_length + 2;
  119. }
  120. if (manager.is_current_suggestion_complete() && index == manager.next_index()) {
  121. VT::apply_style(Style::reset_style());
  122. fflush(stderr);
  123. }
  124. return IterationDecision::Continue;
  125. });
  126. m_lines_used_for_last_suggestions = lines_used;
  127. // If we filled the screen, move back the origin.
  128. if (m_origin_row + lines_used >= m_num_lines) {
  129. m_origin_row = m_num_lines - lines_used;
  130. }
  131. if (m_pages.size() > 1) {
  132. auto left_arrow = page_index > 0 ? '<' : ' ';
  133. auto right_arrow = page_index < m_pages.size() - 1 ? '>' : ' ';
  134. auto string = String::format("%c page %zu of %zu %c", left_arrow, page_index + 1, m_pages.size(), right_arrow);
  135. if (string.length() > m_num_columns - 1) {
  136. // This would overflow into the next line, so just don't print an indicator.
  137. fflush(stderr);
  138. return;
  139. }
  140. VT::move_absolute(m_origin_row + lines_used, m_num_columns - string.length() - 1);
  141. VT::apply_style({ Style::Background(Style::XtermColor::Green) });
  142. fputs(string.characters(), stderr);
  143. VT::apply_style(Style::reset_style());
  144. }
  145. fflush(stderr);
  146. }
  147. bool XtermSuggestionDisplay::cleanup()
  148. {
  149. did_cleanup();
  150. if (m_lines_used_for_last_suggestions) {
  151. VT::clear_lines(0, m_lines_used_for_last_suggestions);
  152. m_lines_used_for_last_suggestions = 0;
  153. return true;
  154. }
  155. return false;
  156. }
  157. size_t XtermSuggestionDisplay::fit_to_page_boundary(size_t selection_index)
  158. {
  159. VERIFY(m_pages.size() > 0);
  160. size_t index = 0;
  161. auto* match = binary_search(
  162. m_pages.span(),
  163. PageRange { selection_index, selection_index },
  164. &index,
  165. [](auto& a, auto& b) -> int {
  166. if (a.start >= b.start && a.start < b.end)
  167. return 0;
  168. return a.start - b.start;
  169. });
  170. if (!match)
  171. return m_pages.size() - 1;
  172. return index;
  173. }
  174. }