updated
This commit is contained in:
@@ -7,7 +7,10 @@ DOCDIR ?= $(PREFIX)/share/doc/sfm
|
|||||||
|
|
||||||
CXX ?= g++
|
CXX ?= g++
|
||||||
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
|
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
|
||||||
LDFLAGS ?= -lncursesw
|
# Pick a curses library: prefer ncursesw, fall back to ncurses, then plain
|
||||||
|
# curses (netbsd-curses and BSD base systems provide only libcurses).
|
||||||
|
CURSES_LIBS ?= $(shell pkg-config --libs ncursesw 2>/dev/null || pkg-config --libs ncurses 2>/dev/null || echo -lcurses)
|
||||||
|
LDLIBS ?= $(CURSES_LIBS)
|
||||||
|
|
||||||
INSTALL ?= install
|
INSTALL ?= install
|
||||||
RM ?= rm -f
|
RM ?= rm -f
|
||||||
@@ -17,7 +20,7 @@ RM ?= rm -f
|
|||||||
all: sfm
|
all: sfm
|
||||||
|
|
||||||
sfm: sfm.cpp
|
sfm: sfm.cpp
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(LDFLAGS)
|
$(CXX) $(CXXFLAGS) -o $@ $< $(LDLIBS)
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
@echo "Installing sfm to $(DESTDIR)$(BINDIR)/sfm ..."
|
@echo "Installing sfm to $(DESTDIR)$(BINDIR)/sfm ..."
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ sfm - Simple File Manager
|
|||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
sfm is a lightweight, terminal-based file manager with a vim-inspired
|
sfm is a lightweight, terminal-based file manager with a vim-inspired
|
||||||
key layout. The primary version is a fast C++/ncurses binary. A portable
|
key layout. The primary version is a fast C++ binary that works with
|
||||||
POSIX sh version (sfm.sh) is also included, requiring only standard Unix
|
both ncurses and netbsd-curses. A portable POSIX sh version (sfm.sh) is
|
||||||
tools (ls, awk, tput, stty, mv, cp, rm).
|
also included, requiring only standard Unix tools (ls, awk, tput, stty,
|
||||||
|
mv, cp, rm).
|
||||||
|
|
||||||
|
|
||||||
REQUIREMENTS
|
REQUIREMENTS
|
||||||
------------
|
------------
|
||||||
C++ version (sfm):
|
C++ version (sfm):
|
||||||
- ncursesw (libncursesw)
|
- A curses library: ncursesw, ncurses, or netbsd-curses (libcurses)
|
||||||
- A C++17 compiler (g++ or compatible)
|
- A C++17 compiler (g++ or compatible)
|
||||||
|
|
||||||
Shell version (sfm.sh):
|
Shell version (sfm.sh):
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// sfm - Simple File Manager in C++17 with ncurses
|
// sfm - Simple File Manager in C++17 with curses
|
||||||
|
// Works with both ncurses and netbsd-curses.
|
||||||
// Rewrite of the POSIX sh original for speed and efficiency
|
// Rewrite of the POSIX sh original for speed and efficiency
|
||||||
#include <ncurses.h>
|
#include <curses.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
@@ -18,6 +19,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -27,6 +29,140 @@ namespace fs = std::filesystem;
|
|||||||
// ─── terminal helpers ───────────────────────────────────────────────────────
|
// ─── terminal helpers ───────────────────────────────────────────────────────
|
||||||
[[nodiscard]] bool can_color() { return has_colors(); }
|
[[nodiscard]] bool can_color() { return has_colors(); }
|
||||||
|
|
||||||
|
// ─── key input normalization ────────────────────────────────────────────────
|
||||||
|
// ncurses decodes escape sequences (arrows, Home/End, ...) into KEY_* codes
|
||||||
|
// itself. netbsd-curses returns the raw bytes (e.g. ESC [ B for the down
|
||||||
|
// arrow), which would leak stray characters into the command handling.
|
||||||
|
// get_key() presents KEY_* codes (or 27 for a bare ESC) on both.
|
||||||
|
|
||||||
|
#ifdef NCURSES_VERSION
|
||||||
|
|
||||||
|
static int get_key() { return getch(); }
|
||||||
|
|
||||||
|
#else // manual escape-sequence decoding (netbsd-curses etc.)
|
||||||
|
|
||||||
|
static void nap_ms(int ms) {
|
||||||
|
struct timespec ts;
|
||||||
|
ts.tv_sec = ms / 1000;
|
||||||
|
ts.tv_nsec = (ms % 1000) * 1000000L;
|
||||||
|
::nanosleep(&ts, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int g_pushback[32];
|
||||||
|
static int g_pushback_n = 0;
|
||||||
|
|
||||||
|
static void queue_key(int k) {
|
||||||
|
if (g_pushback_n < static_cast<int>(sizeof(g_pushback) / sizeof(g_pushback[0])))
|
||||||
|
g_pushback[g_pushback_n++] = k;
|
||||||
|
}
|
||||||
|
|
||||||
|
// seq holds the bytes after ESC; seq[0] is '[' (CSI) or 'O' (SS3).
|
||||||
|
static int decode_escape_seq(const std::string &seq) {
|
||||||
|
if (seq.size() < 2) return ERR;
|
||||||
|
char lead = seq[0];
|
||||||
|
char fin = seq.back();
|
||||||
|
if (lead == 'O') {
|
||||||
|
if (seq.size() != 2) return ERR;
|
||||||
|
switch (fin) {
|
||||||
|
case 'A': return KEY_UP;
|
||||||
|
case 'B': return KEY_DOWN;
|
||||||
|
case 'C': return KEY_RIGHT;
|
||||||
|
case 'D': return KEY_LEFT;
|
||||||
|
case 'H': return KEY_HOME;
|
||||||
|
case 'F': return KEY_END;
|
||||||
|
case 'P': case 'Q': case 'R': case 'S': return KEY_F(fin - 'P' + 1);
|
||||||
|
}
|
||||||
|
return ERR;
|
||||||
|
}
|
||||||
|
if (lead != '[') return ERR;
|
||||||
|
switch (fin) {
|
||||||
|
case 'A': return KEY_UP;
|
||||||
|
case 'B': return KEY_DOWN;
|
||||||
|
case 'C': return KEY_RIGHT;
|
||||||
|
case 'D': return KEY_LEFT;
|
||||||
|
case 'H': return KEY_HOME;
|
||||||
|
case 'F': return KEY_END;
|
||||||
|
case 'Z': return KEY_BTAB;
|
||||||
|
}
|
||||||
|
// CSI n~ : Home/Insert/Delete/End/PgUp/PgDn (single digit only; multi-
|
||||||
|
// digit and modified variants fall through and are swallowed).
|
||||||
|
if (seq.size() == 3 && fin == '~' && seq[1] >= '0' && seq[1] <= '9') {
|
||||||
|
switch (seq[1]) {
|
||||||
|
case '1': case '7': return KEY_HOME;
|
||||||
|
case '2': return KEY_IC;
|
||||||
|
case '3': return KEY_DC;
|
||||||
|
case '4': case '8': return KEY_END;
|
||||||
|
case '5': return KEY_PPAGE;
|
||||||
|
case '6': return KEY_NPAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Like getch(), but assembles raw ESC sequences into KEY_* codes. A bare ESC
|
||||||
|
// is returned as 27 (possibly after a short wait for the rest of a sequence).
|
||||||
|
static int get_key() {
|
||||||
|
if (g_pushback_n > 0) return g_pushback[--g_pushback_n];
|
||||||
|
|
||||||
|
int k = getch();
|
||||||
|
if (k != 27) return k;
|
||||||
|
|
||||||
|
// ESC — check whether an escape sequence follows. Real terminals send
|
||||||
|
// the whole sequence in one burst, so the bytes are already queued.
|
||||||
|
nodelay(stdscr, TRUE);
|
||||||
|
std::string seq;
|
||||||
|
int c;
|
||||||
|
for (int i = 0; i < 4 && seq.empty(); ++i) {
|
||||||
|
c = getch();
|
||||||
|
if (c == ERR) nap_ms(10);
|
||||||
|
else seq += static_cast<char>(c);
|
||||||
|
}
|
||||||
|
if (seq.empty()) { nodelay(stdscr, FALSE); return 27; } // bare ESC
|
||||||
|
|
||||||
|
if (seq[0] == '[') {
|
||||||
|
// CSI: parameter/intermediate bytes, then a final byte (0x40-0x7E).
|
||||||
|
// The lead '[' itself is in that range, so collect at least one more.
|
||||||
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
if (seq.size() >= 2 && seq.back() >= 0x40 && seq.back() <= 0x7E) break;
|
||||||
|
c = getch();
|
||||||
|
if (c == ERR) { nap_ms(10); c = getch(); }
|
||||||
|
if (c == ERR) break;
|
||||||
|
seq += static_cast<char>(c);
|
||||||
|
}
|
||||||
|
} else if (seq[0] == 'O') {
|
||||||
|
// SS3: exactly one final byte follows.
|
||||||
|
for (int i = 0; i < 4 && seq.size() < 2; ++i) {
|
||||||
|
c = getch();
|
||||||
|
if (c == ERR) { nap_ms(10); c = getch(); }
|
||||||
|
if (c == ERR) break;
|
||||||
|
seq += static_cast<char>(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nodelay(stdscr, FALSE);
|
||||||
|
|
||||||
|
int key = decode_escape_seq(seq);
|
||||||
|
if (key == ERR) {
|
||||||
|
bool complete = seq.back() >= 0x40 && seq.back() <= 0x7E;
|
||||||
|
if (!complete) {
|
||||||
|
// Truncated sequence — replay it as raw keys (ESC first).
|
||||||
|
for (size_t i = seq.size(); i-- > 0;)
|
||||||
|
queue_key(static_cast<unsigned char>(seq[i]));
|
||||||
|
queue_key(27);
|
||||||
|
return 27;
|
||||||
|
}
|
||||||
|
// Unknown but well-formed sequence (e.g. an F-key): swallow it,
|
||||||
|
// like curses implementations do.
|
||||||
|
return ERR;
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
// netbsd-curses never reports KEY_RESIZE; catch SIGWINCH ourselves.
|
||||||
|
static volatile sig_atomic_t g_winch = 0;
|
||||||
|
static void on_sigwinch(int) { g_winch = 1; }
|
||||||
|
|
||||||
|
#endif // NCURSES_VERSION
|
||||||
|
|
||||||
// Color pair indices
|
// Color pair indices
|
||||||
enum {
|
enum {
|
||||||
CP_DIR = 1,
|
CP_DIR = 1,
|
||||||
@@ -200,6 +336,9 @@ private:
|
|||||||
const std::vector<std::string> &items,
|
const std::vector<std::string> &items,
|
||||||
bool allow_cancel = true);
|
bool allow_cancel = true);
|
||||||
|
|
||||||
|
// ── input ──────────────────────────────────────────────────────────────
|
||||||
|
bool handle_key(int key); // returns false to quit
|
||||||
|
|
||||||
// ── line input ─────────────────────────────────────────────────────────
|
// ── line input ─────────────────────────────────────────────────────────
|
||||||
bool read_line(std::string &out, const std::string &prompt,
|
bool read_line(std::string &out, const std::string &prompt,
|
||||||
const std::string &initial = "");
|
const std::string &initial = "");
|
||||||
@@ -945,7 +1084,7 @@ bool FileManager::confirm_overlay(const std::string &prompt) {
|
|||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
int ch = getch();
|
int ch = get_key();
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case KEY_LEFT: case 'h': sel_yes = true; break;
|
case KEY_LEFT: case 'h': sel_yes = true; break;
|
||||||
case KEY_RIGHT: case 'l': sel_yes = false; break;
|
case KEY_RIGHT: case 'l': sel_yes = false; break;
|
||||||
@@ -956,18 +1095,7 @@ bool FileManager::confirm_overlay(const std::string &prompt) {
|
|||||||
case '\n': case '\r': case KEY_ENTER:
|
case '\n': case '\r': case KEY_ENTER:
|
||||||
need_full_redraw_ = true;
|
need_full_redraw_ = true;
|
||||||
return sel_yes;
|
return sel_yes;
|
||||||
case 27: {
|
case 27: need_full_redraw_ = true; return false;
|
||||||
nodelay(stdscr, TRUE);
|
|
||||||
int n = getch();
|
|
||||||
nodelay(stdscr, FALSE);
|
|
||||||
if (n == ERR) { need_full_redraw_ = true; return false; }
|
|
||||||
if (n == '[') {
|
|
||||||
int m = getch();
|
|
||||||
if (m == 'D' || m == 'C') { sel_yes = !sel_yes; break; }
|
|
||||||
}
|
|
||||||
need_full_redraw_ = true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1066,7 +1194,7 @@ std::string FileManager::overlay_picker(const std::string &title,
|
|||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
int ch = getch();
|
int ch = get_key();
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'j': case KEY_DOWN: psel++; break;
|
case 'j': case KEY_DOWN: psel++; break;
|
||||||
case 'k': case KEY_UP: psel--; break;
|
case 'k': case KEY_UP: psel--; break;
|
||||||
@@ -1077,20 +1205,7 @@ std::string FileManager::overlay_picker(const std::string &title,
|
|||||||
case 'q': case 'Q': case 'h':
|
case 'q': case 'Q': case 'h':
|
||||||
if (allow_cancel) { need_full_redraw_ = true; return ""; }
|
if (allow_cancel) { need_full_redraw_ = true; return ""; }
|
||||||
break;
|
break;
|
||||||
case 27: {
|
case 27: need_full_redraw_ = true; return "";
|
||||||
nodelay(stdscr, TRUE);
|
|
||||||
int n = getch();
|
|
||||||
nodelay(stdscr, FALSE);
|
|
||||||
if (n == ERR) { need_full_redraw_ = true; return ""; }
|
|
||||||
if (n == '[') {
|
|
||||||
int m = getch();
|
|
||||||
if (m == 'A') psel--;
|
|
||||||
else if (m == 'B') psel++;
|
|
||||||
else if (m == 'D') { need_full_redraw_ = true; return ""; }
|
|
||||||
else { need_full_redraw_ = true; return ""; }
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1126,18 +1241,12 @@ bool FileManager::read_line(std::string &out, const std::string &prompt,
|
|||||||
move(bot, static_cast<int>(prompt.size() + pos));
|
move(bot, static_cast<int>(prompt.size() + pos));
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
int ch = getch();
|
int ch = get_key();
|
||||||
if (ch == 27) { // ESC
|
if (ch == 27) { // bare ESC — cancel
|
||||||
nodelay(stdscr, TRUE);
|
|
||||||
int n = getch();
|
|
||||||
nodelay(stdscr, FALSE);
|
|
||||||
if (n == ERR) {
|
|
||||||
leaveok(stdscr, TRUE);
|
leaveok(stdscr, TRUE);
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
out.clear();
|
out.clear();
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
continue;
|
|
||||||
} else if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
|
} else if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
|
||||||
leaveok(stdscr, TRUE);
|
leaveok(stdscr, TRUE);
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
@@ -1307,20 +1416,14 @@ void FileManager::do_search() {
|
|||||||
need_full_redraw_ = true;
|
need_full_redraw_ = true;
|
||||||
draw();
|
draw();
|
||||||
|
|
||||||
int ch = getch();
|
int ch = get_key();
|
||||||
if (ch == 27) { // ESC
|
if (ch == 27) {
|
||||||
nodelay(stdscr, TRUE);
|
|
||||||
int n = getch();
|
|
||||||
nodelay(stdscr, FALSE);
|
|
||||||
if (n == ERR) {
|
|
||||||
// Bare ESC — clear filter and exit search
|
// Bare ESC — clear filter and exit search
|
||||||
filter_.clear();
|
filter_.clear();
|
||||||
searching_ = false;
|
searching_ = false;
|
||||||
apply_filter();
|
apply_filter();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
need_full_redraw_ = true;
|
need_full_redraw_ = true;
|
||||||
}
|
|
||||||
// else: arrow key or other sequence — ignore
|
|
||||||
} else if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
|
} else if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) {
|
||||||
searching_ = false;
|
searching_ = false;
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
@@ -1475,13 +1578,7 @@ void FileManager::do_info() {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
// Wait for any key
|
// Wait for any key
|
||||||
nodelay(stdscr, FALSE);
|
get_key();
|
||||||
int ch = getch();
|
|
||||||
if (ch == 27) {
|
|
||||||
nodelay(stdscr, TRUE);
|
|
||||||
getch();
|
|
||||||
nodelay(stdscr, FALSE);
|
|
||||||
}
|
|
||||||
need_full_redraw_ = true;
|
need_full_redraw_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1598,13 +1695,7 @@ void FileManager::do_help() {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
// Wait for any key
|
// Wait for any key
|
||||||
nodelay(stdscr, FALSE);
|
get_key();
|
||||||
int ch = getch();
|
|
||||||
if (ch == 27) {
|
|
||||||
nodelay(stdscr, TRUE);
|
|
||||||
getch();
|
|
||||||
nodelay(stdscr, FALSE);
|
|
||||||
}
|
|
||||||
need_full_redraw_ = true;
|
need_full_redraw_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2327,24 +2418,7 @@ void FileManager::open_file(const std::string &path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ─── main event loop ────────────────────────────────────────────────────────
|
// ─── main event loop ────────────────────────────────────────────────────────
|
||||||
void FileManager::run() {
|
bool FileManager::handle_key(int key) {
|
||||||
setup_term();
|
|
||||||
if (can_color()) init_colors();
|
|
||||||
load_entries();
|
|
||||||
|
|
||||||
is_root_ = (geteuid() == 0);
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
draw();
|
|
||||||
int key = getch();
|
|
||||||
|
|
||||||
// Handle terminal resize
|
|
||||||
if (key == KEY_RESIZE) {
|
|
||||||
update_size();
|
|
||||||
need_full_redraw_ = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'j': case KEY_DOWN:
|
case 'j': case KEY_DOWN:
|
||||||
if (!entries_.empty() && sel_ < static_cast<int>(entries_.size()) - 1)
|
if (!entries_.empty() && sel_ < static_cast<int>(entries_.size()) - 1)
|
||||||
@@ -2399,10 +2473,57 @@ void FileManager::run() {
|
|||||||
case 'r': do_rename(); break;
|
case 'r': do_rename(); break;
|
||||||
case 'm': do_mkdir(); break;
|
case 'm': do_mkdir(); break;
|
||||||
case 'n': do_newfile(); break;
|
case 'n': do_newfile(); break;
|
||||||
case 'q': case 'Q': return;
|
case 'q': case 'Q': return false;
|
||||||
case '!': do_shell(); break;
|
case '!': do_shell(); break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileManager::run() {
|
||||||
|
setup_term();
|
||||||
|
if (can_color()) init_colors();
|
||||||
|
load_entries();
|
||||||
|
|
||||||
|
is_root_ = (geteuid() == 0);
|
||||||
|
|
||||||
|
#ifndef NCURSES_VERSION
|
||||||
|
struct sigaction sa;
|
||||||
|
std::memset(&sa, 0, sizeof(sa));
|
||||||
|
sa.sa_handler = on_sigwinch;
|
||||||
|
sigaction(SIGWINCH, &sa, nullptr);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
draw();
|
||||||
|
while (true) {
|
||||||
|
#ifndef NCURSES_VERSION
|
||||||
|
// netbsd-curses never delivers KEY_RESIZE; poll for SIGWINCH and
|
||||||
|
// re-query the terminal size ourselves.
|
||||||
|
if (g_winch) {
|
||||||
|
g_winch = 0;
|
||||||
|
endwin(); // endwin()+refresh() makes curses re-read the size
|
||||||
|
refresh();
|
||||||
|
need_full_redraw_ = true;
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
nodelay(stdscr, TRUE);
|
||||||
|
#endif
|
||||||
|
int key = get_key();
|
||||||
|
#ifndef NCURSES_VERSION
|
||||||
|
nodelay(stdscr, FALSE);
|
||||||
|
if (key == ERR) { nap_ms(20); continue; }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Handle terminal resize (ncurses)
|
||||||
|
if (key == KEY_RESIZE) {
|
||||||
|
update_size();
|
||||||
|
need_full_redraw_ = true;
|
||||||
|
draw();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!handle_key(key)) return;
|
||||||
|
draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user