From f596bb8f5e9fb0b9e42ad681886f2ab8991e82d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Mon, 28 Sep 2020 22:51:15 +0200 Subject: [PATCH] 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. --- sdn.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sdn.cpp b/sdn.cpp index 734c78e..ca38e75 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -784,7 +784,7 @@ fun operator< (const entry &e1, const entry &e2) -> bool { return a.filename < b.filename; } -fun reload (const string &old_cwd) { +fun reload (bool keep_anchor) { g.unames.clear(); while (auto *ent = getpwent ()) g.unames.emplace (ent->pw_uid, ent->pw_name); @@ -796,8 +796,8 @@ fun reload (const string &old_cwd) { endgrent(); string anchor; - if (g.cwd == old_cwd && !g.entries.empty ()) - anchor = g.entries[g.cursor].filename; + if (keep_anchor && !g.entries.empty ()) + anchor = g.entries.at (g.cursor).filename; auto now = time (NULL); g.now = *localtime (&now); auto dir = opendir ("."); @@ -1089,12 +1089,13 @@ fun change_dir (const string &path) { auto old_cwd = g.cwd; level last {g.offset, g.cursor, old_cwd, g.entries[g.cursor].filename}; 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)) { g.levels.push_back (last); g.offset = g.cursor = 0; - } else { + } else if (!same_path) { pop_levels (); } } @@ -1206,12 +1207,12 @@ fun handle (wint_t c) -> bool { case ACTION_SORT_LEFT: g.sort_column = (g.sort_column + entry::COLUMNS - 1) % entry::COLUMNS; g.sort_flash_ttl = 2; - reload (g.cwd); + reload (true); break; case ACTION_SORT_RIGHT: g.sort_column = (g.sort_column + entry::COLUMNS + 1) % entry::COLUMNS; g.sort_flash_ttl = 2; - reload (g.cwd); + reload (true); break; case ACTION_UP: @@ -1284,7 +1285,7 @@ fun handle (wint_t c) -> bool { g.editor_on_confirm = [] { auto mb = to_mb (g.editor_line); rename (g.entries[g.cursor].filename.c_str (), mb.c_str ()); - reload (g.cwd); + reload (true); }; break; @@ -1293,17 +1294,17 @@ fun handle (wint_t c) -> bool { break; case ACTION_REVERSE_SORT: g.reverse_sort = !g.reverse_sort; - reload (g.cwd); + reload (true); break; case ACTION_SHOW_HIDDEN: g.show_hidden = !g.show_hidden; - reload (g.cwd); + reload (true); break; case ACTION_REDRAW: clear (); break; case ACTION_RELOAD: - reload (g.cwd); + reload (true); break; default: if (c != KEY (RESIZE) && c != WEOF) @@ -1635,7 +1636,7 @@ int main (int argc, char *argv[]) { load_colors (); g.start_dir = g.cwd = initial_cwd (); - reload (g.cwd); + reload (false); pop_levels (); update ();