reload() doesn't really need to know the old path

Just whether it's safe for it to keep an anchor on the current item.

I've left in the safety check for an empty entries array,
for the theoretical case of an empty or entirely filtered-out root
directory.  It will likely crash in that case, anyway,
the cursor may be either -1 or 0.
This commit is contained in:
Přemysl Eric Janouch 2020-09-28 22:51:15 +02:00
parent 67da8d965b
commit f596bb8f5e
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 13 additions and 12 deletions

25
sdn.cpp
View File

@ -784,7 +784,7 @@ fun operator< (const entry &e1, const entry &e2) -> bool {
return a.filename < b.filename; return a.filename < b.filename;
} }
fun reload (const string &old_cwd) { fun reload (bool keep_anchor) {
g.unames.clear(); g.unames.clear();
while (auto *ent = getpwent ()) while (auto *ent = getpwent ())
g.unames.emplace (ent->pw_uid, ent->pw_name); g.unames.emplace (ent->pw_uid, ent->pw_name);
@ -796,8 +796,8 @@ fun reload (const string &old_cwd) {
endgrent(); endgrent();
string anchor; string anchor;
if (g.cwd == old_cwd && !g.entries.empty ()) if (keep_anchor && !g.entries.empty ())
anchor = g.entries[g.cursor].filename; anchor = g.entries.at (g.cursor).filename;
auto now = time (NULL); g.now = *localtime (&now); auto now = time (NULL); g.now = *localtime (&now);
auto dir = opendir ("."); auto dir = opendir (".");
@ -1089,12 +1089,13 @@ fun change_dir (const string &path) {
auto old_cwd = g.cwd; auto old_cwd = g.cwd;
level last {g.offset, g.cursor, old_cwd, g.entries[g.cursor].filename}; level last {g.offset, g.cursor, old_cwd, g.entries[g.cursor].filename};
g.cwd = full_path; g.cwd = full_path;
reload (old_cwd); bool same_path = old_cwd == g.cwd;
reload (same_path);
if (is_ancestor_dir (last.path, g.cwd)) { if (is_ancestor_dir (last.path, g.cwd)) {
g.levels.push_back (last); g.levels.push_back (last);
g.offset = g.cursor = 0; g.offset = g.cursor = 0;
} else { } else if (!same_path) {
pop_levels (); pop_levels ();
} }
} }
@ -1206,12 +1207,12 @@ fun handle (wint_t c) -> bool {
case ACTION_SORT_LEFT: case ACTION_SORT_LEFT:
g.sort_column = (g.sort_column + entry::COLUMNS - 1) % entry::COLUMNS; g.sort_column = (g.sort_column + entry::COLUMNS - 1) % entry::COLUMNS;
g.sort_flash_ttl = 2; g.sort_flash_ttl = 2;
reload (g.cwd); reload (true);
break; break;
case ACTION_SORT_RIGHT: case ACTION_SORT_RIGHT:
g.sort_column = (g.sort_column + entry::COLUMNS + 1) % entry::COLUMNS; g.sort_column = (g.sort_column + entry::COLUMNS + 1) % entry::COLUMNS;
g.sort_flash_ttl = 2; g.sort_flash_ttl = 2;
reload (g.cwd); reload (true);
break; break;
case ACTION_UP: case ACTION_UP:
@ -1284,7 +1285,7 @@ fun handle (wint_t c) -> bool {
g.editor_on_confirm = [] { g.editor_on_confirm = [] {
auto mb = to_mb (g.editor_line); auto mb = to_mb (g.editor_line);
rename (g.entries[g.cursor].filename.c_str (), mb.c_str ()); rename (g.entries[g.cursor].filename.c_str (), mb.c_str ());
reload (g.cwd); reload (true);
}; };
break; break;
@ -1293,17 +1294,17 @@ fun handle (wint_t c) -> bool {
break; break;
case ACTION_REVERSE_SORT: case ACTION_REVERSE_SORT:
g.reverse_sort = !g.reverse_sort; g.reverse_sort = !g.reverse_sort;
reload (g.cwd); reload (true);
break; break;
case ACTION_SHOW_HIDDEN: case ACTION_SHOW_HIDDEN:
g.show_hidden = !g.show_hidden; g.show_hidden = !g.show_hidden;
reload (g.cwd); reload (true);
break; break;
case ACTION_REDRAW: case ACTION_REDRAW:
clear (); clear ();
break; break;
case ACTION_RELOAD: case ACTION_RELOAD:
reload (g.cwd); reload (true);
break; break;
default: default:
if (c != KEY (RESIZE) && c != WEOF) if (c != KEY (RESIZE) && c != WEOF)
@ -1635,7 +1636,7 @@ int main (int argc, char *argv[]) {
load_colors (); load_colors ();
g.start_dir = g.cwd = initial_cwd (); g.start_dir = g.cwd = initial_cwd ();
reload (g.cwd); reload (false);
pop_levels (); pop_levels ();
update (); update ();