mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 20:01:27 +00:00
Refactor parts of browse() into populate() and redraw()
This commit is contained in:
parent
d9b231742a
commit
ac36d0b287
123
noice.c
123
noice.c
|
@ -75,6 +75,12 @@ struct entry {
|
||||||
time_t t;
|
time_t t;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Global context */
|
||||||
|
struct entry *dents;
|
||||||
|
int n, cur;
|
||||||
|
char *path, *oldpath;
|
||||||
|
char *fltr;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Layout:
|
* Layout:
|
||||||
* .---------
|
* .---------
|
||||||
|
@ -565,36 +571,29 @@ dentfind(struct entry *dents, int n, char *cwd, char *path)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
browse(const char *ipath, const char *ifilter)
|
populate(void)
|
||||||
{
|
{
|
||||||
struct entry *dents;
|
regex_t re;
|
||||||
int i, n, cur, r, fd;
|
int r;
|
||||||
int nlines, odd;
|
|
||||||
char *path = xstrdup(ipath);
|
|
||||||
char *filter = xstrdup(ifilter);
|
|
||||||
regex_t filter_re, re;
|
|
||||||
char *cwd, *newpath, *oldpath = NULL;
|
|
||||||
struct stat sb;
|
|
||||||
char *name, *bin, *dir, *tmp, *run;
|
|
||||||
int nowtyping = 0;
|
|
||||||
|
|
||||||
begin:
|
|
||||||
/* Path and filter should be malloc(3)-ed strings at all times */
|
|
||||||
n = 0;
|
|
||||||
dents = NULL;
|
|
||||||
|
|
||||||
|
/* Can fail when permissions change while browsing */
|
||||||
if (canopendir(path) == 0) {
|
if (canopendir(path) == 0) {
|
||||||
printwarn();
|
printwarn();
|
||||||
goto nochange;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Search filter */
|
/* Search filter */
|
||||||
r = setfilter(&filter_re, filter);
|
r = setfilter(&re, fltr);
|
||||||
if (r != 0)
|
if (r != 0)
|
||||||
goto nochange;
|
return -1;
|
||||||
|
|
||||||
n = dentfill(path, &dents, visible, &filter_re);
|
dentfree(dents, n);
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
dents = NULL;
|
||||||
|
|
||||||
|
n = dentfill(path, &dents, visible, &re);
|
||||||
|
|
||||||
qsort(dents, n, sizeof(*dents), entrycmp);
|
qsort(dents, n, sizeof(*dents), entrycmp);
|
||||||
|
|
||||||
|
@ -603,7 +602,16 @@ begin:
|
||||||
free(oldpath);
|
free(oldpath);
|
||||||
oldpath = NULL;
|
oldpath = NULL;
|
||||||
|
|
||||||
for (;;) {
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
redraw(void)
|
||||||
|
{
|
||||||
|
int nlines, odd;
|
||||||
|
char *cwd;
|
||||||
|
int i;
|
||||||
|
|
||||||
nlines = MIN(LINES - 4, n);
|
nlines = MIN(LINES - 4, n);
|
||||||
|
|
||||||
/* Clean screen */
|
/* Clean screen */
|
||||||
|
@ -639,16 +647,40 @@ begin:
|
||||||
i < cur + nlines / 2 + odd; i++)
|
i < cur + nlines / 2 + odd; i++)
|
||||||
printent(&dents[i], i == cur);
|
printent(&dents[i], i == cur);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
browse(const char *ipath, const char *ifilter)
|
||||||
|
{
|
||||||
|
int r, fd;
|
||||||
|
regex_t re;
|
||||||
|
char *newpath;
|
||||||
|
struct stat sb;
|
||||||
|
char *name, *bin, *dir, *tmp, *run;
|
||||||
|
int nowtyping = 0;
|
||||||
|
|
||||||
|
oldpath = NULL;
|
||||||
|
path = xstrdup(ipath);
|
||||||
|
fltr = xstrdup(ifilter);
|
||||||
|
begin:
|
||||||
|
/* Path and filter should be malloc(3)-ed strings at all times */
|
||||||
|
r = populate();
|
||||||
|
if (r == -1) {
|
||||||
|
nowtyping = 0;
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
redraw();
|
||||||
|
|
||||||
/* Handle filter-as-you-type mode */
|
/* Handle filter-as-you-type mode */
|
||||||
if (nowtyping)
|
if (nowtyping)
|
||||||
goto moretyping;
|
goto moretyping;
|
||||||
|
|
||||||
nochange:
|
nochange:
|
||||||
switch (nextsel(&run)) {
|
switch (nextsel(&run)) {
|
||||||
case SEL_QUIT:
|
case SEL_QUIT:
|
||||||
free(path);
|
free(path);
|
||||||
free(filter);
|
free(fltr);
|
||||||
dentfree(dents, n);
|
dentfree(dents, n);
|
||||||
return;
|
return;
|
||||||
case SEL_BACK:
|
case SEL_BACK:
|
||||||
|
@ -666,9 +698,9 @@ nochange:
|
||||||
oldpath = path;
|
oldpath = path;
|
||||||
path = dir;
|
path = dir;
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
free(filter);
|
free(fltr);
|
||||||
filter = xstrdup(ifilter);
|
fltr = xstrdup(ifilter);
|
||||||
goto out;
|
goto begin;
|
||||||
case SEL_GOIN:
|
case SEL_GOIN:
|
||||||
/* Cannot descend in empty directories */
|
/* Cannot descend in empty directories */
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
|
@ -705,9 +737,9 @@ nochange:
|
||||||
free(path);
|
free(path);
|
||||||
path = newpath;
|
path = newpath;
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
free(filter);
|
free(fltr);
|
||||||
filter = xstrdup(ifilter);
|
fltr = xstrdup(ifilter);
|
||||||
goto out;
|
goto begin;
|
||||||
case S_IFREG:
|
case S_IFREG:
|
||||||
bin = openwith(newpath);
|
bin = openwith(newpath);
|
||||||
if (bin == NULL) {
|
if (bin == NULL) {
|
||||||
|
@ -736,13 +768,13 @@ nochange:
|
||||||
free(tmp);
|
free(tmp);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
free(filter);
|
free(fltr);
|
||||||
filter = tmp;
|
fltr = tmp;
|
||||||
DPRINTF_S(filter);
|
DPRINTF_S(fltr);
|
||||||
/* Save current */
|
/* Save current */
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
oldpath = makepath(path, dents[cur].name);
|
oldpath = makepath(path, dents[cur].name);
|
||||||
goto out;
|
goto begin;
|
||||||
case SEL_TYPE:
|
case SEL_TYPE:
|
||||||
nowtyping = 1;
|
nowtyping = 1;
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
|
@ -767,17 +799,17 @@ moretyping:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Copy or reset filter */
|
/* Copy or reset filter */
|
||||||
free(filter);
|
free(fltr);
|
||||||
if (tmp != NULL)
|
if (tmp != NULL)
|
||||||
filter = xstrdup(tmp);
|
fltr = xstrdup(tmp);
|
||||||
else
|
else
|
||||||
filter = xstrdup(ifilter);
|
fltr = xstrdup(ifilter);
|
||||||
/* Save current */
|
/* Save current */
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
oldpath = makepath(path, dents[cur].name);
|
oldpath = makepath(path, dents[cur].name);
|
||||||
if (!nowtyping)
|
if (!nowtyping)
|
||||||
free(tmp);
|
free(tmp);
|
||||||
goto out;
|
goto begin;
|
||||||
case SEL_NEXT:
|
case SEL_NEXT:
|
||||||
if (cur < n - 1)
|
if (cur < n - 1)
|
||||||
cur++;
|
cur++;
|
||||||
|
@ -811,15 +843,15 @@ moretyping:
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
path = newpath;
|
path = newpath;
|
||||||
free(filter);
|
free(fltr);
|
||||||
filter = xstrdup(ifilter); /* Reset filter */
|
fltr = xstrdup(ifilter); /* Reset filter */
|
||||||
DPRINTF_S(path);
|
DPRINTF_S(path);
|
||||||
goto out;
|
goto begin;
|
||||||
case SEL_MTIME:
|
case SEL_MTIME:
|
||||||
mtimeorder = !mtimeorder;
|
mtimeorder = !mtimeorder;
|
||||||
goto out;
|
goto begin;
|
||||||
case SEL_REDRAW:
|
case SEL_REDRAW:
|
||||||
goto out;
|
goto begin;
|
||||||
case SEL_RUN:
|
case SEL_RUN:
|
||||||
exitcurses();
|
exitcurses();
|
||||||
spawn(run, NULL, path);
|
spawn(run, NULL, path);
|
||||||
|
@ -833,11 +865,6 @@ moretyping:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
|
||||||
dentfree(dents, n);
|
|
||||||
|
|
||||||
goto begin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Reference in a new issue