Compare commits

...

3 Commits

Author SHA1 Message Date
638d570cbd
Add & and ~ chdir shortcuts
& goes to the starting directory, ~ goes home.
2018-10-24 05:16:22 +02:00
ae310d1380
Use Doxygen field comments 2018-10-24 05:05:26 +02:00
edf94db8df
Limit inotify to visible events 2018-10-24 04:51:16 +02:00

62
sdn.cpp
View File

@ -297,27 +297,28 @@ static const char *g_ls_colors[] = {LS(XX)};
#undef XX
static struct {
string cwd; // Current working directory
vector<entry> entries; // Current directory entries
int offset, cursor; // Scroll offset and cursor position
bool full_view; // Whether to show extended information
int max_widths[row::COLUMNS]; // Column widths
string cwd; ///< Current working directory
string start_dir; ///< Starting directory
vector<entry> entries; ///< Current directory entries
int offset, cursor; ///< Scroll offset and cursor position
bool full_view; ///< Show extended information
int max_widths[row::COLUMNS]; ///< Column widths
string chosen; // Chosen item for the command line
bool chosen_full; // Use the full path
string chosen; ///< Chosen item for the command line
bool chosen_full; ///< Use the full path
int inotify_fd, inotify_wd = -1; // File watch
bool out_of_date; // Entries may be out of date
int inotify_fd, inotify_wd = -1; ///< File watch
bool out_of_date; ///< Entries may be out of date
wchar_t editor; // Prompt character for editing
wstring editor_line; // Current user input
wchar_t editor; ///< Prompt character for editing
wstring editor_line; ///< Current user input
enum { AT_CURSOR, AT_BAR, AT_CWD, AT_INPUT, AT_COUNT };
chtype attrs[AT_COUNT] = {A_REVERSE, 0, A_BOLD, 0};
const char *attr_names[AT_COUNT] = {"cursor", "bar", "cwd", "input"};
map<int, chtype> ls_colors; // LS_COLORS decoded
map<string, chtype> ls_exts; // LS_COLORS file extensions
map<int, chtype> ls_colors; ///< LS_COLORS decoded
map<string, chtype> ls_exts; ///< LS_COLORS file extensions
} g;
fun ls_format (const string &filename, const struct stat &info) -> chtype {
@ -495,8 +496,9 @@ fun reload () {
if (g.inotify_wd != -1)
inotify_rm_watch (g.inotify_fd, g.inotify_wd);
// We don't show atime, so access and open are merely spam
g.inotify_wd = inotify_add_watch (g.inotify_fd, buf,
IN_ALL_EVENTS | IN_ONLYDIR);
(IN_ALL_EVENTS | IN_ONLYDIR | IN_EXCL_UNLINK) & ~(IN_ACCESS | IN_OPEN));
}
fun search (const wstring &needle) {
@ -536,6 +538,16 @@ fun handle_editor (wint_t c, bool is_char) {
beep ();
}
fun change_dir (const string& path) {
if (chdir (path.c_str ())) {
beep ();
} else {
// TODO: remember cursor going down, then restore going up
g.cursor = 0;
reload ();
}
}
fun choose (const entry &entry) -> bool {
bool is_dir = S_ISDIR (entry.info.st_mode) != 0;
// Dive into directories and accessible symlinks to them
@ -552,13 +564,7 @@ fun choose (const entry &entry) -> bool {
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 ();
}
change_dir (entry.filename);
return true;
}
@ -616,6 +622,16 @@ fun handle (wint_t c, bool is_char) -> bool {
case CTRL L'e': g.offset++; break;
case CTRL L'y': g.offset--; break;
case '&':
change_dir (g.start_dir);
break;
case '~':
if (const auto *home = getenv ("HOME"))
change_dir (home);
else if (const auto *pw = getpwuid (getuid ()))
change_dir (pw->pw_dir);
break;
case L't':
case ALT | L't':
g.full_view = !g.full_view;
@ -794,8 +810,8 @@ int main (int argc, char *argv[]) {
load_configuration ();
reload ();
g.start_dir = g.cwd;
update ();
auto start_dir = g.cwd;
wint_t c;
while (1) {
@ -818,7 +834,7 @@ int main (int argc, char *argv[]) {
cout << "local insert=" << shell_escape (full_path) << endl;
return 0;
}
if (g.cwd != start_dir)
if (g.cwd != g.start_dir)
cout << "local cd=" << shell_escape (g.cwd) << endl;
if (!g.chosen.empty ())
cout << "local insert=" << shell_escape (g.chosen) << endl;