Compare commits
5 Commits
98612f5492
...
ec1f1031cc
Author | SHA1 | Date | |
---|---|---|---|
ec1f1031cc | |||
bc99b3dd48 | |||
e948741864 | |||
0adbac2066 | |||
2238db5a4e |
87
sdn.cpp
87
sdn.cpp
@ -459,8 +459,13 @@ static map<wint_t, action> g_input_actions {
|
||||
{CTRL ('A'), ACTION_INPUT_BEGINNING}, {KEY (HOME), ACTION_INPUT_BEGINNING},
|
||||
{CTRL ('E'), ACTION_INPUT_END}, {KEY (END), ACTION_INPUT_END},
|
||||
};
|
||||
static map<wint_t, action> g_search_actions {
|
||||
{CTRL ('P'), ACTION_UP}, {KEY (UP), ACTION_UP},
|
||||
{CTRL ('N'), ACTION_DOWN}, {KEY (DOWN), ACTION_DOWN},
|
||||
};
|
||||
static const map<string, map<wint_t, action>*> g_binding_contexts {
|
||||
{"normal", &g_normal_actions}, {"input", &g_input_actions},
|
||||
{"search", &g_search_actions},
|
||||
};
|
||||
|
||||
#define LS(XX) XX(NORMAL, "no") XX(FILE, "fi") XX(RESET, "rs") \
|
||||
@ -527,16 +532,18 @@ static struct {
|
||||
bool out_of_date; ///< Entries may be out of date
|
||||
|
||||
const wchar_t *editor; ///< Prompt string for editing
|
||||
wstring editor_info; ///< Right-side prompt while editing
|
||||
wstring editor_line; ///< Current user input
|
||||
int editor_cursor = 0; ///< Cursor position
|
||||
bool editor_inserting; ///< Inserting a literal character
|
||||
void (*editor_on_change) (); ///< Callback on editor change
|
||||
void (*editor_on_confirm) (); ///< Callback on editor confirmation
|
||||
map<action, void (*) ()> editor_on; ///< Handlers for custom actions
|
||||
|
||||
enum { AT_CURSOR, AT_BAR, AT_CWD, AT_INPUT, AT_CMDLINE, AT_COUNT };
|
||||
chtype attrs[AT_COUNT] = {A_REVERSE, 0, A_BOLD, 0, 0};
|
||||
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, A_ITALIC, 0};
|
||||
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<string, chtype> ls_exts; ///< LS_COLORS file extensions
|
||||
@ -750,9 +757,16 @@ fun update () {
|
||||
curs_set (0);
|
||||
if (g.editor) {
|
||||
move (LINES - 1, 0);
|
||||
auto prompt = apply_attrs (wstring (g.editor) + L": ", 0);
|
||||
auto line = apply_attrs (g.editor_line, 0);
|
||||
print (prompt + line, COLS - 1);
|
||||
auto prompt = apply_attrs (wstring (g.editor) + L": ", 0),
|
||||
line = apply_attrs (g.editor_line, 0),
|
||||
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));
|
||||
move (LINES - 1, compute_width (start));
|
||||
curs_set (1);
|
||||
@ -989,17 +1003,32 @@ fun show_help () {
|
||||
fclose (contents);
|
||||
}
|
||||
|
||||
fun search (const wstring &needle) {
|
||||
int best = g.cursor, best_n = 0;
|
||||
for (int i = 0; i < int (g.entries.size ()); i++) {
|
||||
auto o = (i + g.cursor) % g.entries.size ();
|
||||
/// Stays on the current match when there are no better ones, unless it's pushed
|
||||
fun search (const wstring &needle, int push) -> int {
|
||||
int best = g.cursor, best_n = 0, matches = 0, step = push != 0 ? push : 1;
|
||||
for (int i = 0, count = g.entries.size (); i < count; i++) {
|
||||
auto o = (g.cursor + (count + i * step) + (count + push)) % count;
|
||||
int n = prefix_length (to_wide (g.entries[o].filename), needle);
|
||||
matches += n == needle.size ();
|
||||
if (n > best_n) {
|
||||
best = o;
|
||||
best_n = n;
|
||||
}
|
||||
}
|
||||
g.cursor = best;
|
||||
return matches;
|
||||
}
|
||||
|
||||
fun search_interactive (int push) {
|
||||
int matches = search (g.editor_line, push);
|
||||
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)";
|
||||
}
|
||||
|
||||
fun fix_cursor_and_offset () {
|
||||
@ -1058,7 +1087,7 @@ fun pop_levels (const string& old_cwd) {
|
||||
|
||||
fix_cursor_and_offset ();
|
||||
if (!anchor.empty () && at_cursor ().filename != anchor)
|
||||
search (to_wide (anchor));
|
||||
search (to_wide (anchor), 0);
|
||||
}
|
||||
|
||||
fun explode_path (const string &path, vector<string> &out) {
|
||||
@ -1189,25 +1218,37 @@ fun move_towards_spacing (int diff) -> bool {
|
||||
}
|
||||
|
||||
fun handle_editor (wint_t c) {
|
||||
auto i = g_input_actions.find (g.editor_inserting ? WEOF : c);
|
||||
auto action = ACTION_NONE;
|
||||
if (g.editor_inserting) {
|
||||
(void) halfdelay (1);
|
||||
g.editor_inserting = false;
|
||||
} else {
|
||||
auto i = g_input_actions.find (c);
|
||||
if (i != g_input_actions.end ())
|
||||
action = i->second;
|
||||
|
||||
auto m = g_binding_contexts.find (to_mb (g.editor));
|
||||
if (m != g_binding_contexts.end ()
|
||||
&& (i = m->second->find (c)) != m->second->end ())
|
||||
action = i->second;
|
||||
}
|
||||
|
||||
switch (i == g_input_actions.end () ? ACTION_NONE : i->second) {
|
||||
auto original = g.editor_line;
|
||||
switch (action) {
|
||||
case ACTION_INPUT_CONFIRM:
|
||||
if (g.editor_on_confirm)
|
||||
g.editor_on_confirm ();
|
||||
// Fall-through
|
||||
case ACTION_INPUT_ABORT:
|
||||
g.editor = 0;
|
||||
g.editor_info.clear ();
|
||||
g.editor_line.clear ();
|
||||
g.editor_cursor = 0;
|
||||
g.editor_inserting = false;
|
||||
g.editor_on_change = nullptr;
|
||||
g.editor_on_confirm = nullptr;
|
||||
break;
|
||||
g.editor_on.clear ();
|
||||
return;
|
||||
case ACTION_INPUT_BEGINNING:
|
||||
g.editor_cursor = 0;
|
||||
break;
|
||||
@ -1251,15 +1292,17 @@ fun handle_editor (wint_t c) {
|
||||
g.editor_inserting = true;
|
||||
break;
|
||||
default:
|
||||
if (c & (ALT | SYM)) {
|
||||
if (auto handler = g.editor_on[action]) {
|
||||
handler ();
|
||||
} else if (c & (ALT | SYM)) {
|
||||
beep ();
|
||||
} else {
|
||||
g.editor_line.insert (g.editor_cursor, 1, c);
|
||||
g.editor_cursor++;
|
||||
if (g.editor_on_change)
|
||||
g.editor_on_change ();
|
||||
}
|
||||
}
|
||||
if (g.editor_on_change && g.editor_line != original)
|
||||
g.editor_on_change ();
|
||||
}
|
||||
|
||||
fun handle (wint_t c) -> bool {
|
||||
@ -1371,12 +1414,10 @@ fun handle (wint_t c) -> bool {
|
||||
|
||||
case ACTION_SEARCH:
|
||||
g.editor = L"search";
|
||||
g.editor_on_change = [] {
|
||||
search (g.editor_line);
|
||||
};
|
||||
g.editor_on_confirm = [] {
|
||||
choose (at_cursor ());
|
||||
};
|
||||
g.editor_on_change = [] { search_interactive (0); };
|
||||
g.editor_on[ACTION_UP] = [] { search_interactive (-1); };
|
||||
g.editor_on[ACTION_DOWN] = [] { search_interactive (+1); };
|
||||
g.editor_on_confirm = [] { choose (at_cursor ()); };
|
||||
break;
|
||||
case ACTION_RENAME_PREFILL:
|
||||
g.editor_line = to_wide (current.filename);
|
||||
|
Loading…
x
Reference in New Issue
Block a user