make mark all work correctly as expected

This commit is contained in:
Night Kaly 2024-03-17 23:21:58 +00:00
parent 0f52bd265f
commit 255e09c18d
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
2 changed files with 28 additions and 16 deletions

19
ccc.c
View file

@ -236,16 +236,15 @@ int main(int argc, char** argv)
break; break;
/* mark one file */ /* mark one file */
case SPACE:; case SPACE:
add_file_stat(get_filepath(current_selection), 1); add_file_stat(get_filepath(current_selection), 1);
highlight_current_line(); highlight_current_line();
break; break;
/* mark all files in directory */ /* mark all files in directory */
case 'a':; case 'a':
int save_current_sel = current_selection; populate_files(cwd, 2);
populate_files(cwd, 1); change_dir(cwd, current_selection); /* reload current dir */
change_dir(cwd, save_current_sel); /* reload current dir */
break; break;
/* mark actions: */ /* mark actions: */
@ -368,7 +367,7 @@ int mkdir_p(const char *destdir)
/* /*
* Read the provided directory and add all files in directory to linked list * Read the provided directory and add all files in directory to linked list
* ftype: normal files = 0, marked = 1 * ftype: normal files = 0, marked = 1, marking ALL = 2
* ep->d_name -> filename * ep->d_name -> filename
*/ */
void populate_files(const char *path, int ftype) void populate_files(const char *path, int ftype)
@ -417,7 +416,7 @@ int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, s
/* /*
* Get file's last modified time, size, type * Get file's last modified time, size, type
* Add that file into list * Add that file into list
* ftype: normal file = 0, marked = 1 * ftype: normal file = 0, normal marked = 1, marking ALL = 2
*/ */
long add_file_stat(char *filepath, int ftype) long add_file_stat(char *filepath, int ftype)
{ {
@ -457,8 +456,10 @@ long add_file_stat(char *filepath, int ftype)
} }
/* if file is to be marked */ /* if file is to be marked */
if (ftype == 1) { if (ftype == 1 || ftype == 2) {
long index = add_marked(filepath, type); /* force if user is marking all files */
bool force = ftype == 2 ? true : false;
long index = add_marked(filepath, type, force);
/* free type and return without allocating more stuff */ /* free type and return without allocating more stuff */
free(type); free(type);
if (index != -1) { if (index != -1) {

13
file.c
View file

@ -131,7 +131,10 @@ void remove_marked(file *marked_file)
} }
} }
long add_marked(char *filepath, char *type) /*
* force will not remove duplicate marked files, instead it just skip adding
*/
long add_marked(char *filepath, char *type, bool force)
{ {
file *current = marked; file *current = marked;
file *new_file = memalloc(sizeof(file)); file *new_file = memalloc(sizeof(file));
@ -153,18 +156,26 @@ long add_marked(char *filepath, char *type)
long index = 1; long index = 1;
while (current->next != NULL) { while (current->next != NULL) {
if (strcmp(current->path, new_file->path) == 0) { if (strcmp(current->path, new_file->path) == 0) {
if (force) {
return index;
} else {
remove_marked(current); remove_marked(current);
free_file(new_file); free_file(new_file);
return -1; return -1;
} }
}
current = current->next; current = current->next;
index++; index++;
} }
if (strcmp(current->path, new_file->path) == 0){ if (strcmp(current->path, new_file->path) == 0){
if (force) {
return 0;
} else {
remove_marked(current); remove_marked(current);
free_file(new_file); free_file(new_file);
return -1; return -1;
} }
}
current->next = new_file; current->next = new_file;
return index; return index;
} }