mirror of
https://github.com/jarun/nnn.git
synced 2024-11-18 17:09:14 +00:00
Refactor
This commit is contained in:
parent
1b3b9f503d
commit
71bf18cee3
132
src/nnn.c
132
src/nnn.c
|
@ -410,6 +410,7 @@ static char mv[] = "mv -i";
|
||||||
#define NONE_SELECTED 4
|
#define NONE_SELECTED 4
|
||||||
#define UTIL_MISSING 5
|
#define UTIL_MISSING 5
|
||||||
#define OPERATION_FAILED 6
|
#define OPERATION_FAILED 6
|
||||||
|
#define SESSION_NAME 7
|
||||||
|
|
||||||
static const char * const messages[] = {
|
static const char * const messages[] = {
|
||||||
"no traversal",
|
"no traversal",
|
||||||
|
@ -419,6 +420,7 @@ static const char * const messages[] = {
|
||||||
"0 selected",
|
"0 selected",
|
||||||
"missing dep",
|
"missing dep",
|
||||||
"failed!",
|
"failed!",
|
||||||
|
"session name: ",
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Supported configuration environment variables */
|
/* Supported configuration environment variables */
|
||||||
|
@ -2028,7 +2030,7 @@ end:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show a prompt with input string and return the changes */
|
/* Show a prompt with input string and return the changes */
|
||||||
static char *xreadline(char *prefill, char *prompt)
|
static char *xreadline(const char *prefill, const char *prompt)
|
||||||
{
|
{
|
||||||
size_t len, pos;
|
size_t len, pos;
|
||||||
int x, r;
|
int x, r;
|
||||||
|
@ -2695,11 +2697,11 @@ static void savecurctx(settings *curcfg, char *path, char *curname, int r /* nex
|
||||||
|
|
||||||
static void save_session(bool last_session, int *presel)
|
static void save_session(bool last_session, int *presel)
|
||||||
{
|
{
|
||||||
char session_path[PATH_MAX + 1];
|
char spath[PATH_MAX];
|
||||||
int status = _FAILURE;
|
|
||||||
int i;
|
int i;
|
||||||
session_header_t header;
|
session_header_t header;
|
||||||
char *session_name;
|
char *sname;
|
||||||
|
bool status = FALSE;
|
||||||
|
|
||||||
header.ver = SESSIONS_VERSION;
|
header.ver = SESSIONS_VERSION;
|
||||||
|
|
||||||
|
@ -2715,13 +2717,11 @@ static void save_session(bool last_session, int *presel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
session_name = !last_session ? xreadline("", "session name: ") : "@";
|
sname = !last_session ? xreadline(NULL, messages[SESSION_NAME]) : "@";
|
||||||
if (session_name[0] != '\0')
|
if (!sname[0])
|
||||||
mkpath(sessiondir, session_name, session_path);
|
|
||||||
else
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
FILE *fsession = fopen(session_path, "wb");
|
FILE *fsession = fopen(spath, "wb");
|
||||||
if (!fsession) {
|
if (!fsession) {
|
||||||
printwait("failed to open session file", presel);
|
printwait("failed to open session file", presel);
|
||||||
return;
|
return;
|
||||||
|
@ -2740,40 +2740,39 @@ static void save_session(bool last_session, int *presel)
|
||||||
|| (header.pathln[i] > 0 && fwrite(g_ctx[i].c_path, header.pathln[i], 1, fsession) != 1))
|
|| (header.pathln[i] > 0 && fwrite(g_ctx[i].c_path, header.pathln[i], 1, fsession) != 1))
|
||||||
goto END;
|
goto END;
|
||||||
|
|
||||||
status = _SUCCESS;
|
status = TRUE;
|
||||||
|
|
||||||
END:
|
END:
|
||||||
fclose(fsession);
|
fclose(fsession);
|
||||||
|
|
||||||
if (status == _FAILURE)
|
if (!status)
|
||||||
printwait("failed to write session data", presel);
|
printwait("failed to write session data", presel);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool load_session(const char *session_name, char **path, char **lastdir
|
static bool load_session(const char *sname, char **path, char **lastdir, char **lastname, bool restore) {
|
||||||
, char **lastname, bool restore_session) {
|
char spath[PATH_MAX];
|
||||||
char session_path[PATH_MAX + 1];
|
|
||||||
int status = _FAILURE;
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
session_header_t header;
|
session_header_t header;
|
||||||
bool has_loaded_dynamically = !(session_name || restore_session);
|
bool has_loaded_dynamically = !(sname || restore);
|
||||||
|
bool status = FALSE;
|
||||||
|
|
||||||
if (!restore_session) {
|
if (!restore) {
|
||||||
session_name = session_name ? session_name : xreadline("", "session name: ");
|
sname = sname ? sname : xreadline(NULL, messages[SESSION_NAME]);
|
||||||
if (session_name[0] != '\0')
|
if (!sname[0])
|
||||||
mkpath(sessiondir, session_name ? session_name : xreadline("", "session name: "), session_path);
|
return FALSE;
|
||||||
else
|
|
||||||
return _FAILURE;
|
mkpath(sessiondir, sname ? sname : xreadline(NULL, messages[SESSION_NAME]), spath);
|
||||||
} else
|
} else
|
||||||
mkpath(sessiondir, "@", session_path);
|
mkpath(sessiondir, "@", spath);
|
||||||
|
|
||||||
if (has_loaded_dynamically)
|
if (has_loaded_dynamically)
|
||||||
save_session(TRUE, NULL);
|
save_session(TRUE, NULL);
|
||||||
|
|
||||||
FILE *fsession = fopen(session_path, "rb");
|
FILE *fsession = fopen(spath, "rb");
|
||||||
if (!fsession) {
|
if (!fsession) {
|
||||||
printmsg("failed to open session file");
|
printmsg("failed to open session file");
|
||||||
xdelay();
|
xdelay();
|
||||||
return _FAILURE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((fread(&header, sizeof(header), 1, fsession) != 1)
|
if ((fread(&header, sizeof(header), 1, fsession) != 1)
|
||||||
|
@ -2796,12 +2795,12 @@ static bool load_session(const char *session_name, char **path, char **lastdir
|
||||||
*path = g_ctx[cfg.curctx].c_path;
|
*path = g_ctx[cfg.curctx].c_path;
|
||||||
*lastdir = g_ctx[cfg.curctx].c_last;
|
*lastdir = g_ctx[cfg.curctx].c_last;
|
||||||
*lastname = g_ctx[cfg.curctx].c_name;
|
*lastname = g_ctx[cfg.curctx].c_name;
|
||||||
status = _SUCCESS;
|
status = TRUE;
|
||||||
|
|
||||||
END:
|
END:
|
||||||
fclose(fsession);
|
fclose(fsession);
|
||||||
|
|
||||||
if (status == _FAILURE) {
|
if (!status) {
|
||||||
printmsg("failed to read session data");
|
printmsg("failed to read session data");
|
||||||
xdelay();
|
xdelay();
|
||||||
}
|
}
|
||||||
|
@ -3910,7 +3909,7 @@ static void browse(char *ipath, const char *session)
|
||||||
xcols = COLS;
|
xcols = COLS;
|
||||||
|
|
||||||
/* setup first context */
|
/* setup first context */
|
||||||
if (!session || load_session(session, &path, &lastdir, &lastname, FALSE) == _FAILURE) {
|
if (!session || !load_session(session, &path, &lastdir, &lastname, FALSE)) {
|
||||||
xstrlcpy(g_ctx[0].c_path, ipath, PATH_MAX); /* current directory */
|
xstrlcpy(g_ctx[0].c_path, ipath, PATH_MAX); /* current directory */
|
||||||
path = g_ctx[0].c_path;
|
path = g_ctx[0].c_path;
|
||||||
g_ctx[0].c_last[0] = g_ctx[0].c_name[0] = '\0';
|
g_ctx[0].c_last[0] = g_ctx[0].c_name[0] = '\0';
|
||||||
|
@ -4465,28 +4464,28 @@ nochange:
|
||||||
copycurname();
|
copycurname();
|
||||||
goto begin;
|
goto begin;
|
||||||
case SEL_STATS:
|
case SEL_STATS:
|
||||||
if (!ndents)
|
if (ndents) {
|
||||||
break;
|
mkpath(path, dents[cur].name, newpath);
|
||||||
|
if (lstat(newpath, &sb) == -1 || !show_stats(newpath, &sb)) {
|
||||||
mkpath(path, dents[cur].name, newpath);
|
printwarn(&presel);
|
||||||
if (lstat(newpath, &sb) == -1 || !show_stats(newpath, &sb)) {
|
goto nochange;
|
||||||
printwarn(&presel);
|
}
|
||||||
goto nochange;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SEL_ARCHIVELS: // fallthrough
|
case SEL_ARCHIVELS: // fallthrough
|
||||||
case SEL_EXTRACT: // fallthrough
|
case SEL_EXTRACT: // fallthrough
|
||||||
case SEL_RUNEDIT: // fallthrough
|
|
||||||
case SEL_RUNPAGE:
|
|
||||||
if (!ndents)
|
|
||||||
break; // fallthrough
|
|
||||||
case SEL_REDRAW: // fallthrough
|
case SEL_REDRAW: // fallthrough
|
||||||
case SEL_RENAMEMUL: // fallthrough
|
case SEL_RENAMEMUL: // fallthrough
|
||||||
case SEL_HELP: // fallthrough
|
case SEL_HELP: // fallthrough
|
||||||
|
case SEL_RUNEDIT: // fallthrough
|
||||||
|
case SEL_RUNPAGE: // fallthrough
|
||||||
case SEL_LOCK:
|
case SEL_LOCK:
|
||||||
{
|
{
|
||||||
if (ndents)
|
if (ndents)
|
||||||
mkpath(path, dents[cur].name, newpath);
|
mkpath(path, dents[cur].name, newpath);
|
||||||
|
else if (sel == SEL_ARCHIVELS || sel == SEL_EXTRACT
|
||||||
|
|| sel == SEL_RUNEDIT || sel == SEL_RUNPAGE)
|
||||||
|
break;
|
||||||
|
|
||||||
switch (sel) {
|
switch (sel) {
|
||||||
case SEL_ARCHIVELS:
|
case SEL_ARCHIVELS:
|
||||||
|
@ -4496,9 +4495,7 @@ nochange:
|
||||||
handle_archive(newpath, path, 'x');
|
handle_archive(newpath, path, 'x');
|
||||||
break;
|
break;
|
||||||
case SEL_REDRAW:
|
case SEL_REDRAW:
|
||||||
if (ndents)
|
break;
|
||||||
copycurname();
|
|
||||||
goto begin;
|
|
||||||
case SEL_RENAMEMUL:
|
case SEL_RENAMEMUL:
|
||||||
endselection();
|
endselection();
|
||||||
|
|
||||||
|
@ -4524,7 +4521,7 @@ nochange:
|
||||||
/* In case of successful operation, reload contents */
|
/* In case of successful operation, reload contents */
|
||||||
|
|
||||||
/* Continue in navigate-as-you-type mode, if enabled */
|
/* Continue in navigate-as-you-type mode, if enabled */
|
||||||
if (cfg.filtermode)
|
if (cfg.filtermode && sel != SEL_REDRAW)
|
||||||
presel = FILTER;
|
presel = FILTER;
|
||||||
|
|
||||||
/* Save current */
|
/* Save current */
|
||||||
|
@ -4667,15 +4664,16 @@ nochange:
|
||||||
presel = FILTER;
|
presel = FILTER;
|
||||||
goto begin;
|
goto begin;
|
||||||
}
|
}
|
||||||
case SEL_OPENWITH: // fallthrough
|
|
||||||
case SEL_RENAME:
|
|
||||||
if (!ndents)
|
|
||||||
break; // fallthrough
|
|
||||||
case SEL_ARCHIVE: // fallthrough
|
case SEL_ARCHIVE: // fallthrough
|
||||||
case SEL_NEW:
|
case SEL_OPENWITH: // fallthrough
|
||||||
|
case SEL_NEW: // fallthrough
|
||||||
|
case SEL_RENAME:
|
||||||
{
|
{
|
||||||
int dup = 'n';
|
int dup = 'n';
|
||||||
|
|
||||||
|
if (!ndents && (sel == SEL_OPENWITH || sel == SEL_RENAME))
|
||||||
|
break;
|
||||||
|
|
||||||
switch (sel) {
|
switch (sel) {
|
||||||
case SEL_ARCHIVE:
|
case SEL_ARCHIVE:
|
||||||
r = get_input("archive selection (else current)? [y/Y confirms]");
|
r = get_input("archive selection (else current)? [y/Y confirms]");
|
||||||
|
@ -4973,9 +4971,27 @@ nochange:
|
||||||
tmp = ndents ? dents[cur].name : NULL;
|
tmp = ndents ? dents[cur].name : NULL;
|
||||||
unmount(tmp, newpath, &presel, path);
|
unmount(tmp, newpath, &presel, path);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
|
case SEL_SESSIONS:
|
||||||
|
r = get_input("'s'(ave) / 'l'(oad) / 'r'(estore) session?");
|
||||||
|
|
||||||
|
if (r == 's') {
|
||||||
|
save_session(FALSE, &presel);
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r == 'l' || r == 'r') {
|
||||||
|
if (load_session(NULL, &path, &lastdir, &lastname, r == 'r')) {
|
||||||
|
setdirwatch();
|
||||||
|
goto begin;
|
||||||
|
}
|
||||||
|
|
||||||
|
presel = MSGWAIT;
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SEL_QUITCTX: // fallthrough
|
||||||
case SEL_QUITCD: // fallthrough
|
case SEL_QUITCD: // fallthrough
|
||||||
case SEL_QUIT: // fallthrough
|
case SEL_QUIT:
|
||||||
case SEL_QUITCTX:
|
|
||||||
if (sel == SEL_QUITCTX) {
|
if (sel == SEL_QUITCTX) {
|
||||||
fd = cfg.curctx; /* fd used as tmp var */
|
fd = cfg.curctx; /* fd used as tmp var */
|
||||||
for (r = (fd + 1) & ~CTX_MAX;
|
for (r = (fd + 1) & ~CTX_MAX;
|
||||||
|
@ -5030,22 +5046,6 @@ nochange:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case SEL_SESSIONS:
|
|
||||||
r = get_input("'s'(ave) / 'l'(oad) / 'r'(estore) session?");
|
|
||||||
|
|
||||||
if (r == 's') {
|
|
||||||
save_session(FALSE, &presel);
|
|
||||||
goto nochange;
|
|
||||||
} else if (r == 'l' || r == 'r') {
|
|
||||||
if (load_session(NULL, &path, &lastdir, &lastname, r == 'r') == _SUCCESS) {
|
|
||||||
setdirwatch();
|
|
||||||
goto begin;
|
|
||||||
}
|
|
||||||
|
|
||||||
presel = MSGWAIT;
|
|
||||||
goto nochange;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
if (xlines != LINES || xcols != COLS) {
|
if (xlines != LINES || xcols != COLS) {
|
||||||
idle = 0;
|
idle = 0;
|
||||||
|
|
|
@ -102,11 +102,11 @@ enum action {
|
||||||
SEL_RUNEDIT,
|
SEL_RUNEDIT,
|
||||||
SEL_RUNPAGE,
|
SEL_RUNPAGE,
|
||||||
SEL_LOCK,
|
SEL_LOCK,
|
||||||
|
SEL_SESSIONS,
|
||||||
SEL_QUITCTX,
|
SEL_QUITCTX,
|
||||||
SEL_QUITCD,
|
SEL_QUITCD,
|
||||||
SEL_QUIT,
|
SEL_QUIT,
|
||||||
SEL_CLICK,
|
SEL_CLICK,
|
||||||
SEL_SESSIONS,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Associate a pressed key to an action */
|
/* Associate a pressed key to an action */
|
||||||
|
|
Loading…
Reference in a new issue