Show content size of cur dir in du mode

This commit is contained in:
Arun Prakash Jana 2017-06-21 23:51:53 +05:30
parent f59cd3e90f
commit 90680cb370
No known key found for this signature in database
GPG Key ID: A75979F35C080412
1 changed files with 50 additions and 24 deletions

74
nnn.c
View File

@ -115,6 +115,7 @@ static void disabledbg()
#define SP_NORMAL 0x80 /* spawn child process in non-curses regular mode */ #define SP_NORMAL 0x80 /* spawn child process in non-curses regular mode */
typedef unsigned long ulong; typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned char uchar; typedef unsigned char uchar;
/* Directory entry */ /* Directory entry */
@ -167,8 +168,9 @@ static char *copier;
static char *editor; static char *editor;
static char *desktop_manager; static char *desktop_manager;
static off_t blk_size; static off_t blk_size;
static off_t dir_size;
static size_t fs_free; static size_t fs_free;
static unsigned int open_max; static uint open_max;
static bm bookmark[MAX_BM]; static bm bookmark[MAX_BM];
static const double div_2_pow_10 = 1.0 / 1024.0; static const double div_2_pow_10 = 1.0 / 1024.0;
@ -524,8 +526,8 @@ static char *
getmime(char *file) getmime(char *file)
{ {
regex_t regex; regex_t regex;
unsigned int i; uint i;
static unsigned int len = LEN(assocs); static uint len = LEN(assocs);
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
if (regcomp(&regex, assocs[i].regex, if (regcomp(&regex, assocs[i].regex,
@ -647,9 +649,11 @@ printprompt(char *str)
static int static int
nextsel(char **run, char **env, int *presel) nextsel(char **run, char **env, int *presel)
{ {
int c = *presel; static int c;
unsigned int i; static uchar i;
static unsigned int len = LEN(bindings); static uint len = LEN(bindings);
c = *presel;
if (c == 0) if (c == 0)
c = getch(); c = getch();
@ -1475,6 +1479,9 @@ dentfill(char *path, struct entry **dents,
n = 0; n = 0;
if (cfg.bsizeorder)
dir_size = 0;
dirp = opendir(path); dirp = opendir(path);
if (dirp == NULL) if (dirp == NULL)
return 0; return 0;
@ -1485,8 +1492,31 @@ dentfill(char *path, struct entry **dents,
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))) (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))))
continue; continue;
if (filter(re, dp->d_name) == 0) mkpath(path, dp->d_name, newpath, PATH_MAX);
if (lstat(newpath, &sb) == -1) {
continue; continue;
/* if (*dents)
free(*dents);
printerr(1, "lstat"); */
}
if (filter(re, dp->d_name) == 0) {
if (!cfg.bsizeorder)
continue;
if (S_ISDIR(sb.st_mode)) {
blk_size = 0;
if (nftw(newpath, sum_bsizes, open_max,
FTW_MOUNT | FTW_PHYS) == -1) {
printmsg("nftw(3) failed");
dir_size += sb.st_blocks;
} else
dir_size += blk_size;
} else
dir_size += sb.st_blocks;
continue;
}
if (n == total_dents) { if (n == total_dents) {
total_dents += 64; total_dents += 64;
@ -1496,13 +1526,7 @@ dentfill(char *path, struct entry **dents,
} }
xstrlcpy((*dents)[n].name, dp->d_name, NAME_MAX); xstrlcpy((*dents)[n].name, dp->d_name, NAME_MAX);
/* Get mode flags */
mkpath(path, dp->d_name, newpath, PATH_MAX);
if (lstat(newpath, &sb) == -1) {
if (*dents)
free(*dents);
printerr(1, "lstat");
}
(*dents)[n].mode = sb.st_mode; (*dents)[n].mode = sb.st_mode;
(*dents)[n].t = sb.st_mtime; (*dents)[n].t = sb.st_mtime;
(*dents)[n].size = sb.st_size; (*dents)[n].size = sb.st_size;
@ -1518,6 +1542,8 @@ dentfill(char *path, struct entry **dents,
(*dents)[n].bsize = blk_size; (*dents)[n].bsize = blk_size;
} else } else
(*dents)[n].bsize = sb.st_blocks; (*dents)[n].bsize = sb.st_blocks;
dir_size += (*dents)[n].bsize;
} }
++n; ++n;
@ -1593,7 +1619,6 @@ populate(char *path, char *oldpath, char *fltr)
static void static void
redraw(char *path) redraw(char *path)
{ {
static char cwd[PATH_MAX];
static int nlines, i; static int nlines, i;
static size_t ncols; static size_t ncols;
@ -1613,7 +1638,7 @@ redraw(char *path)
DPRINTF_S(path); DPRINTF_S(path);
/* No text wrapping in cwd line */ /* No text wrapping in cwd line */
if (!realpath(path, cwd)) { if (!realpath(path, g_buf)) {
printmsg("Cannot resolve path"); printmsg("Cannot resolve path");
return; return;
} }
@ -1621,8 +1646,8 @@ redraw(char *path)
ncols = COLS; ncols = COLS;
if (ncols > PATH_MAX) if (ncols > PATH_MAX)
ncols = PATH_MAX; ncols = PATH_MAX;
cwd[ncols - strlen(CWD) - 1] = '\0'; g_buf[ncols - strlen(CWD) - 1] = '\0';
printw(CWD "%s\n\n", cwd); printw(CWD "%s\n\n", g_buf);
/* Print listing */ /* Print listing */
if (cur < (nlines >> 1)) { if (cur < (nlines >> 1)) {
@ -1666,15 +1691,16 @@ redraw(char *path)
ind[0] = '\0'; ind[0] = '\0';
if (!cfg.bsizeorder) if (!cfg.bsizeorder)
sprintf(cwd, "total %d %s[%s%s]", ndents, sort, sprintf(g_buf, "total %d %s[%s%s]", ndents, sort,
replace_escape(dents[cur].name), ind); replace_escape(dents[cur].name), ind);
else else {
sprintf(cwd, i = sprintf(g_buf, "du: %s in dir, ",
"total %d by disk usage, %s free [%s%s]", coolsize(dir_size << 9));
ndents, coolsize(fs_free), sprintf(g_buf + i, "%s free [%s%s]", coolsize(fs_free),
replace_escape(dents[cur].name), ind); replace_escape(dents[cur].name), ind);
}
printmsg(cwd); printmsg(g_buf);
} else } else
printmsg("0 items"); printmsg("0 items");
} }