Factor out choose()

This commit is contained in:
Přemysl Eric Janouch 2017-06-30 23:33:52 +02:00
parent e38f0234c9
commit 8c658475b4
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 28 additions and 21 deletions

49
sdn.cpp
View File

@ -436,6 +436,31 @@ fun handle_editor (wint_t c, bool is_char) {
beep (); beep ();
} }
fun choose (const entry &entry) -> bool {
bool is_dir = S_ISDIR (entry.info.st_mode) != 0;
// Dive into directories and accessible symlinks to them
if (S_ISLNK (entry.info.st_mode)) {
char buf[PATH_MAX];
struct stat sb = {};
auto len = readlink (entry.filename.c_str (), buf, sizeof buf);
is_dir = len > 0 && size_t (len) < sizeof buf
&& !stat (entry.filename.c_str (), &sb)
&& S_ISDIR (sb.st_mode) != 0;
}
if (!is_dir) {
g.chosen = entry.filename;
return false;
}
if (chdir (entry.filename.c_str ())) {
beep ();
} else {
// TODO: remember cursor going down, then restore going up
g.cursor = 0;
reload ();
}
return true;
}
fun handle (wint_t c, bool is_char) -> bool { fun handle (wint_t c, bool is_char) -> bool {
// If an editor is active, let it handle the key instead and eat it // If an editor is active, let it handle the key instead and eat it
if (g.editor) { if (g.editor) {
@ -462,27 +487,9 @@ fun handle (wint_t c, bool is_char) -> bool {
return false; return false;
case L'\r': case L'\r':
case KEY_ENTER: case KEY_ENTER:
{ if (choose (current))
bool is_dir = S_ISDIR (current.info.st_mode) != 0; break;
// Dive into directories and accessible symlinks to them return false;
if (S_ISLNK (current.info.st_mode)) {
char buf[PATH_MAX];
struct stat sb = {};
auto len = readlink (current.filename.c_str (), buf, sizeof buf);
is_dir = len > 0 && size_t (len) < sizeof buf
&& !stat (current.filename.c_str (), &sb)
&& S_ISDIR (sb.st_mode) != 0;
}
if (!is_dir) {
g.chosen = current.filename;
return false;
}
if (!chdir (current.filename.c_str ())) {
g.cursor = 0;
reload ();
}
break;
}
// M-o ought to be the same shortcut the navigator is launched with // M-o ought to be the same shortcut the navigator is launched with
case ALT | L'o': case ALT | L'o':