Enable pushing the search in a certain direction

We want to make it possible to iterate all current matches.
This commit is contained in:
Přemysl Eric Janouch 2021-07-17 08:15:07 +02:00
parent 0adbac2066
commit e948741864
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 7 additions and 6 deletions

13
sdn.cpp
View File

@ -989,10 +989,11 @@ fun show_help () {
fclose (contents); fclose (contents);
} }
fun search (const wstring &needle) -> int { /// Stays on the current match when there are no better ones, unless it's pushed
int best = g.cursor, best_n = 0, matches = 0; fun search (const wstring &needle, int push) -> int {
for (int i = 0; i < int (g.entries.size ()); i++) { int best = g.cursor, best_n = 0, matches = 0, step = push != 0 ? push : 1;
auto o = (i + g.cursor) % g.entries.size (); 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); int n = prefix_length (to_wide (g.entries[o].filename), needle);
matches += n == needle.size (); matches += n == needle.size ();
if (n > best_n) { if (n > best_n) {
@ -1060,7 +1061,7 @@ fun pop_levels (const string& old_cwd) {
fix_cursor_and_offset (); fix_cursor_and_offset ();
if (!anchor.empty () && at_cursor ().filename != anchor) 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) { fun explode_path (const string &path, vector<string> &out) {
@ -1375,7 +1376,7 @@ 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); search (g.editor_line, 0);
}; };
g.editor_on_confirm = [] { g.editor_on_confirm = [] {
choose (at_cursor ()); choose (at_cursor ());