Printing and line wrapping in a function, show dirs and links

This commit is contained in:
lostd 2014-10-09 17:07:21 +03:00
parent ee8898547c
commit 0e2ea28a30

70
noice.c
View file

@ -209,6 +209,38 @@ testopendir(char *path)
} }
} }
void
printent(struct entry *ent, int active)
{
char *name;
unsigned int maxlen = COLS - strlen(CURSR) - 1;
char cm = 0;
/* Copy name locally */
name = strdup(ent->name);
if (name == NULL)
printerr(1, "strdup name");
if (S_ISDIR(ent->mode)) {
cm = '/';
maxlen--;
} else if (S_ISLNK(ent->mode)) {
cm = '@';
maxlen--;
}
/* No text wrapping in entries */
if (strlen(name) > maxlen)
name[maxlen] = '\0';
if (cm == 0)
printw("%s%s\n", active ? CURSR : EMPTY, name);
else
printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
free(name);
}
void void
browse(const char *ipath) browse(const char *ipath)
{ {
@ -233,9 +265,6 @@ begin:
printwarn(); printwarn();
goto nochange; goto nochange;
} }
dfd = dirfd(dirp);
if (dfd == -1)
printerr(1, "dirfd");
while ((dp = readdir(dirp)) != NULL) { while ((dp = readdir(dirp)) != NULL) {
char *name; char *name;
@ -252,7 +281,9 @@ begin:
if (dents[n].name == NULL) if (dents[n].name == NULL)
printerr(1, "strdup"); printerr(1, "strdup");
/* Get mode flags */ /* Get mode flags */
r = fstatat(dfd, dents[n].name, &sb, 0); asprintf(&name, "%s/%s", path, dents[n].name);
r = lstat(name, &sb);
free(name);
if (r == -1) if (r == -1)
printerr(1, "stat"); printerr(1, "stat");
dents[n].mode = sb.st_mode; dents[n].mode = sb.st_mode;
@ -263,7 +294,6 @@ begin:
for (;;) { for (;;) {
int nlines; int nlines;
struct entry *tmpents;
int maxlen; int maxlen;
int odd; int odd;
@ -288,20 +318,6 @@ redraw:
strlcpy(cwd, path, COLS * sizeof(char)); strlcpy(cwd, path, COLS * sizeof(char));
cwd[COLS - strlen(CWD) - 1] = '\0'; cwd[COLS - strlen(CWD) - 1] = '\0';
/* No text wrapping in entries */
tmpents = malloc(n * sizeof(*tmpents));
maxlen = COLS - strlen(CURSR) - 1;
for (i = 0; i < n; i++) {
struct entry *tmpent = &tmpents[i];
tmpent->name = strdup(dents[i].name);
if (tmpent->name == NULL)
printerr(1, "strdup tmp");
tmpent->mode = dents[i].mode;
if (strlen(tmpent->name) > maxlen)
tmpent->name[maxlen] = '\0';
}
/* Print cwd. If empty we are on the root. We store it /* Print cwd. If empty we are on the root. We store it
* as an empty string so that when we navigate in /mnt * as an empty string so that when we navigate in /mnt
* is doesn't come up as //mnt. */ * is doesn't come up as //mnt. */
@ -313,26 +329,16 @@ redraw:
odd = ISODD(nlines); odd = ISODD(nlines);
if (cur < nlines / 2) { if (cur < nlines / 2) {
for (i = 0; i < nlines; i++) for (i = 0; i < nlines; i++)
printw("%s%s\n", printent(&dents[i], i == cur);
i == cur ? CURSR : EMPTY,
tmpents[i].name);
} else if (cur >= n - nlines / 2) { } else if (cur >= n - nlines / 2) {
for (i = n - nlines; i < n; i++) for (i = n - nlines; i < n; i++)
printw("%s%s\n", printent(&dents[i], i == cur);
i == cur ? CURSR : EMPTY,
tmpents[i].name);
} else { } else {
for (i = cur - nlines / 2; for (i = cur - nlines / 2;
i < cur + nlines / 2 + odd; i++) i < cur + nlines / 2 + odd; i++)
printw("%s%s\n", printent(&dents[i], i == cur);
i == cur ? CURSR : EMPTY,
tmpents[i].name);
} }
for (i = 0; i < n; i++)
free(tmpents[i].name);
free(tmpents);
nochange: nochange:
ret = nextsel(&cur, n); ret = nextsel(&cur, n);
if (ret == 1) { if (ret == 1) {