Use existing buffer to (un)select file names

This commit is contained in:
Arun Prakash Jana 2021-05-11 03:07:55 +05:30
parent 9b6e7e86bc
commit e4c3f7d739
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 63 additions and 40 deletions

View file

@ -11,5 +11,5 @@ CheckOptions:
- key: fuchsia-restrict-system-includes.Includes - key: fuchsia-restrict-system-includes.Includes
value: '*,-stdint.h,-stdbool.h' value: '*,-stdint.h,-stdbool.h'
- key: readability-function-size.StatementThreshold - key: readability-function-size.StatementThreshold
value: '925' value: '900'
... ...

101
src/nnn.c
View file

@ -1458,15 +1458,68 @@ static void startselection(void)
} }
} }
static void updateselbuf(const char *path, char *newpath) static size_t appendslash(char *path)
{ {
size_t r;
for (int i = 0; i < ndents; ++i) size_t len = 1;
if (pdents[i].flags & FILE_SELECTED) {
r = mkpath(path, pdents[i].name, newpath); if (path[1] != '\0') {
appendfpath(newpath, r); len = xstrlen(path);
path[len] = '/';
++len;
} }
return len;
}
static void invertselbuf(char *path, bool toggle)
{
selbufpos = lastappendpos;
if (!nselected) {
writesel(NULL, 0);
return;
}
size_t len = appendslash(path);
for (int i = 0; i < ndents; ++i) {
if (toggle) { /* Toggle selection status */
pdents[i].flags ^= FILE_SELECTED;
pdents[i].flags & FILE_SELECTED ? ++nselected : --nselected;
}
if (pdents[i].flags & FILE_SELECTED)
appendfpath(path, len + xstrsncpy(path + len, pdents[i].name, PATH_MAX - len));
}
if (len > 1)
--len;
path[len] = '\0';
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
}
static void addtoselbuf(char *path, int startid, int endid)
{
size_t len = appendslash(path);
/* Remember current selection buffer position */
for (int i = startid; i <= endid; ++i) {
if (!(pdents[i].flags & FILE_SELECTED)) {
/* Write the path to selection file to avoid flush */
appendfpath(path, len + xstrsncpy(path + len, pdents[i].name, PATH_MAX - len));
pdents[i].flags |= FILE_SELECTED;
++nselected;
}
}
if (len > 1)
--len;
path[len] = '\0';
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
} }
/* Finish selection procedure before an operation */ /* Finish selection procedure before an operation */
@ -6617,12 +6670,8 @@ nochange:
appendfpath(newpath, mkpath(path, pdents[cur].name, newpath)); appendfpath(newpath, mkpath(path, pdents[cur].name, newpath));
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */ writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
} else { } else {
selbufpos = lastappendpos; --nselected;
if (--nselected) { invertselbuf(path, FALSE);
updateselbuf(path, newpath);
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
} else
writesel(NULL, 0);
} }
#ifndef NOX11 #ifndef NOX11
@ -6693,33 +6742,7 @@ nochange:
selendid = ndents - 1; selendid = ndents - 1;
} }
if (sel == SEL_SELINV) { (sel == SEL_SELINV) ? invertselbuf(path, TRUE) : addtoselbuf(path, selstartid, selendid);
/* Toggle selection status */
for (r = selstartid; r <= selendid; ++r) {
pdents[r].flags ^= FILE_SELECTED;
pdents[r].flags & FILE_SELECTED ? ++nselected : --nselected;
}
selbufpos = lastappendpos;
if (nselected) {
updateselbuf(path, newpath);
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
} else
writesel(NULL, 0);
} else {
/* Remember current selection buffer position */
for (r = selstartid; r <= selendid; ++r) {
if (!(pdents[r].flags & FILE_SELECTED)) {
/* Write the path to selection file to avoid flush */
appendfpath(newpath, mkpath(path, pdents[r].name, newpath));
pdents[r].flags |= FILE_SELECTED;
++nselected;
}
}
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */
}
#ifndef NOX11 #ifndef NOX11
if (cfg.x11) if (cfg.x11)