Update add to and invert selection logic

This commit is contained in:
Arun Prakash Jana 2021-07-13 21:46:18 +05:30
parent 96f2dfa8a5
commit b3ef30a8ba
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -1515,20 +1515,6 @@ static void clearselection(void)
writesel(NULL, 0); writesel(NULL, 0);
} }
static size_t appendslash(char *path)
{
size_t len = 1;
if (path[1] != '\0') {
len = xstrlen(path);
path[len] = '/';
++len;
}
return len;
}
static char *findinsel(char *startpos, int len) static char *findinsel(char *startpos, int len)
{ {
if (!selbufpos) if (!selbufpos)
@ -1565,7 +1551,7 @@ static int markcmp(const void *va, const void *vb)
return ma->startpos - mb->startpos; return ma->startpos - mb->startpos;
} }
static void findmarkentry(char *path, struct entry *dentp) static inline void findmarkentry(char *path, struct entry *dentp)
{ {
if (!(dentp->flags & FILE_SCANNED)) { if (!(dentp->flags & FILE_SCANNED)) {
if (findinsel(findselpos, mkpath(path, dentp->name, g_buf))) if (findinsel(findselpos, mkpath(path, dentp->name, g_buf)))
@ -1581,6 +1567,7 @@ static void invertselbuf(char *path)
int nmarked = 0, prev = 0; int nmarked = 0, prev = 0;
struct entry *dentp; struct entry *dentp;
selmark *marked = malloc(nselected * sizeof(selmark)); selmark *marked = malloc(nselected * sizeof(selmark));
bool scan = FALSE;
if (nselected > LARGESEL) { if (nselected > LARGESEL) {
printmsg("processing..."); printmsg("processing...");
@ -1590,24 +1577,36 @@ static void invertselbuf(char *path)
/* First pass: inversion */ /* First pass: inversion */
for (int i = 0; i < ndents; ++i) { for (int i = 0; i < ndents; ++i) {
dentp = &pdents[i]; dentp = &pdents[i];
findmarkentry(path, dentp);
/* Toggle selection status */ if (dentp->flags & FILE_SCANNED) {
dentp->flags ^= FILE_SELECTED; if (dentp->flags & FILE_SELECTED) {
dentp->flags ^= FILE_SELECTED; /* Clear selection status */
scan = TRUE;
} else {
++nselected;
dentp->flags |= FILE_SELECTED;
}
} else {
dentp->flags |= FILE_SCANNED;
scan = TRUE;
}
/* Find where the files marked for deselection are in selection buffer */ if (scan) {
if (!(dentp->flags & FILE_SELECTED)) {
len = mkpath(path, dentp->name, g_buf); len = mkpath(path, dentp->name, g_buf);
found = findinsel(findselpos, len); found = findinsel(findselpos, len);
if (found) {
marked[nmarked].startpos = found;
marked[nmarked].len = len;
++nmarked;
marked[nmarked].startpos = found; --nselected;
marked[nmarked].len = len; offset += len; /* buffer size adjustment */
++nmarked; } else {
++nselected;
--nselected; dentp->flags |= FILE_SELECTED;
offset += len; /* buffer size adjustment */ }
} else scan = FALSE;
++nselected; }
} }
/* /*
@ -1706,30 +1705,34 @@ static bool scanselforpath(const char *path)
static void addtoselbuf(char *path, int startid, int endid) static void addtoselbuf(char *path, int startid, int endid)
{ {
size_t len = appendslash(path); size_t len;
struct entry *dentp; struct entry *dentp;
bool add = FALSE;
/* Remember current selection buffer position */ /* Remember current selection buffer position */
for (int i = startid; i <= endid; ++i) { for (int i = startid; i <= endid; ++i) {
dentp = &pdents[i]; dentp = &pdents[i];
if (findselpos) len = mkpath(path, dentp->name, g_buf);
findmarkentry(path, dentp);
else
dentp->flags |= FILE_SCANNED;
if (!(dentp->flags & FILE_SELECTED)) { if (findselpos) {
if (dentp->flags & FILE_SCANNED) {
if (!(dentp->flags & FILE_SELECTED))
add = TRUE;
} else
add = !findinsel(findselpos, len);
} else
add = TRUE;
if (add) {
/* Write the path to selection file to avoid flush */ /* Write the path to selection file to avoid flush */
if (appendfpath(path, len + xstrsncpy(path + len, dentp->name, PATH_MAX - len))) if (appendfpath(g_buf, len) && findselpos)
scanselforpath(path); scanselforpath(path);
dentp->flags |= FILE_SELECTED;
++nselected; ++nselected;
add = FALSE;
} }
dentp->flags |= (FILE_SCANNED | FILE_SELECTED);
} }
if (len > 1)
--len;
path[len] = '\0';
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */ writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
} }