XtermSuggestionDisplay.cpp 6.3 KB

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