mirror of
https://github.com/jarun/nnn.git
synced 2025-02-26 03:01:32 +00:00
More optimization
This commit is contained in:
parent
76d600b072
commit
e585d830cd
1 changed files with 36 additions and 47 deletions
83
nnn.c
83
nnn.c
|
@ -163,12 +163,10 @@ extern int wget_wch(WINDOW *win, wint_t *wch);
|
||||||
/* Configuration */
|
/* Configuration */
|
||||||
static settings cfg = {0, 0, 0, 0, 0, 1, 1, 0};
|
static settings cfg = {0, 0, 0, 0, 0, 1, 1, 0};
|
||||||
|
|
||||||
/* Idle timeout in seconds, 0 to disable */
|
|
||||||
static uint idletimeout;
|
|
||||||
|
|
||||||
static struct entry *dents;
|
static struct entry *dents;
|
||||||
static int ndents, cur, total_dents;
|
static int ndents, cur, total_dents;
|
||||||
static uint idle;
|
static uint idle;
|
||||||
|
static uint idletimeout;
|
||||||
static char *player;
|
static char *player;
|
||||||
static char *copier;
|
static char *copier;
|
||||||
static char *editor;
|
static char *editor;
|
||||||
|
@ -200,6 +198,11 @@ static char *metaviewer;
|
||||||
/* For use in functions which are isolated and don't return the buffer */
|
/* For use in functions which are isolated and don't return the buffer */
|
||||||
static char g_buf[MAX_CMD_LEN];
|
static char g_buf[MAX_CMD_LEN];
|
||||||
|
|
||||||
|
/* Common message strings */
|
||||||
|
static char *STR_NFTWFAIL = "nftw(3) failed";
|
||||||
|
static char *STR_ATROOT = "You are at /";
|
||||||
|
static char *STR_NOHOME = "HOME not set";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Layout:
|
* Layout:
|
||||||
* .---------
|
* .---------
|
||||||
|
@ -457,9 +460,9 @@ initcurses(void)
|
||||||
char *term = getenv("TERM");
|
char *term = getenv("TERM");
|
||||||
|
|
||||||
if (term != NULL)
|
if (term != NULL)
|
||||||
fprintf(stderr, "error opening terminal: %s\n", term);
|
fprintf(stderr, "error opening TERM: %s\n", term);
|
||||||
else
|
else
|
||||||
fprintf(stderr, "failed to initialize curses\n");
|
fprintf(stderr, "initscr() failed\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
cbreak();
|
cbreak();
|
||||||
|
@ -475,13 +478,6 @@ initcurses(void)
|
||||||
timeout(1000); /* One second */
|
timeout(1000); /* One second */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Exit curses mode */
|
|
||||||
static void
|
|
||||||
exitcurses(void)
|
|
||||||
{
|
|
||||||
endwin(); /* Restore terminal */
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Spawns a child process. Behaviour can be controlled using flag.
|
* Spawns a child process. Behaviour can be controlled using flag.
|
||||||
* Limited to 2 arguments to a program, flag works on bit set.
|
* Limited to 2 arguments to a program, flag works on bit set.
|
||||||
|
@ -493,7 +489,7 @@ spawn(char *file, char *arg1, char *arg2, char *dir, uchar flag)
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
if (flag & F_NORMAL)
|
if (flag & F_NORMAL)
|
||||||
exitcurses();
|
endwin();
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
|
@ -719,7 +715,7 @@ printwarn(void)
|
||||||
static void
|
static void
|
||||||
printerr(int ret, char *prefix)
|
printerr(int ret, char *prefix)
|
||||||
{
|
{
|
||||||
exitcurses();
|
endwin();
|
||||||
fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
|
fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
|
||||||
exit(ret);
|
exit(ret);
|
||||||
}
|
}
|
||||||
|
@ -772,6 +768,16 @@ nextsel(char **run, char **env, int *presel)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
dentcpy(struct entry *dst, struct entry *src)
|
||||||
|
{
|
||||||
|
xstrlcpy(dst->name, src->name, NAME_MAX);
|
||||||
|
dst->mode = src->mode;
|
||||||
|
dst->t = src->t;
|
||||||
|
dst->size = src->size;
|
||||||
|
dst->blocks = src->blocks;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Move non-matching entries to the end
|
* Move non-matching entries to the end
|
||||||
*/
|
*/
|
||||||
|
@ -787,29 +793,13 @@ fill(struct entry **dents,
|
||||||
static struct entry _dent;
|
static struct entry _dent;
|
||||||
|
|
||||||
/* Copy count to tmp */
|
/* Copy count to tmp */
|
||||||
xstrlcpy(_dent.name, (*dents)[count].name,
|
dentcpy(&_dent, &(*dents)[count]);
|
||||||
NAME_MAX);
|
|
||||||
_dent.mode = (*dents)[count].mode;
|
|
||||||
_dent.t = (*dents)[count].t;
|
|
||||||
_dent.size = (*dents)[count].size;
|
|
||||||
_dent.blocks = (*dents)[count].blocks;
|
|
||||||
|
|
||||||
/* Copy ndents - 1 to count */
|
/* Copy ndents - 1 to count */
|
||||||
xstrlcpy((*dents)[count].name,
|
dentcpy(&(*dents)[count], &(*dents)[ndents]);
|
||||||
(*dents)[ndents].name, NAME_MAX);
|
|
||||||
(*dents)[count].mode = (*dents)[ndents].mode;
|
|
||||||
(*dents)[count].t = (*dents)[ndents].t;
|
|
||||||
(*dents)[count].size = (*dents)[ndents].size;
|
|
||||||
(*dents)[count].blocks
|
|
||||||
= (*dents)[ndents].blocks;
|
|
||||||
|
|
||||||
/* Copy tmp to ndents - 1 */
|
/* Copy tmp to ndents - 1 */
|
||||||
xstrlcpy((*dents)[ndents].name, _dent.name,
|
dentcpy(&(*dents)[ndents], &_dent);
|
||||||
NAME_MAX);
|
|
||||||
(*dents)[ndents].mode = _dent.mode;
|
|
||||||
(*dents)[ndents].t = _dent.t;
|
|
||||||
(*dents)[ndents].size = _dent.size;
|
|
||||||
(*dents)[ndents].blocks = _dent.blocks;
|
|
||||||
|
|
||||||
--count;
|
--count;
|
||||||
}
|
}
|
||||||
|
@ -1089,8 +1079,7 @@ printent(struct entry *ent, int sel)
|
||||||
static char *
|
static char *
|
||||||
coolsize(off_t size)
|
coolsize(off_t size)
|
||||||
{
|
{
|
||||||
static const char * const U[]
|
static const char * const U = "BKMGTPEZY";
|
||||||
= {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
|
|
||||||
static char size_buf[12]; /* Buffer to hold human readable size */
|
static char size_buf[12]; /* Buffer to hold human readable size */
|
||||||
static int i;
|
static int i;
|
||||||
static off_t tmp;
|
static off_t tmp;
|
||||||
|
@ -1106,7 +1095,7 @@ coolsize(off_t size)
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(size_buf, 12, "%.*Lf%s", i, size + rem * div_2_pow_10, U[i]);
|
snprintf(size_buf, 12, "%.*Lf%c", i, size + rem * div_2_pow_10, U[i]);
|
||||||
return size_buf;
|
return size_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1440,7 +1429,7 @@ show_stats(char *fpath, char *fname, struct stat *sb)
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
exitcurses();
|
endwin();
|
||||||
get_output(NULL, 0, "cat", tmp, NULL, 1);
|
get_output(NULL, 0, "cat", tmp, NULL, 1);
|
||||||
unlink(tmp);
|
unlink(tmp);
|
||||||
initcurses();
|
initcurses();
|
||||||
|
@ -1453,7 +1442,7 @@ show_mediainfo(char *fpath, char *arg)
|
||||||
if (!get_output(g_buf, MAX_CMD_LEN, "which", metaviewer, NULL, 0))
|
if (!get_output(g_buf, MAX_CMD_LEN, "which", metaviewer, NULL, 0))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
exitcurses();
|
endwin();
|
||||||
get_output(NULL, 0, metaviewer, fpath, arg, 1);
|
get_output(NULL, 0, metaviewer, fpath, arg, 1);
|
||||||
initcurses();
|
initcurses();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1533,7 +1522,7 @@ show_help(void)
|
||||||
dprintf(fd, "\n");
|
dprintf(fd, "\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
exitcurses();
|
endwin();
|
||||||
get_output(NULL, 0, "cat", tmp, NULL, 1);
|
get_output(NULL, 0, "cat", tmp, NULL, 1);
|
||||||
unlink(tmp);
|
unlink(tmp);
|
||||||
initcurses();
|
initcurses();
|
||||||
|
@ -1626,7 +1615,7 @@ dentfill(char *path, struct entry **dents,
|
||||||
|
|
||||||
if (nftw(g_buf, sum_bsizes, open_max,
|
if (nftw(g_buf, sum_bsizes, open_max,
|
||||||
FTW_MOUNT | FTW_PHYS) == -1) {
|
FTW_MOUNT | FTW_PHYS) == -1) {
|
||||||
printmsg("nftw(3) failed");
|
printmsg(STR_NFTWFAIL);
|
||||||
dir_blocks += sb.st_blocks;
|
dir_blocks += sb.st_blocks;
|
||||||
} else
|
} else
|
||||||
dir_blocks += ent_blocks;
|
dir_blocks += ent_blocks;
|
||||||
|
@ -1666,7 +1655,7 @@ dentfill(char *path, struct entry **dents,
|
||||||
|
|
||||||
if (nftw(g_buf, sum_bsizes, open_max,
|
if (nftw(g_buf, sum_bsizes, open_max,
|
||||||
FTW_MOUNT | FTW_PHYS) == -1) {
|
FTW_MOUNT | FTW_PHYS) == -1) {
|
||||||
printmsg("nftw(3) failed");
|
printmsg(STR_NFTWFAIL);
|
||||||
(*dents)[n].blocks = sb.st_blocks;
|
(*dents)[n].blocks = sb.st_blocks;
|
||||||
} else
|
} else
|
||||||
(*dents)[n].blocks = ent_blocks;
|
(*dents)[n].blocks = ent_blocks;
|
||||||
|
@ -1922,7 +1911,7 @@ nochange:
|
||||||
case SEL_BACK:
|
case SEL_BACK:
|
||||||
/* There is no going back */
|
/* There is no going back */
|
||||||
if (path[0] == '/' && path[1] == '\0') {
|
if (path[0] == '/' && path[1] == '\0') {
|
||||||
printmsg("You are at /");
|
printmsg(STR_ATROOT);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2083,7 +2072,7 @@ nochange:
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
|
|
||||||
exitcurses();
|
endwin();
|
||||||
tmp = readline("chdir: ");
|
tmp = readline("chdir: ");
|
||||||
initcurses();
|
initcurses();
|
||||||
|
|
||||||
|
@ -2115,7 +2104,7 @@ nochange:
|
||||||
home, tmp + 1);
|
home, tmp + 1);
|
||||||
else {
|
else {
|
||||||
free(input);
|
free(input);
|
||||||
printmsg("HOME not set");
|
printmsg(STR_NOHOME);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
} else if (tmp[0] == '-' && tmp[1] == '\0') {
|
} else if (tmp[0] == '-' && tmp[1] == '\0') {
|
||||||
|
@ -2136,7 +2125,7 @@ nochange:
|
||||||
|
|
||||||
/* Show a message if already at / */
|
/* Show a message if already at / */
|
||||||
if (path[0] == '/' && path[1] == '\0') {
|
if (path[0] == '/' && path[1] == '\0') {
|
||||||
printmsg("You are at /");
|
printmsg(STR_ATROOT);
|
||||||
free(input);
|
free(input);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
|
@ -2282,7 +2271,7 @@ nochange:
|
||||||
bookmark[r].loc
|
bookmark[r].loc
|
||||||
+ 1);
|
+ 1);
|
||||||
else {
|
else {
|
||||||
printmsg("HOME not set");
|
printmsg(STR_NOHOME);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
|
@ -2584,7 +2573,7 @@ main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
initcurses();
|
initcurses();
|
||||||
browse(ipath, ifilter);
|
browse(ipath, ifilter);
|
||||||
exitcurses();
|
endwin();
|
||||||
#ifdef DEBUGMODE
|
#ifdef DEBUGMODE
|
||||||
disabledbg();
|
disabledbg();
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue