Add and integrate sdn-open
All checks were successful
Alpine 3.22 Success
Arch Linux AUR Success
OpenBSD 7.8 Success

Originally I thought that not supporting %cd would be an issue,
making this kind of utility unclean.

It turns out the desire to launch xdg-open quickly is stronger.
This commit is contained in:
2025-11-20 20:49:14 +01:00
parent 977d1a7120
commit 3e39cc5660
6 changed files with 96 additions and 10 deletions

20
sdn.cpp
View File

@@ -1,7 +1,7 @@
//
// sdn: simple directory navigator
//
// Copyright (c) 2017 - 2024, Přemysl Eric Janouch <p@janouch.name>
// Copyright (c) 2017 - 2025, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
@@ -430,8 +430,8 @@ enum { ALT = 1 << 24, SYM = 1 << 25 }; // Outside the range of Unicode
#define CTRL(char) ((char) == '?' ? 0x7f : (char) & 0x1f)
#define ACTIONS(XX) XX(NONE) XX(HELP) XX(QUIT) XX(QUIT_NO_CHDIR) \
XX(ENTER) XX(CHOOSE) XX(CHOOSE_FULL) XX(VIEW_RAW) XX(VIEW) XX(EDIT) \
XX(SORT_LEFT) XX(SORT_RIGHT) \
XX(ENTER) XX(OPEN) XX(CHOOSE) XX(CHOOSE_FULL) \
XX(VIEW_RAW) XX(VIEW) XX(EDIT) XX(SORT_LEFT) XX(SORT_RIGHT) \
XX(SELECT) XX(DESELECT) XX(SELECT_TOGGLE) XX(SELECT_ABORT) \
XX(UP) XX(DOWN) XX(TOP) XX(BOTTOM) XX(HIGH) XX(MIDDLE) XX(LOW) \
XX(PAGE_PREVIOUS) XX(PAGE_NEXT) XX(SCROLL_UP) XX(SCROLL_DOWN) XX(CENTER) \
@@ -453,7 +453,7 @@ static const char *g_action_names[] = {ACTIONS(XX)};
static map<wint_t, action> g_normal_actions {
{'\r', ACTION_ENTER}, {KEY (ENTER), ACTION_ENTER},
{ALT | '\r', ACTION_CHOOSE}, {ALT | KEY (ENTER), ACTION_CHOOSE},
{ALT | '\r', ACTION_OPEN}, {ALT | KEY (ENTER), ACTION_OPEN},
{'t', ACTION_CHOOSE}, {'T', ACTION_CHOOSE_FULL},
{KEY (F (1)), ACTION_HELP}, {'h', ACTION_HELP},
{KEY (F (3)), ACTION_VIEW}, {KEY (F (13)), ACTION_VIEW_RAW},
@@ -1056,13 +1056,18 @@ fun run_program (initializer_list<const char *> list, const string &filename) {
update ();
}
fun sdn_open (const string &filename) {
run_program ({(const char *) getenv ("SDN_OPENER"), "sdn-open", "xdg-open"},
filename);
}
fun view_raw (const string &filename) {
// XXX: we cannot realistically detect that the pager hasn't made a pause
// at the end of the file, so we can't ensure all contents have been seen
run_program ({(const char *) getenv ("PAGER"), "less", "cat"}, filename);
}
fun view (const string &filename) {
fun sdn_view (const string &filename) {
run_program ({(const char *) getenv ("SDN_VIEWER"), "sdn-view",
(const char *) getenv ("PAGER"), "less", "cat"}, filename);
}
@@ -1525,12 +1530,15 @@ fun handle (wint_t c) -> bool {
case ACTION_ENTER:
enter (current);
break;
case ACTION_OPEN:
sdn_open (current.filename);
break;
case ACTION_VIEW_RAW:
// Mimic mc, it does not seem sensible to page directories
(is_directory ? change_dir : view_raw) (current.filename);
break;
case ACTION_VIEW:
(is_directory ? change_dir : view) (current.filename);
(is_directory ? change_dir : sdn_view) (current.filename);
break;
case ACTION_EDIT:
edit (current.filename);