From 081b4db5c38dcc6632577a19e46994f62e6cf5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?=
Date: Fri, 2 Nov 2018 20:22:50 +0100 Subject: [PATCH] Fix level popping --- sdn.cpp | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/sdn.cpp b/sdn.cpp index 4ef3560..6a255a0 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -866,6 +866,24 @@ fun search (const wstring &needle) { g.cursor = best; } +fun fix_cursor_and_offset () { + g.cursor = max (g.cursor, 0); + g.cursor = min (g.cursor, int (g.entries.size ()) - 1); + + // Decrease the offset when more items can suddenly fit + int pushable = visible_lines () - (int (g.entries.size ()) - g.offset); + g.offset -= max (pushable, 0); + + // Make sure cursor is visible + g.offset = max (g.offset, 0); + g.offset = min (g.offset, int (g.entries.size ()) - 1); + + if (g.offset > g.cursor) + g.offset = g.cursor; + if (g.cursor - g.offset >= visible_lines ()) + g.offset = g.cursor - visible_lines () + 1; +} + fun is_ancestor_dir (const string &ancestor, const string &of) -> bool { if (strncmp (ancestor.c_str (), of.c_str (), ancestor.length ())) return false; @@ -883,8 +901,8 @@ fun pop_levels () { i++; g.levels.pop_back (); } - if (!anchor.empty () && (g.cursor >= g.entries.size () - || g.entries[g.cursor].filename != anchor)) + fix_cursor_and_offset (); + if (!anchor.empty () && g.entries[g.cursor].filename != anchor) search (to_wide (anchor)); } @@ -1064,22 +1082,7 @@ fun handle (wint_t c) -> bool { if (c != KEY (RESIZE) && c != WEOF) beep (); } - g.cursor = max (g.cursor, 0); - g.cursor = min (g.cursor, int (g.entries.size ()) - 1); - - // Decrease the offset when more items can suddenly fit - int pushable = visible_lines () - (int (g.entries.size ()) - g.offset); - g.offset -= max (pushable, 0); - - // Make sure cursor is visible - g.offset = max (g.offset, 0); - g.offset = min (g.offset, int (g.entries.size ()) - 1); - - if (g.offset > g.cursor) - g.offset = g.cursor; - if (g.cursor - g.offset >= visible_lines ()) - g.offset = g.cursor - visible_lines () + 1; - + fix_cursor_and_offset (); update (); return true; }