2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
// IWYU pragma: no_include <cstddef>
|
2016-05-03 02:54:01 +08:00
|
|
|
#include <stddef.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <wctype.h>
|
2017-02-11 10:47:02 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
2019-10-14 06:50:48 +08:00
|
|
|
#include <cwchar>
|
2016-12-03 15:00:06 +08:00
|
|
|
#include <numeric>
|
2019-03-04 09:34:00 +08:00
|
|
|
#include <type_traits>
|
2017-08-20 00:55:06 +08:00
|
|
|
#include <unordered_map>
|
2016-05-03 02:54:01 +08:00
|
|
|
#include <vector>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
2016-06-24 13:44:58 +08:00
|
|
|
#include "fallback.h"
|
2020-08-24 06:12:47 +08:00
|
|
|
#include "flog.h"
|
2016-05-03 02:54:01 +08:00
|
|
|
#include "highlight.h"
|
|
|
|
#include "pager.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "reader.h"
|
2016-05-03 02:54:01 +08:00
|
|
|
#include "screen.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2019-11-26 08:56:39 +08:00
|
|
|
using comp_t = pager_t::comp_t;
|
|
|
|
using comp_info_list_t = std::vector<comp_t>;
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// The minimum width (in characters) the terminal must to show completions at all.
|
2013-12-02 07:11:25 +08:00
|
|
|
#define PAGER_MIN_WIDTH 16
|
|
|
|
|
2016-12-04 05:35:24 +08:00
|
|
|
/// Minimum height to show completions
|
|
|
|
#define PAGER_MIN_HEIGHT 4
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// The maximum number of columns of completion to attempt to fit onto the screen.
|
2013-12-02 07:11:25 +08:00
|
|
|
#define PAGER_MAX_COLS 6
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Width of the search field.
|
2014-01-27 16:56:13 +08:00
|
|
|
#define PAGER_SEARCH_FIELD_WIDTH 12
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Text we use for the search field.
|
2014-01-27 16:56:13 +08:00
|
|
|
#define SEARCH_FIELD_PROMPT _(L"search: ")
|
|
|
|
|
2019-04-29 05:06:03 +08:00
|
|
|
inline bool selection_direction_is_cardinal(selection_motion_t dir) {
|
2017-02-14 12:37:27 +08:00
|
|
|
switch (dir) {
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::north:
|
|
|
|
case selection_motion_t::east:
|
|
|
|
case selection_motion_t::south:
|
|
|
|
case selection_motion_t::west:
|
|
|
|
case selection_motion_t::page_north:
|
|
|
|
case selection_motion_t::page_south: {
|
2017-02-14 12:37:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::next:
|
|
|
|
case selection_motion_t::prev:
|
|
|
|
case selection_motion_t::deselect: {
|
2017-02-14 12:37:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 07:21:35 +08:00
|
|
|
|
2019-04-29 05:06:03 +08:00
|
|
|
DIE("unreachable");
|
2017-02-14 12:37:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Returns numer / denom, rounding up. As a "courtesy" 0/0 is 0.
|
|
|
|
static size_t divide_round_up(size_t numer, size_t denom) {
|
|
|
|
if (numer == 0) return 0;
|
2014-01-27 16:56:13 +08:00
|
|
|
assert(denom > 0);
|
2017-02-11 10:47:02 +08:00
|
|
|
bool has_rem = (numer % denom) != 0;
|
2014-10-31 13:40:35 +08:00
|
|
|
return numer / denom + (has_rem ? 1 : 0);
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Print the specified string, but use at most the specified amount of space. If the whole string
|
|
|
|
/// can't be fitted, ellipsize it.
|
|
|
|
///
|
|
|
|
/// \param str the string to print
|
|
|
|
/// \param color the color to apply to every printed character
|
|
|
|
/// \param max the maximum space that may be used for printing
|
|
|
|
/// \param has_more if this flag is true, this is not the entire string, and the string should be
|
2016-12-03 18:40:07 +08:00
|
|
|
/// ellipsized even if the string fits but takes up the whole space.
|
2016-12-04 05:38:50 +08:00
|
|
|
static size_t print_max(const wcstring &str, highlight_spec_t color, size_t max, bool has_more,
|
|
|
|
line_t *line) {
|
2016-12-03 18:40:07 +08:00
|
|
|
size_t remaining = max;
|
2016-05-03 02:54:01 +08:00
|
|
|
for (size_t i = 0; i < str.size(); i++) {
|
2014-01-14 08:41:22 +08:00
|
|
|
wchar_t c = str.at(i);
|
2016-12-03 18:40:07 +08:00
|
|
|
int iwidth_c = fish_wcwidth(c);
|
|
|
|
if (iwidth_c < 0) {
|
|
|
|
// skip non-printable characters
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-03 07:04:04 +08:00
|
|
|
auto width_c = size_t(iwidth_c);
|
2016-12-03 18:40:07 +08:00
|
|
|
|
|
|
|
if (width_c > remaining) break;
|
2014-01-14 08:41:22 +08:00
|
|
|
|
2019-04-29 06:00:36 +08:00
|
|
|
wchar_t ellipsis = get_ellipsis_char();
|
2016-12-03 18:40:07 +08:00
|
|
|
if ((width_c == remaining) && (has_more || i + 1 < str.size())) {
|
2019-04-29 06:00:36 +08:00
|
|
|
line->append(ellipsis, color);
|
|
|
|
int ellipsis_width = fish_wcwidth(ellipsis);
|
2016-12-03 18:40:07 +08:00
|
|
|
remaining -= std::min(remaining, size_t(ellipsis_width));
|
2014-01-14 08:41:22 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-14 08:41:22 +08:00
|
|
|
line->append(c, color);
|
2016-12-03 18:40:07 +08:00
|
|
|
assert(remaining >= width_c);
|
|
|
|
remaining -= width_c;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
2016-12-03 18:40:07 +08:00
|
|
|
|
|
|
|
// return how much we consumed
|
|
|
|
assert(remaining <= max);
|
|
|
|
return max - remaining;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Print the specified item using at the specified amount of space.
|
|
|
|
line_t pager_t::completion_print_item(const wcstring &prefix, const comp_t *c, size_t row,
|
2016-12-03 18:40:07 +08:00
|
|
|
size_t column, size_t width, bool secondary, bool selected,
|
2016-05-03 02:54:01 +08:00
|
|
|
page_rendering_t *rendering) const {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(column);
|
|
|
|
UNUSED(row);
|
|
|
|
UNUSED(rendering);
|
2019-01-17 05:38:27 +08:00
|
|
|
size_t comp_width;
|
2014-01-14 08:41:22 +08:00
|
|
|
line_t line_data;
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-12-03 11:24:54 +08:00
|
|
|
if (c->preferred_width() <= width) {
|
2016-05-03 02:54:01 +08:00
|
|
|
// The entry fits, we give it as much space as it wants.
|
2013-12-02 07:11:25 +08:00
|
|
|
comp_width = c->comp_width;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
|
|
|
// The completion and description won't fit on the allocated space. Give a maximum of 2/3 of
|
2016-12-03 18:40:07 +08:00
|
|
|
// the space to the completion, and whatever is left to the description
|
|
|
|
// This expression is an overflow-safe way of calculating (width-4)*2/3
|
|
|
|
size_t width_minus_spacer = width - std::min(width, size_t(4));
|
2016-12-04 05:38:50 +08:00
|
|
|
size_t two_thirds_width = (width_minus_spacer / 3) * 2 + ((width_minus_spacer % 3) * 2) / 3;
|
2016-12-03 18:40:07 +08:00
|
|
|
comp_width = std::min(c->comp_width, two_thirds_width);
|
|
|
|
|
|
|
|
// If the description is short, give the completion the remaining space
|
|
|
|
size_t desc_punct_width = c->description_punctuated_width();
|
|
|
|
if (width > desc_punct_width) {
|
|
|
|
comp_width = std::max(comp_width, width - desc_punct_width);
|
|
|
|
}
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-12-03 18:40:07 +08:00
|
|
|
// The description gets what's left
|
|
|
|
assert(comp_width <= width);
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2019-03-04 09:34:00 +08:00
|
|
|
auto modify_role = [=](highlight_role_t role) -> highlight_role_t {
|
|
|
|
using uint_t = typename std::underlying_type<highlight_role_t>::type;
|
2020-04-03 07:04:04 +08:00
|
|
|
auto base = static_cast<uint_t>(role);
|
2019-03-04 09:34:00 +08:00
|
|
|
if (selected) {
|
|
|
|
base += static_cast<uint_t>(highlight_role_t::pager_selected_background) -
|
|
|
|
static_cast<uint_t>(highlight_role_t::pager_background);
|
|
|
|
} else if (secondary) {
|
|
|
|
base += static_cast<uint_t>(highlight_role_t::pager_secondary_background) -
|
|
|
|
static_cast<uint_t>(highlight_role_t::pager_background);
|
|
|
|
}
|
|
|
|
return static_cast<highlight_role_t>(base);
|
|
|
|
};
|
|
|
|
|
|
|
|
highlight_role_t bg_role = modify_role(highlight_role_t::pager_background);
|
|
|
|
highlight_spec_t bg = {highlight_role_t::normal, bg_role};
|
|
|
|
highlight_spec_t prefix_col = {modify_role(highlight_role_t::pager_prefix), bg_role};
|
|
|
|
highlight_spec_t comp_col = {modify_role(highlight_role_t::pager_completion), bg_role};
|
|
|
|
highlight_spec_t desc_col = {modify_role(highlight_role_t::pager_description), bg_role};
|
|
|
|
|
2016-12-03 18:40:07 +08:00
|
|
|
// Print the completion part
|
|
|
|
size_t comp_remaining = comp_width;
|
2016-05-03 02:54:01 +08:00
|
|
|
for (size_t i = 0; i < c->comp.size(); i++) {
|
2013-12-02 07:11:25 +08:00
|
|
|
const wcstring &comp = c->comp.at(i);
|
2018-03-31 18:41:16 +08:00
|
|
|
|
2016-12-03 18:40:07 +08:00
|
|
|
if (i > 0) {
|
2019-05-05 18:09:25 +08:00
|
|
|
comp_remaining -=
|
|
|
|
print_max(PAGER_SPACER_STRING, bg, comp_remaining, true /* has_more */, &line_data);
|
2016-12-03 18:40:07 +08:00
|
|
|
}
|
2014-01-14 08:41:22 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
comp_remaining -= print_max(prefix, prefix_col, comp_remaining, !comp.empty(), &line_data);
|
2016-12-04 05:38:50 +08:00
|
|
|
comp_remaining -=
|
2019-01-13 15:16:30 +08:00
|
|
|
print_max(comp, comp_col, comp_remaining, i + 1 < c->comp.size(), &line_data);
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
|
|
|
|
2016-12-03 18:40:07 +08:00
|
|
|
size_t desc_remaining = width - comp_width + comp_remaining;
|
|
|
|
if (c->desc_width > 0 && desc_remaining > 4) {
|
|
|
|
// always have at least two spaces to separate completion and description
|
2019-01-13 15:16:30 +08:00
|
|
|
desc_remaining -= print_max(L" ", bg, 2, false, &line_data);
|
2016-12-03 18:40:07 +08:00
|
|
|
|
|
|
|
// right-justify the description by adding spaces
|
|
|
|
// the 2 here refers to the parenthesis below
|
|
|
|
while (desc_remaining > c->desc_width + 2) {
|
2019-01-13 15:16:30 +08:00
|
|
|
desc_remaining -= print_max(L" ", bg, 1, false, &line_data);
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
2016-12-03 18:40:07 +08:00
|
|
|
|
|
|
|
assert(desc_remaining >= 2);
|
2019-03-04 09:34:00 +08:00
|
|
|
highlight_spec_t paren_col = {highlight_role_t::pager_completion, bg_role};
|
2019-01-13 15:16:30 +08:00
|
|
|
desc_remaining -= print_max(L"(", paren_col, 1, false, &line_data);
|
|
|
|
desc_remaining -= print_max(c->desc, desc_col, desc_remaining - 1, false, &line_data);
|
|
|
|
desc_remaining -= print_max(L")", paren_col, 1, false, &line_data);
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2016-12-03 18:40:07 +08:00
|
|
|
// No description, or it won't fit. Just add spaces.
|
2018-03-31 18:41:16 +08:00
|
|
|
print_max(wcstring(desc_remaining, L' '), bg, desc_remaining, false, &line_data);
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
2014-01-15 07:39:53 +08:00
|
|
|
|
2014-01-14 08:41:22 +08:00
|
|
|
return line_data;
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Print the specified part of the completion list, using the specified column offsets and quoting
|
|
|
|
/// style.
|
|
|
|
///
|
|
|
|
/// \param cols number of columns to print in
|
2016-12-03 15:29:14 +08:00
|
|
|
/// \param width_by_column An array specifying the width of each column
|
2016-05-03 02:54:01 +08:00
|
|
|
/// \param row_start The first row to print
|
|
|
|
/// \param row_stop the row after the last row to print
|
|
|
|
/// \param prefix The string to print before each completion
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param lst The list of completions to print
|
2016-12-03 15:29:14 +08:00
|
|
|
void pager_t::completion_print(size_t cols, const size_t *width_by_column, size_t row_start,
|
2016-05-03 02:54:01 +08:00
|
|
|
size_t row_stop, const wcstring &prefix, const comp_info_list_t &lst,
|
|
|
|
page_rendering_t *rendering) const {
|
|
|
|
// Teach the rendering about the rows it printed.
|
2014-01-22 06:35:18 +08:00
|
|
|
assert(row_stop >= row_start);
|
|
|
|
rendering->row_start = row_start;
|
|
|
|
rendering->row_end = row_stop;
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-12-03 15:29:14 +08:00
|
|
|
size_t rows = divide_round_up(lst.size(), cols);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-20 08:41:26 +08:00
|
|
|
size_t effective_selected_idx = this->visual_selected_completion_index(rows, cols);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
for (size_t row = row_start; row < row_stop; row++) {
|
|
|
|
for (size_t col = 0; col < cols; col++) {
|
|
|
|
if (lst.size() <= col * rows + row) continue;
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2014-01-16 10:21:38 +08:00
|
|
|
size_t idx = col * rows + row;
|
|
|
|
const comp_t *el = &lst.at(idx);
|
2014-01-19 04:42:53 +08:00
|
|
|
bool is_selected = (idx == effective_selected_idx);
|
2014-01-14 08:41:22 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Print this completion on its own "line".
|
2016-12-04 05:38:50 +08:00
|
|
|
line_t line = completion_print_item(prefix, el, row, col, width_by_column[col], row % 2,
|
|
|
|
is_selected, rendering);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// If there's more to come, append two spaces.
|
|
|
|
if (col + 1 < cols) {
|
2019-03-04 09:34:00 +08:00
|
|
|
line.append(PAGER_SPACER_STRING, highlight_spec_t{});
|
2014-01-15 07:39:53 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Append this to the real line.
|
2014-01-22 06:35:18 +08:00
|
|
|
rendering->screen_data.create_line(row - row_start).append_line(line);
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Trim leading and trailing whitespace, and compress other whitespace runs into a single space.
|
|
|
|
static void mangle_1_completion_description(wcstring *str) {
|
2014-01-14 08:41:22 +08:00
|
|
|
size_t leading = 0, trailing = 0, len = str->size();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Skip leading spaces.
|
|
|
|
for (; leading < len; leading++) {
|
|
|
|
if (!iswspace(str->at(leading))) break;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Compress runs of spaces to a single space.
|
2014-01-14 08:41:22 +08:00
|
|
|
bool was_space = false;
|
2016-05-03 02:54:01 +08:00
|
|
|
for (; leading < len; leading++) {
|
2014-01-14 08:41:22 +08:00
|
|
|
wchar_t wc = str->at(leading);
|
|
|
|
bool is_space = iswspace(wc);
|
2016-05-03 02:54:01 +08:00
|
|
|
if (!is_space) { // normal character
|
2014-01-14 08:41:22 +08:00
|
|
|
str->at(trailing++) = wc;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else if (!was_space) { // initial space in a run
|
2014-01-14 08:41:22 +08:00
|
|
|
str->at(trailing++) = L' ';
|
2016-11-03 05:17:26 +08:00
|
|
|
} // else non-initial space in a run, do nothing
|
2014-01-14 08:41:22 +08:00
|
|
|
was_space = is_space;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// leading is now at len, trailing is the new length of the string. Delete trailing spaces.
|
|
|
|
while (trailing > 0 && iswspace(str->at(trailing - 1))) {
|
2014-01-14 08:41:22 +08:00
|
|
|
trailing--;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-14 08:41:22 +08:00
|
|
|
str->resize(trailing);
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
static void join_completions(comp_info_list_t *comps) {
|
|
|
|
// A map from description to index in the completion list of the element with that description.
|
|
|
|
// The indexes are stored +1.
|
2017-08-20 07:27:24 +08:00
|
|
|
std::unordered_map<wcstring, size_t> desc_table;
|
2014-01-14 08:41:22 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Note that we mutate the completion list as we go, so the size changes.
|
|
|
|
for (size_t i = 0; i < comps->size(); i++) {
|
2014-01-14 08:41:22 +08:00
|
|
|
const comp_t &new_comp = comps->at(i);
|
|
|
|
const wcstring &desc = new_comp.desc;
|
2016-05-03 02:54:01 +08:00
|
|
|
if (desc.empty()) continue;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// See if it's in the table.
|
2014-01-14 08:41:22 +08:00
|
|
|
size_t prev_idx_plus_one = desc_table[desc];
|
2016-05-03 02:54:01 +08:00
|
|
|
if (prev_idx_plus_one == 0) {
|
|
|
|
// We're the first with this description.
|
|
|
|
desc_table[desc] = i + 1;
|
|
|
|
} else {
|
2014-01-14 08:41:22 +08:00
|
|
|
// There's a prior completion with this description. Append the new ones to it.
|
|
|
|
comp_t *prior_comp = &comps->at(prev_idx_plus_one - 1);
|
2016-05-03 02:54:01 +08:00
|
|
|
prior_comp->comp.insert(prior_comp->comp.end(), new_comp.comp.begin(),
|
|
|
|
new_comp.comp.end());
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Erase the element at this index, and decrement the index to reflect that fact.
|
2014-01-14 08:41:22 +08:00
|
|
|
comps->erase(comps->begin() + i);
|
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Generate a list of comp_t structures from a list of completions.
|
2016-10-10 05:38:26 +08:00
|
|
|
static comp_info_list_t process_completions_into_infos(const completion_list_t &lst) {
|
2014-01-14 08:41:22 +08:00
|
|
|
const size_t lst_size = lst.size();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Make the list of the correct size up-front.
|
2014-01-14 08:41:22 +08:00
|
|
|
comp_info_list_t result(lst_size);
|
2016-05-03 02:54:01 +08:00
|
|
|
for (size_t i = 0; i < lst_size; i++) {
|
2014-01-14 08:41:22 +08:00
|
|
|
const completion_t &comp = lst.at(i);
|
|
|
|
comp_t *comp_info = &result.at(i);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-14 08:41:22 +08:00
|
|
|
// Append the single completion string. We may later merge these into multiple.
|
2018-08-12 18:37:13 +08:00
|
|
|
comp_info->comp.push_back(escape_string(comp.completion, ESCAPE_NO_QUOTED));
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Append the mangled description.
|
2014-01-14 08:41:22 +08:00
|
|
|
comp_info->desc = comp.description;
|
|
|
|
mangle_1_completion_description(&comp_info->desc);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Set the representative completion.
|
2014-01-25 07:59:18 +08:00
|
|
|
comp_info->representative = comp;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::measure_completion_infos(comp_info_list_t *infos, const wcstring &prefix) const {
|
2018-10-20 23:57:39 +08:00
|
|
|
size_t prefix_len = fish_wcswidth(prefix);
|
2019-11-20 05:46:47 +08:00
|
|
|
for (auto &info : *infos) {
|
|
|
|
comp_t *comp = &info;
|
2014-01-14 08:41:22 +08:00
|
|
|
const wcstring_list_t &comp_strings = comp->comp;
|
2017-07-05 04:03:25 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
for (size_t j = 0; j < comp_strings.size(); j++) {
|
|
|
|
// If there's more than one, append the length of ', '.
|
|
|
|
if (j >= 1) comp->comp_width += 2;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2017-07-05 04:03:25 +08:00
|
|
|
// fish_wcswidth() can return -1 if it can't calculate the width. So be cautious.
|
2018-10-20 23:57:39 +08:00
|
|
|
int comp_width = fish_wcswidth(comp_strings.at(j));
|
2017-09-24 04:06:44 +08:00
|
|
|
if (comp_width >= 0) comp->comp_width += prefix_len + comp_width;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2017-07-05 04:03:25 +08:00
|
|
|
// fish_wcswidth() can return -1 if it can't calculate the width. So be cautious.
|
2018-10-20 23:57:39 +08:00
|
|
|
int desc_width = fish_wcswidth(comp->desc);
|
2017-07-05 04:03:25 +08:00
|
|
|
comp->desc_width = desc_width > 0 ? desc_width : 0;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Indicates if the given completion info passes any filtering we have.
|
|
|
|
bool pager_t::completion_info_passes_filter(const comp_t &info) const {
|
|
|
|
// If we have no filter, everything passes.
|
|
|
|
if (!search_field_shown || this->search_field_line.empty()) return true;
|
2014-01-27 16:56:13 +08:00
|
|
|
|
2020-02-04 19:47:44 +08:00
|
|
|
const wcstring &needle = this->search_field_line.text();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2018-09-27 19:07:54 +08:00
|
|
|
// We do full fuzzy matching just like the completion code itself.
|
|
|
|
const fuzzy_match_type_t limit = fuzzy_match_none;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Match against the description.
|
|
|
|
if (string_fuzzy_match_string(needle, info.desc, limit).type != fuzzy_match_none) {
|
2014-01-27 16:56:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Match against the completion strings.
|
2019-11-20 05:46:47 +08:00
|
|
|
for (const auto &i : info.comp) {
|
|
|
|
if (string_fuzzy_match_string(needle, prefix + i, limit).type != fuzzy_match_none) {
|
2014-01-27 16:56:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
return false; // no match
|
2014-01-27 16:56:13 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Update completion_infos from unfiltered_completion_infos, to reflect the filter.
|
|
|
|
void pager_t::refilter_completions() {
|
2014-01-27 16:56:13 +08:00
|
|
|
this->completion_infos.clear();
|
2019-11-20 05:46:47 +08:00
|
|
|
for (const auto &info : this->unfiltered_completion_infos) {
|
2016-05-03 02:54:01 +08:00
|
|
|
if (this->completion_info_passes_filter(info)) {
|
2014-01-27 16:56:13 +08:00
|
|
|
this->completion_infos.push_back(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::set_completions(const completion_list_t &raw_completions) {
|
|
|
|
// Get completion infos out of it.
|
2016-10-10 05:38:26 +08:00
|
|
|
unfiltered_completion_infos = process_completions_into_infos(raw_completions);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Maybe join them.
|
|
|
|
if (prefix == L"-") join_completions(&unfiltered_completion_infos);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Compute their various widths.
|
2014-01-27 16:56:13 +08:00
|
|
|
measure_completion_infos(&unfiltered_completion_infos, prefix);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Refilter them.
|
2014-01-27 16:56:13 +08:00
|
|
|
this->refilter_completions();
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::set_prefix(const wcstring &pref) { prefix = pref; }
|
2014-01-16 10:21:38 +08:00
|
|
|
|
2020-06-08 09:59:15 +08:00
|
|
|
void pager_t::set_term_size(termsize_t ts) {
|
|
|
|
available_term_width = ts.width > 0 ? ts.width : 0;
|
|
|
|
available_term_height = ts.height > 0 ? ts.height : 0;
|
2014-01-14 08:41:22 +08:00
|
|
|
}
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-12-03 15:00:06 +08:00
|
|
|
/// Try to print the list of completions lst with the prefix prefix using cols as the number of
|
2016-10-30 12:20:29 +08:00
|
|
|
/// columns. Return true if the completion list was printed, false if the terminal is too narrow for
|
2016-05-03 02:54:01 +08:00
|
|
|
/// the specified number of columns. Always succeeds if cols is 1.
|
|
|
|
bool pager_t::completion_try_print(size_t cols, const wcstring &prefix, const comp_info_list_t &lst,
|
|
|
|
page_rendering_t *rendering, size_t suggested_start_row) const {
|
2016-12-03 15:29:14 +08:00
|
|
|
assert(cols > 0);
|
2016-05-03 02:54:01 +08:00
|
|
|
// The calculated preferred width of each column.
|
2016-12-03 15:29:14 +08:00
|
|
|
size_t width_by_column[PAGER_MAX_COLS] = {0};
|
2016-12-03 18:40:07 +08:00
|
|
|
|
2016-12-04 05:35:24 +08:00
|
|
|
// Skip completions on tiny terminals.
|
2016-12-04 05:38:50 +08:00
|
|
|
if (this->available_term_width < PAGER_MIN_WIDTH ||
|
|
|
|
this->available_term_height < PAGER_MIN_HEIGHT)
|
|
|
|
return true;
|
2016-12-04 05:35:24 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Compute the effective term width and term height, accounting for disclosure.
|
2016-10-10 05:36:08 +08:00
|
|
|
size_t term_width = this->available_term_width;
|
2016-12-04 05:38:50 +08:00
|
|
|
size_t term_height =
|
|
|
|
this->available_term_height - 1 -
|
|
|
|
(search_field_shown ? 1 : 0); // we always subtract 1 to make room for a comment row
|
2016-05-03 02:54:01 +08:00
|
|
|
if (!this->fully_disclosed) {
|
2019-11-19 09:08:16 +08:00
|
|
|
term_height = std::min(term_height, static_cast<size_t>(PAGER_UNDISCLOSED_MAX_ROWS));
|
2014-02-21 05:30:26 +08:00
|
|
|
}
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2014-01-22 06:35:18 +08:00
|
|
|
size_t row_count = divide_round_up(lst.size(), cols);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// We have more to disclose if we are not fully disclosed and there's more rows than we have in
|
|
|
|
// our term height.
|
|
|
|
if (!this->fully_disclosed && row_count > term_height) {
|
2014-01-23 09:45:27 +08:00
|
|
|
rendering->remaining_to_disclose = row_count - term_height;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-23 09:45:27 +08:00
|
|
|
rendering->remaining_to_disclose = 0;
|
2014-08-12 00:55:07 +08:00
|
|
|
}
|
2016-05-03 02:54:01 +08:00
|
|
|
|
|
|
|
// If we have only one row remaining to disclose, then squelch the comment row. This prevents us
|
|
|
|
// from consuming a line to show "...and 1 more row".
|
2016-12-03 15:00:06 +08:00
|
|
|
if (rendering->remaining_to_disclose == 1) {
|
2014-08-12 00:55:07 +08:00
|
|
|
term_height += 1;
|
|
|
|
rendering->remaining_to_disclose = 0;
|
2014-01-23 09:45:27 +08:00
|
|
|
}
|
2013-12-02 07:11:25 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Calculate how wide the list would be.
|
2016-10-10 05:36:08 +08:00
|
|
|
for (size_t col = 0; col < cols; col++) {
|
|
|
|
for (size_t row = 0; row < row_count; row++) {
|
2016-12-03 15:00:06 +08:00
|
|
|
const size_t comp_idx = col * row_count + row;
|
|
|
|
if (comp_idx >= lst.size()) continue;
|
2016-12-03 15:29:14 +08:00
|
|
|
const comp_t &c = lst.at(comp_idx);
|
|
|
|
width_by_column[col] = std::max(width_by_column[col], c.preferred_width());
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-03 02:54:01 +08:00
|
|
|
|
2016-12-03 15:29:14 +08:00
|
|
|
bool print;
|
2016-05-03 02:54:01 +08:00
|
|
|
// Force fit if one column.
|
|
|
|
if (cols == 1) {
|
2016-12-03 15:29:14 +08:00
|
|
|
width_by_column[0] = std::min(width_by_column[0], term_width);
|
2014-01-25 08:51:52 +08:00
|
|
|
print = true;
|
2016-12-03 15:29:14 +08:00
|
|
|
} else {
|
|
|
|
// Compute total preferred width, plus spacing
|
|
|
|
size_t total_width_needed = std::accumulate(width_by_column, width_by_column + cols, 0);
|
|
|
|
total_width_needed += (cols - 1) * PAGER_SPACER_STRING_WIDTH;
|
|
|
|
print = (total_width_needed <= term_width);
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
2016-10-30 12:20:29 +08:00
|
|
|
if (!print) {
|
|
|
|
return false; // no need to continue
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-30 12:20:29 +08:00
|
|
|
// Determine the starting and stop row.
|
|
|
|
size_t start_row = 0, stop_row = 0;
|
|
|
|
if (row_count <= term_height) {
|
|
|
|
// Easy, we can show everything.
|
|
|
|
start_row = 0;
|
|
|
|
stop_row = row_count;
|
|
|
|
} else {
|
|
|
|
// We can only show part of the full list. Determine which part based on the
|
|
|
|
// suggested_start_row.
|
|
|
|
assert(row_count > term_height);
|
|
|
|
size_t last_starting_row = row_count - term_height;
|
2019-03-13 13:38:42 +08:00
|
|
|
start_row = std::min(suggested_start_row, last_starting_row);
|
2016-10-30 12:20:29 +08:00
|
|
|
stop_row = start_row + term_height;
|
2017-01-27 09:28:46 +08:00
|
|
|
assert(start_row <= last_starting_row);
|
2016-10-30 12:20:29 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-30 12:20:29 +08:00
|
|
|
assert(stop_row >= start_row);
|
|
|
|
assert(stop_row <= row_count);
|
|
|
|
assert(stop_row - start_row <= term_height);
|
2016-12-03 15:00:06 +08:00
|
|
|
completion_print(cols, width_by_column, start_row, stop_row, prefix, lst, rendering);
|
2016-10-30 12:20:29 +08:00
|
|
|
|
|
|
|
// Add the progress line. It's a "more to disclose" line if necessary, or a row listing if
|
|
|
|
// it's scrollable; otherwise ignore it.
|
2016-12-03 15:00:06 +08:00
|
|
|
// We should never have one row remaining to disclose (else we would have just disclosed it)
|
2016-10-30 12:20:29 +08:00
|
|
|
wcstring progress_text;
|
2016-12-03 15:00:06 +08:00
|
|
|
assert(rendering->remaining_to_disclose != 1);
|
|
|
|
if (rendering->remaining_to_disclose > 1) {
|
2019-04-29 06:00:36 +08:00
|
|
|
progress_text = format_string(_(L"%lsand %lu more rows"), get_ellipsis_str(),
|
2019-11-19 09:08:16 +08:00
|
|
|
static_cast<unsigned long>(rendering->remaining_to_disclose));
|
2016-10-30 12:20:29 +08:00
|
|
|
} else if (start_row > 0 || stop_row < row_count) {
|
|
|
|
// We have a scrollable interface. The +1 here is because we are zero indexed, but want
|
|
|
|
// to present things as 1-indexed. We do not add 1 to stop_row or row_count because
|
|
|
|
// these are the "past the last value".
|
|
|
|
progress_text =
|
|
|
|
format_string(_(L"rows %lu to %lu of %lu"), start_row + 1, stop_row, row_count);
|
|
|
|
} else if (completion_infos.empty() && !unfiltered_completion_infos.empty()) {
|
|
|
|
// Everything is filtered.
|
|
|
|
progress_text = _(L"(no matches)");
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-30 12:20:29 +08:00
|
|
|
if (!progress_text.empty()) {
|
|
|
|
line_t &line = rendering->screen_data.add_line();
|
2019-03-04 09:34:00 +08:00
|
|
|
highlight_spec_t spec = {highlight_role_t::pager_progress,
|
|
|
|
highlight_role_t::pager_progress};
|
2016-12-04 05:38:50 +08:00
|
|
|
print_max(progress_text, spec, term_width, true /* has_more */, &line);
|
2016-10-30 12:20:29 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2017-02-16 07:21:35 +08:00
|
|
|
if (!search_field_shown) {
|
|
|
|
return true;
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
2016-10-30 12:20:29 +08:00
|
|
|
|
2017-02-16 07:21:35 +08:00
|
|
|
// Add the search field.
|
2020-02-04 19:47:44 +08:00
|
|
|
wcstring search_field_text = search_field_line.text();
|
2017-02-16 07:21:35 +08:00
|
|
|
// Append spaces to make it at least the required width.
|
|
|
|
if (search_field_text.size() < PAGER_SEARCH_FIELD_WIDTH) {
|
|
|
|
search_field_text.append(PAGER_SEARCH_FIELD_WIDTH - search_field_text.size(), L' ');
|
|
|
|
}
|
|
|
|
line_t *search_field = &rendering->screen_data.insert_line_at_index(0);
|
|
|
|
|
|
|
|
// We limit the width to term_width - 1.
|
2019-03-04 09:34:00 +08:00
|
|
|
highlight_spec_t underline{};
|
|
|
|
underline.force_underline = true;
|
|
|
|
|
2017-02-16 07:21:35 +08:00
|
|
|
size_t search_field_remaining = term_width - 1;
|
2019-03-04 09:34:00 +08:00
|
|
|
search_field_remaining -= print_max(SEARCH_FIELD_PROMPT, highlight_role_t::normal,
|
2017-02-16 07:21:35 +08:00
|
|
|
search_field_remaining, false, search_field);
|
2019-03-04 09:34:00 +08:00
|
|
|
search_field_remaining -=
|
|
|
|
print_max(search_field_text, underline, search_field_remaining, false, search_field);
|
2016-10-30 12:20:29 +08:00
|
|
|
return true;
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
page_rendering_t pager_t::render() const {
|
|
|
|
/// Try to print the completions. Start by trying to print the list in PAGER_MAX_COLS columns,
|
|
|
|
/// if the completions won't fit, reduce the number of columns by one. Printing a single column
|
|
|
|
/// never fails.
|
2014-01-14 08:41:22 +08:00
|
|
|
page_rendering_t rendering;
|
2014-01-23 09:45:27 +08:00
|
|
|
rendering.term_width = this->available_term_width;
|
|
|
|
rendering.term_height = this->available_term_height;
|
2014-01-27 16:56:13 +08:00
|
|
|
rendering.search_field_shown = this->search_field_shown;
|
|
|
|
rendering.search_field_line = this->search_field_line;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-10 05:36:08 +08:00
|
|
|
for (size_t cols = PAGER_MAX_COLS; cols > 0; cols--) {
|
2016-05-03 02:54:01 +08:00
|
|
|
// Initially empty rendering.
|
2014-01-27 16:56:13 +08:00
|
|
|
rendering.screen_data.resize(0);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Determine how many rows we would need if we had 'cols' columns. Then determine how many
|
|
|
|
// columns we want from that. For example, say we had 19 completions. We can fit them into 6
|
|
|
|
// columns, 4 rows, with the last row containing only 1 entry. Or we can fit them into 5
|
|
|
|
// columns, 4 rows, the last row containing 4 entries. Since fewer columns with the same
|
|
|
|
// number of rows is better, skip cases where we know we can do better.
|
2014-01-27 16:56:13 +08:00
|
|
|
size_t min_rows_required_for_cols = divide_round_up(completion_infos.size(), cols);
|
2016-05-03 02:54:01 +08:00
|
|
|
size_t min_cols_required_for_rows =
|
|
|
|
divide_round_up(completion_infos.size(), min_rows_required_for_cols);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-27 16:56:13 +08:00
|
|
|
assert(min_cols_required_for_rows <= cols);
|
2016-05-03 02:54:01 +08:00
|
|
|
if (cols > 1 && min_cols_required_for_rows < cols) {
|
|
|
|
// Next iteration will be better, so skip this one.
|
2014-01-27 16:56:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2019-11-19 09:08:16 +08:00
|
|
|
rendering.cols = cols;
|
2014-01-27 16:56:13 +08:00
|
|
|
rendering.rows = min_rows_required_for_cols;
|
2016-05-03 02:54:01 +08:00
|
|
|
rendering.selected_completion_idx =
|
|
|
|
this->visual_selected_completion_index(rendering.rows, rendering.cols);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
if (completion_try_print(cols, prefix, completion_infos, &rendering, suggested_row_start)) {
|
2014-01-27 16:56:13 +08:00
|
|
|
break;
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-14 08:41:22 +08:00
|
|
|
return rendering;
|
2013-12-02 07:11:25 +08:00
|
|
|
}
|
2014-01-16 10:21:38 +08:00
|
|
|
|
2020-08-24 06:12:47 +08:00
|
|
|
bool pager_t::rendering_needs_update(const page_rendering_t &rendering) const {
|
|
|
|
// Common case is no pager.
|
|
|
|
if (this->empty() && rendering.screen_data.empty()) return false;
|
|
|
|
|
2020-08-27 04:34:15 +08:00
|
|
|
return this->empty() && !rendering.screen_data.empty() || // Do update after clear().
|
|
|
|
rendering.term_width != this->available_term_width || //
|
2020-08-24 06:12:47 +08:00
|
|
|
rendering.term_height != this->available_term_height || //
|
|
|
|
rendering.selected_completion_idx !=
|
|
|
|
this->visual_selected_completion_index(rendering.rows, rendering.cols) || //
|
|
|
|
rendering.search_field_shown != this->search_field_shown || //
|
|
|
|
rendering.search_field_line.text() != this->search_field_line.text() || //
|
|
|
|
rendering.search_field_line.position() != this->search_field_line.position() || //
|
|
|
|
(rendering.remaining_to_disclose > 0 && this->fully_disclosed);
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::update_rendering(page_rendering_t *rendering) const {
|
2020-08-24 06:12:47 +08:00
|
|
|
if (rendering_needs_update(*rendering)) {
|
2014-01-18 04:04:03 +08:00
|
|
|
*rendering = this->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:12:47 +08:00
|
|
|
pager_t::pager_t() = default;
|
|
|
|
pager_t::~pager_t() = default;
|
2014-01-16 10:21:38 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
bool pager_t::empty() const { return unfiltered_completion_infos.empty(); }
|
2014-01-16 10:21:38 +08:00
|
|
|
|
2019-04-29 05:06:03 +08:00
|
|
|
bool pager_t::select_next_completion_in_direction(selection_motion_t direction,
|
2016-05-03 02:54:01 +08:00
|
|
|
const page_rendering_t &rendering) {
|
|
|
|
// Must have something to select.
|
|
|
|
if (this->completion_infos.empty()) {
|
2014-01-29 00:19:24 +08:00
|
|
|
return false;
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Handle the case of nothing selected yet.
|
|
|
|
if (selected_completion_idx == PAGER_SELECTION_NONE) {
|
|
|
|
switch (direction) {
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::south:
|
|
|
|
case selection_motion_t::page_south:
|
|
|
|
case selection_motion_t::next:
|
|
|
|
case selection_motion_t::north:
|
|
|
|
case selection_motion_t::prev: {
|
2016-05-04 07:23:30 +08:00
|
|
|
// These directions do something sane.
|
2019-04-29 05:06:03 +08:00
|
|
|
if (direction == selection_motion_t::prev ||
|
|
|
|
direction == selection_motion_t::north) {
|
2014-01-25 07:59:18 +08:00
|
|
|
selected_completion_idx = completion_infos.size() - 1;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-22 08:25:55 +08:00
|
|
|
selected_completion_idx = 0;
|
|
|
|
}
|
2014-01-27 18:17:31 +08:00
|
|
|
return true;
|
2016-05-04 07:23:30 +08:00
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::page_north:
|
|
|
|
case selection_motion_t::east:
|
|
|
|
case selection_motion_t::west:
|
|
|
|
case selection_motion_t::deselect: {
|
2016-05-04 07:23:30 +08:00
|
|
|
// These do nothing.
|
2014-01-27 18:17:31 +08:00
|
|
|
return false;
|
2016-05-04 07:23:30 +08:00
|
|
|
}
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Ok, we had something selected already. Select something different.
|
2016-06-15 10:21:50 +08:00
|
|
|
size_t new_selected_completion_idx;
|
2016-05-03 02:54:01 +08:00
|
|
|
if (!selection_direction_is_cardinal(direction)) {
|
|
|
|
// Next, previous, or deselect, all easy.
|
2019-04-29 05:06:03 +08:00
|
|
|
if (direction == selection_motion_t::deselect) {
|
2014-01-25 07:59:18 +08:00
|
|
|
new_selected_completion_idx = PAGER_SELECTION_NONE;
|
2019-04-29 05:06:03 +08:00
|
|
|
} else if (direction == selection_motion_t::next) {
|
2014-01-19 04:42:53 +08:00
|
|
|
new_selected_completion_idx = selected_completion_idx + 1;
|
2016-05-03 02:54:01 +08:00
|
|
|
if (new_selected_completion_idx >= completion_infos.size()) {
|
2014-01-19 04:42:53 +08:00
|
|
|
new_selected_completion_idx = 0;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
} else if (direction == selection_motion_t::prev) {
|
2016-05-03 02:54:01 +08:00
|
|
|
if (selected_completion_idx == 0) {
|
2014-01-19 04:42:53 +08:00
|
|
|
new_selected_completion_idx = completion_infos.size() - 1;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-19 04:42:53 +08:00
|
|
|
new_selected_completion_idx = selected_completion_idx - 1;
|
|
|
|
}
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2016-11-02 12:19:34 +08:00
|
|
|
DIE("unknown non-cardinal direction");
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
|
|
|
// Cardinal directions. We have a completion index; we wish to compute its row and column.
|
2014-01-25 07:59:18 +08:00
|
|
|
size_t current_row = this->get_selected_row(rendering);
|
|
|
|
size_t current_col = this->get_selected_column(rendering);
|
2019-11-19 09:08:16 +08:00
|
|
|
size_t page_height = std::max(rendering.term_height - 1, static_cast<size_t>(1));
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
switch (direction) {
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::page_north: {
|
2016-11-02 13:36:30 +08:00
|
|
|
if (current_row > page_height) {
|
2016-05-03 02:54:01 +08:00
|
|
|
current_row = current_row - page_height;
|
2016-11-02 13:36:30 +08:00
|
|
|
} else {
|
2016-05-03 02:54:01 +08:00
|
|
|
current_row = 0;
|
2016-11-02 13:36:30 +08:00
|
|
|
}
|
2015-03-03 05:08:29 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::north: {
|
2016-05-03 02:54:01 +08:00
|
|
|
// Go up a whole row. If we cycle, go to the previous column.
|
|
|
|
if (current_row > 0) {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_row--;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_row = rendering.rows - 1;
|
2018-01-23 17:39:02 +08:00
|
|
|
if (current_col > 0) {
|
|
|
|
current_col--;
|
|
|
|
} else {
|
|
|
|
current_col = rendering.cols - 1;
|
|
|
|
}
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::page_south: {
|
2016-05-03 02:54:01 +08:00
|
|
|
if (current_row + page_height < rendering.rows) {
|
2015-03-03 05:08:29 +08:00
|
|
|
current_row += page_height;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2015-03-03 05:08:29 +08:00
|
|
|
current_row = rendering.rows - 1;
|
|
|
|
if (current_col * rendering.rows + current_row >= completion_infos.size()) {
|
|
|
|
current_row = (completion_infos.size() - 1) % rendering.rows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::south: {
|
2018-01-23 17:39:02 +08:00
|
|
|
// Go down, unless we are in the last row.
|
|
|
|
// If we go over the last element, wrap to the first.
|
|
|
|
if (current_row + 1 < rendering.rows &&
|
|
|
|
current_col * rendering.rows + current_row + 1 < completion_infos.size()) {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_row++;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_row = 0;
|
2018-01-23 17:39:02 +08:00
|
|
|
current_col = (current_col + 1) % rendering.cols;
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::east: {
|
2016-05-03 02:54:01 +08:00
|
|
|
// Go east, wrapping to the next row. There is no "row memory," so if we run off the
|
|
|
|
// end, wrap.
|
|
|
|
if (current_col + 1 < rendering.cols &&
|
|
|
|
(current_col + 1) * rendering.rows + current_row < completion_infos.size()) {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_col++;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_col = 0;
|
2018-01-23 17:39:02 +08:00
|
|
|
current_row = (current_row + 1) % rendering.rows;
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-04-29 05:06:03 +08:00
|
|
|
case selection_motion_t::west: {
|
2016-05-03 02:54:01 +08:00
|
|
|
// Go west, wrapping to the previous row.
|
|
|
|
if (current_col > 0) {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_col--;
|
2016-05-03 02:54:01 +08:00
|
|
|
} else {
|
2014-01-19 04:42:53 +08:00
|
|
|
current_col = rendering.cols - 1;
|
2018-01-23 17:39:02 +08:00
|
|
|
if (current_row > 0) {
|
|
|
|
current_row--;
|
|
|
|
} else {
|
|
|
|
current_row = rendering.rows - 1;
|
|
|
|
}
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
default: {
|
2016-11-02 12:19:34 +08:00
|
|
|
DIE("unknown cardinal direction");
|
2016-05-04 07:23:30 +08:00
|
|
|
}
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// Compute the new index based on the changed row.
|
2014-01-19 04:42:53 +08:00
|
|
|
new_selected_completion_idx = current_col * rendering.rows + current_row;
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-30 12:20:29 +08:00
|
|
|
if (selected_completion_idx == new_selected_completion_idx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
selected_completion_idx = new_selected_completion_idx;
|
|
|
|
|
|
|
|
// Update suggested_row_start to ensure the selection is visible. suggested_row_start *
|
|
|
|
// rendering.cols is the first suggested visible completion; add the visible completion
|
|
|
|
// count to that to get the last one.
|
|
|
|
size_t visible_row_count = rendering.row_end - rendering.row_start;
|
|
|
|
if (visible_row_count == 0 || selected_completion_idx == PAGER_SELECTION_NONE) {
|
|
|
|
return true; // this should never happen but be paranoid
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-30 12:20:29 +08:00
|
|
|
// Ensure our suggested row start is not past the selected row.
|
|
|
|
size_t row_containing_selection = this->get_selected_row(rendering);
|
|
|
|
if (suggested_row_start > row_containing_selection) {
|
|
|
|
suggested_row_start = row_containing_selection;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-30 12:20:29 +08:00
|
|
|
// Ensure our suggested row start is not too early before it.
|
|
|
|
if (suggested_row_start + visible_row_count <= row_containing_selection) {
|
|
|
|
// The user moved south past the bottom completion.
|
|
|
|
if (!fully_disclosed && rendering.remaining_to_disclose > 0) {
|
|
|
|
fully_disclosed = true; // perform disclosure
|
|
|
|
} else {
|
|
|
|
// Scroll
|
|
|
|
suggested_row_start = row_containing_selection - visible_row_count + 1;
|
|
|
|
// Ensure fully_disclosed is set. I think we can hit this case if the user
|
|
|
|
// resizes the window - we don't want to drop back to the disclosed style.
|
|
|
|
fully_disclosed = true;
|
2014-01-22 06:35:18 +08:00
|
|
|
}
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
2016-10-30 12:20:29 +08:00
|
|
|
|
|
|
|
return true;
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
size_t pager_t::visual_selected_completion_index(size_t rows, size_t cols) const {
|
|
|
|
// No completions -> no selection.
|
|
|
|
if (completion_infos.empty() || rows == 0 || cols == 0) {
|
2014-01-27 16:56:13 +08:00
|
|
|
return PAGER_SELECTION_NONE;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-19 04:42:53 +08:00
|
|
|
size_t result = selected_completion_idx;
|
2016-05-03 02:54:01 +08:00
|
|
|
if (result != PAGER_SELECTION_NONE) {
|
|
|
|
// If the selected completion is beyond the last selection, go left by columns until it's
|
|
|
|
// within it. This is how we implement "column memory".
|
|
|
|
while (result >= completion_infos.size() && result >= rows) {
|
2014-01-20 08:41:26 +08:00
|
|
|
result -= rows;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// If we are still beyond the last selection, clamp it.
|
|
|
|
if (result >= completion_infos.size()) result = completion_infos.size() - 1;
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2014-01-25 07:59:18 +08:00
|
|
|
assert(result == PAGER_SELECTION_NONE || result < completion_infos.size());
|
2014-01-19 04:42:53 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
// It's possible we have no visual selection but are still navigating the contents, e.g. every
|
|
|
|
// completion is filtered.
|
|
|
|
bool pager_t::is_navigating_contents() const {
|
2014-01-27 16:56:13 +08:00
|
|
|
return selected_completion_idx != PAGER_SELECTION_NONE;
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::set_fully_disclosed(bool flag) { fully_disclosed = flag; }
|
2014-02-21 05:30:26 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
const completion_t *pager_t::selected_completion(const page_rendering_t &rendering) const {
|
2019-11-19 10:34:50 +08:00
|
|
|
const completion_t *result = nullptr;
|
2014-01-20 08:41:26 +08:00
|
|
|
size_t idx = visual_selected_completion_index(rendering.rows, rendering.cols);
|
2016-05-03 02:54:01 +08:00
|
|
|
if (idx != PAGER_SELECTION_NONE) {
|
2014-01-25 07:59:18 +08:00
|
|
|
result = &completion_infos.at(idx).representative;
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
return result;
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2014-01-16 10:21:38 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
/// Get the selected row and column. Completions are rendered column first, i.e. we go south before
|
|
|
|
/// we go west. So if we have N rows, and our selected index is N + 2, then our row is 2 (mod by N)
|
|
|
|
/// and our column is 1 (divide by N).
|
|
|
|
size_t pager_t::get_selected_row(const page_rendering_t &rendering) const {
|
|
|
|
if (rendering.rows == 0) return PAGER_SELECTION_NONE;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
return selected_completion_idx == PAGER_SELECTION_NONE
|
|
|
|
? PAGER_SELECTION_NONE
|
|
|
|
: selected_completion_idx % rendering.rows;
|
2014-01-25 07:59:18 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
size_t pager_t::get_selected_column(const page_rendering_t &rendering) const {
|
|
|
|
if (rendering.rows == 0) return PAGER_SELECTION_NONE;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
return selected_completion_idx == PAGER_SELECTION_NONE
|
|
|
|
? PAGER_SELECTION_NONE
|
|
|
|
: selected_completion_idx / rendering.rows;
|
2014-01-25 07:59:18 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::clear() {
|
2014-01-27 16:56:13 +08:00
|
|
|
unfiltered_completion_infos.clear();
|
2014-01-16 10:21:38 +08:00
|
|
|
completion_infos.clear();
|
|
|
|
prefix.clear();
|
2014-01-20 15:52:35 +08:00
|
|
|
selected_completion_idx = PAGER_SELECTION_NONE;
|
2014-01-23 09:45:27 +08:00
|
|
|
fully_disclosed = false;
|
2014-01-26 16:41:30 +08:00
|
|
|
search_field_shown = false;
|
2014-01-27 16:56:13 +08:00
|
|
|
search_field_line.clear();
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
void pager_t::set_search_field_shown(bool flag) { this->search_field_shown = flag; }
|
2014-01-27 16:56:13 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
bool pager_t::is_search_field_shown() const { return this->search_field_shown; }
|
2014-01-27 16:56:13 +08:00
|
|
|
|
2016-05-03 02:54:01 +08:00
|
|
|
size_t pager_t::cursor_position() const {
|
2020-02-04 19:47:44 +08:00
|
|
|
size_t result = std::wcslen(SEARCH_FIELD_PROMPT) + this->search_field_line.position();
|
2016-05-03 02:54:01 +08:00
|
|
|
// Clamp it to the right edge.
|
|
|
|
if (available_term_width > 0 && result + 1 > available_term_width) {
|
2014-01-27 16:56:13 +08:00
|
|
|
result = available_term_width - 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-23 03:04:47 +08:00
|
|
|
page_rendering_t::page_rendering_t() = default;
|