@@ -924,18 +924,26 @@ unsigned char Editline::BufferEndCommand(int ch) {
924924
925925// / Prints completions and their descriptions to the given file. Only the
926926// / completions in the interval [start, end) are printed.
927- static void
927+ static size_t
928928PrintCompletion (FILE *output_file,
929929 llvm::ArrayRef<CompletionResult::Completion> results,
930- size_t max_completion_length, size_t max_length) {
930+ size_t max_completion_length, size_t max_length,
931+ std::optional<size_t > max_height = std::nullopt ) {
931932 constexpr size_t ellipsis_length = 3 ;
932933 constexpr size_t padding_length = 8 ;
933934 constexpr size_t separator_length = 4 ;
934935
935936 const size_t description_col =
936937 std::min (max_completion_length + padding_length, max_length);
937938
939+ size_t lines_printed = 0 ;
940+ size_t results_printed = 0 ;
938941 for (const CompletionResult::Completion &c : results) {
942+ if (max_height && lines_printed == *max_height)
943+ break ;
944+
945+ results_printed++;
946+
939947 if (c.GetCompletion ().empty ())
940948 continue ;
941949
@@ -956,6 +964,7 @@ PrintCompletion(FILE *output_file,
956964 fprintf (output_file, " %.*s...\n " ,
957965 static_cast <int >(max_length - padding_length - ellipsis_length),
958966 c.GetCompletion ().c_str ());
967+ lines_printed++;
959968 continue ;
960969 }
961970
@@ -964,6 +973,7 @@ PrintCompletion(FILE *output_file,
964973 if (c.GetDescription ().empty () ||
965974 description_col + separator_length + ellipsis_length >= max_length) {
966975 fprintf (output_file, " \n " );
976+ lines_printed++;
967977 continue ;
968978 }
969979
@@ -1000,14 +1010,17 @@ PrintCompletion(FILE *output_file,
10001010 if (position + description_length < max_length) {
10011011 fprintf (output_file, " %.*s\n " , static_cast <int >(description_length),
10021012 line.data ());
1013+ lines_printed++;
10031014 } else {
10041015 fprintf (output_file, " %.*s...\n " ,
10051016 static_cast <int >(max_length - position - ellipsis_length),
10061017 line.data ());
1018+ lines_printed++;
10071019 continue ;
10081020 }
10091021 }
10101022 }
1023+ return results_printed;
10111024}
10121025
10131026void Editline::DisplayCompletions (
@@ -1016,7 +1029,11 @@ void Editline::DisplayCompletions(
10161029
10171030 fprintf (editline.m_output_file ,
10181031 " \n " ANSI_CLEAR_BELOW " Available completions:\n " );
1019- const size_t page_size = 40 ;
1032+
1033+ // / Account for the current line, the line showing "Available completions"
1034+ // / before and the line saying "More" after.
1035+ const size_t page_size = editline.GetTerminalHeight () - 3 ;
1036+
10201037 bool all = false ;
10211038
10221039 auto longest =
@@ -1026,21 +1043,15 @@ void Editline::DisplayCompletions(
10261043
10271044 const size_t max_len = longest->GetCompletion ().size ();
10281045
1029- if (results.size () < page_size) {
1030- PrintCompletion (editline.m_output_file , results, max_len,
1031- editline.GetTerminalWidth ());
1032- return ;
1033- }
1034-
10351046 size_t cur_pos = 0 ;
10361047 while (cur_pos < results.size ()) {
10371048 size_t remaining = results.size () - cur_pos;
10381049 size_t next_size = all ? remaining : std::min (page_size, remaining);
10391050
1040- PrintCompletion (editline. m_output_file , results. slice ( cur_pos, next_size),
1041- max_len, editline. GetTerminalWidth ());
1042-
1043- cur_pos += next_size ;
1051+ cur_pos += PrintCompletion (
1052+ editline. m_output_file , results. slice (cur_pos, next_size), max_len,
1053+ editline. GetTerminalWidth (),
1054+ all ? std:: nullopt : std::optional< size_t >(page_size)) ;
10441055
10451056 if (cur_pos >= results.size ())
10461057 break ;
@@ -1525,6 +1536,13 @@ void Editline::ApplyTerminalSizeChange() {
15251536 m_terminal_width = INT_MAX;
15261537 m_current_line_rows = 1 ;
15271538 }
1539+
1540+ int rows;
1541+ if (el_get (m_editline, EL_GETTC, " li" , &rows, nullptr ) == 0 ) {
1542+ m_terminal_height = rows;
1543+ } else {
1544+ m_terminal_height = INT_MAX;
1545+ }
15281546}
15291547
15301548const char *Editline::GetPrompt () { return m_set_prompt.c_str (); }
0 commit comments