Add a right-side prompt to the search

This commit is contained in:
Přemysl Eric Janouch 2021-07-17 12:44:01 +02:00
parent e948741864
commit bc99b3dd48
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 24 additions and 7 deletions

31
sdn.cpp
View File

@ -527,16 +527,17 @@ static struct {
bool out_of_date; ///< Entries may be out of date bool out_of_date; ///< Entries may be out of date
const wchar_t *editor; ///< Prompt string for editing const wchar_t *editor; ///< Prompt string for editing
wstring editor_info; ///< Right-side prompt while editing
wstring editor_line; ///< Current user input wstring editor_line; ///< Current user input
int editor_cursor = 0; ///< Cursor position int editor_cursor = 0; ///< Cursor position
bool editor_inserting; ///< Inserting a literal character bool editor_inserting; ///< Inserting a literal character
void (*editor_on_change) (); ///< Callback on editor change void (*editor_on_change) (); ///< Callback on editor change
void (*editor_on_confirm) (); ///< Callback on editor confirmation void (*editor_on_confirm) (); ///< Callback on editor confirmation
enum { AT_CURSOR, AT_BAR, AT_CWD, AT_INPUT, AT_CMDLINE, AT_COUNT }; enum { AT_CURSOR, AT_BAR, AT_CWD, AT_INPUT, AT_INFO, AT_CMDLINE, AT_COUNT };
chtype attrs[AT_COUNT] = {A_REVERSE, 0, A_BOLD, 0, 0}; chtype attrs[AT_COUNT] = {A_REVERSE, 0, A_BOLD, 0, A_ITALIC, 0};
const char *attr_names[AT_COUNT] = const char *attr_names[AT_COUNT] =
{"cursor", "bar", "cwd", "input", "cmdline"}; {"cursor", "bar", "cwd", "input", "info", "cmdline"};
map<int, chtype> ls_colors; ///< LS_COLORS decoded map<int, chtype> ls_colors; ///< LS_COLORS decoded
map<string, chtype> ls_exts; ///< LS_COLORS file extensions map<string, chtype> ls_exts; ///< LS_COLORS file extensions
@ -750,9 +751,16 @@ fun update () {
curs_set (0); curs_set (0);
if (g.editor) { if (g.editor) {
move (LINES - 1, 0); move (LINES - 1, 0);
auto prompt = apply_attrs (wstring (g.editor) + L": ", 0); auto prompt = apply_attrs (wstring (g.editor) + L": ", 0),
auto line = apply_attrs (g.editor_line, 0); line = apply_attrs (g.editor_line, 0),
print (prompt + line, COLS - 1); info = apply_attrs (g.editor_info, g.attrs[g.AT_INFO]);
auto info_width = compute_width (info);
if (print (prompt + line, COLS - 1) < COLS - info_width) {
move (LINES - 1, COLS - info_width);
print (info, info_width);
}
auto start = sanitize (prompt + line.substr (0, g.editor_cursor)); auto start = sanitize (prompt + line.substr (0, g.editor_cursor));
move (LINES - 1, compute_width (start)); move (LINES - 1, compute_width (start));
curs_set (1); curs_set (1);
@ -1206,6 +1214,7 @@ fun handle_editor (wint_t c) {
// Fall-through // Fall-through
case ACTION_INPUT_ABORT: case ACTION_INPUT_ABORT:
g.editor = 0; g.editor = 0;
g.editor_info.clear ();
g.editor_line.clear (); g.editor_line.clear ();
g.editor_cursor = 0; g.editor_cursor = 0;
g.editor_inserting = false; g.editor_inserting = false;
@ -1376,7 +1385,15 @@ fun handle (wint_t c) -> bool {
case ACTION_SEARCH: case ACTION_SEARCH:
g.editor = L"search"; g.editor = L"search";
g.editor_on_change = [] { g.editor_on_change = [] {
search (g.editor_line, 0); int matches = search (g.editor_line, 0);
if (g.editor_line.empty ())
g.editor_info.clear ();
else if (matches == 0)
g.editor_info = L"(no match)";
else if (matches == 1)
g.editor_info = L"(1 match)";
else
g.editor_info = L"(" + to_wstring (matches) + L" matches)";
}; };
g.editor_on_confirm = [] { g.editor_on_confirm = [] {
choose (at_cursor ()); choose (at_cursor ());