Take care of line wrapping and odd number of lines

This commit is contained in:
lostd 2014-10-07 14:00:25 +00:00 committed by lostd
parent fdf42ec502
commit 908e43cc67

59
noice.c
View file

@ -24,6 +24,7 @@
#define LEN(x) (sizeof(x) / sizeof(*(x))) #define LEN(x) (sizeof(x) / sizeof(*(x)))
#define MIN(x, y) ((x) < (y) ? (x) : (y)) #define MIN(x, y) ((x) < (y) ? (x) : (y))
#define ISODD(x) ((x) & 1)
/* /*
* Layout: * Layout:
@ -41,6 +42,10 @@
int die = 0; int die = 0;
struct entry {
char name[MAXNAMLEN + 1];
};
struct assoc { struct assoc {
char *ext; /* Extension */ char *ext; /* Extension */
char *bin; /* Program */ char *bin; /* Program */
@ -205,6 +210,7 @@ browse(const char *ipath)
int i, n, cur; int i, n, cur;
int r, ret; int r, ret;
char *path = strdup(ipath); char *path = strdup(ipath);
char *cwd;
begin: begin:
/* Path should be a malloc(3)-ed string at all times */ /* Path should be a malloc(3)-ed string at all times */
@ -234,6 +240,8 @@ begin:
for (;;) { for (;;) {
int nlines; int nlines;
struct entry *tmpents;
int odd;
redraw: redraw:
nlines = MIN(LINES - 4, n); nlines = MIN(LINES - 4, n);
@ -251,29 +259,50 @@ redraw:
DPRINTF_D(cur); DPRINTF_D(cur);
DPRINTF_S(path); DPRINTF_S(path);
#define CWD "cwd: "
#define CURSR " > "
#define EMPTY " "
/* No text wrapping in cwd line */
cwd = malloc(COLS * sizeof(char));
strncpy(cwd, path, COLS);
cwd[COLS - strlen(CWD) - 1] = '\0';
/* No text wrapping in entries */
tmpents = malloc(n * sizeof(*tmpents));
for (i = 0; i < n; i++) {
strncpy(tmpents[i].name, dents[i]->d_name,
sizeof(tmpents[i].name));
tmpents[i].name[COLS - strlen(CURSR) - 1] = '\0';
}
/* Print cwd */ /* Print cwd */
printw("cwd: %s%s\n\n", printw(CWD "%s%s\n\n",
strncmp(path, "", 1) == 0 ? "/" : "", strncmp(cwd, "", 1) == 0 ? "/" : "",
path); cwd);
/* Print listing */ /* Print listing */
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", printw("%s%s\n",
i == cur ? ">" : " ", i == cur ? CURSR : EMPTY,
dents[i]->d_name); 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", printw("%s%s\n",
i == cur ? ">" : " ", i == cur ? CURSR : EMPTY,
dents[i]->d_name); tmpents[i].name);
} else { } else {
for (i = cur - nlines / 2; i <= cur + nlines / 2; i++) for (i = cur - nlines / 2;
printw(" %s %s\n", i < cur + nlines / 2 + odd; i++)
i == cur ? ">" : " ", printw("%s%s\n",
dents[i]->d_name); i == cur ? CURSR : EMPTY,
tmpents[i].name);
} }
free(tmpents);
nochange: nochange:
ret = nextsel(&cur, n); ret = nextsel(&cur, n);
if (ret == 1) { if (ret == 1) {
@ -350,10 +379,6 @@ nochange:
free(file); free(file);
/* Screen may be messed up */
clear();
/* Some programs reset this */
keypad(stdscr, TRUE);
goto redraw; goto redraw;
default: default:
DPRINTF_D(dents[cur]->d_type); DPRINTF_D(dents[cur]->d_type);