refactor code

This commit is contained in:
Night Kaly 2024-03-20 22:43:34 +00:00
parent d723be72ff
commit c9cfda54c3
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE

41
ccc.c
View file

@ -18,7 +18,7 @@
/* functions' definitions */ /* functions' definitions */
void show_help(); void show_help();
void start_ccc(); void start_ccc();
void change_dir(const char *buf, int selection); void change_dir(const char *buf, int selection, int ftype);
int mkdir_p(const char *destdir); int mkdir_p(const char *destdir);
void populate_files(const char *path, int ftype); void populate_files(const char *path, int ftype);
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf); int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
@ -94,6 +94,7 @@ int main(int argc, char** argv)
cwd = memalloc(PATH_MAX * sizeof(char)); cwd = memalloc(PATH_MAX * sizeof(char));
p_cwd = memalloc(PATH_MAX * sizeof(char)); p_cwd = memalloc(PATH_MAX * sizeof(char));
getcwd(cwd, PATH_MAX); getcwd(cwd, PATH_MAX);
populate_files(cwd, 0);
start_ccc(); start_ccc();
int ch, ch2; int ch, ch2;
@ -116,7 +117,7 @@ int main(int argc, char** argv)
/* reload using z */ /* reload using z */
case 'z': case 'z':
change_dir(cwd, 0); change_dir(cwd, 0, 0);
break; break;
/* go back by backspace or h or left arrow */ /* go back by backspace or h or left arrow */
@ -128,11 +129,11 @@ int main(int argc, char** argv)
char *last_slash = strrchr(cwd, '/'); char *last_slash = strrchr(cwd, '/');
if (last_slash != NULL) { if (last_slash != NULL) {
if (last_slash == cwd) { if (last_slash == cwd) {
strcpy(cwd, "/"); change_dir("/", 0, 0);
change_dir(cwd, 0); break;
} }
*last_slash = '\0'; *last_slash = '\0';
change_dir(cwd, 0); change_dir(cwd, 0, 0);
} }
break; break;
@ -145,7 +146,7 @@ int main(int argc, char** argv)
/* check if it is directory or a regular file */ /* check if it is directory or a regular file */
if (strncmp(c_file.type, "DIR", 3) == 0) { if (strncmp(c_file.type, "DIR", 3) == 0) {
/* change cwd to directory */ /* change cwd to directory */
change_dir(c_file.path, 0); change_dir(c_file.path, 0, 0);
} else if (strncmp(c_file.type, "REG", 3) == 0) { } else if (strncmp(c_file.type, "REG", 3) == 0) {
edit_file(); edit_file();
} }
@ -214,7 +215,7 @@ int main(int argc, char** argv)
if (home == NULL) { if (home == NULL) {
wpprintw("$HOME is not defined"); wpprintw("$HOME is not defined");
} else { } else {
change_dir(home, 0); change_dir(home, 0, 0);
} }
break; break;
@ -237,19 +238,19 @@ int main(int argc, char** argv)
} }
} }
} }
change_dir(trash_dir, 0); change_dir(trash_dir, 0, 0);
} }
break; break;
/* show directories' sizes */ /* show directories' sizes */
case 'A': case 'A':
dirs_size = !dirs_size; dirs_size = !dirs_size;
change_dir(cwd, 0); change_dir(cwd, 0, 0);
break; break;
/* go to previous dir */ /* go to previous dir */
case '-': case '-':
change_dir(p_cwd, 0); change_dir(p_cwd, 0, 0);
break; break;
/* show help */ /* show help */
@ -260,13 +261,13 @@ int main(int argc, char** argv)
/* toggle hidden files */ /* toggle hidden files */
case '.': case '.':
show_hidden = !show_hidden; show_hidden = !show_hidden;
change_dir(cwd, 0); change_dir(cwd, 0, 0);
break; break;
/* toggle file details */ /* toggle file details */
case 'i': case 'i':
file_details = !file_details; file_details = !file_details;
change_dir(cwd, 0); change_dir(cwd, 0, 0);
/* mark one file */ /* mark one file */
case SPACE: case SPACE:
@ -276,8 +277,7 @@ int main(int argc, char** argv)
/* mark all files in directory */ /* mark all files in directory */
case 'a': case 'a':
populate_files(cwd, 2); change_dir(cwd, current_selection, 2); /* reload current dir */
change_dir(cwd, current_selection); /* reload current dir */
break; break;
/* mark actions: */ /* mark actions: */
@ -353,15 +353,13 @@ void start_ccc()
{ {
half_width = COLS / 2; half_width = COLS / 2;
init_windows(); init_windows();
refresh();
populate_files(cwd, 0);
highlight_current_line(); highlight_current_line();
} }
/* /*
* Change directory in window with selection * Change directory in window with selection
*/ */
void change_dir(const char *buf, int selection) void change_dir(const char *buf, int selection, int ftype)
{ {
char *buf_dup; char *buf_dup;
if (buf == p_cwd) { if (buf == p_cwd) {
@ -375,7 +373,7 @@ void change_dir(const char *buf, int selection)
strcpy(cwd, buf_dup); strcpy(cwd, buf_dup);
arraylist_free(files); arraylist_free(files);
files = arraylist_init(100); files = arraylist_init(100);
populate_files(cwd, 0); populate_files(cwd, ftype);
current_selection = selection; current_selection = selection;
highlight_current_line(); highlight_current_line();
} }
@ -439,9 +437,6 @@ void populate_files(const char *path, int ftype)
DIR *dp; DIR *dp;
struct dirent *ep; struct dirent *ep;
#if DRAW_BORDERS
draw_border_title(directory_border, true);
#endif
if ((dp = opendir(path)) != NULL) { if ((dp = opendir(path)) != NULL) {
/* clear directory window to ready for printing */ /* clear directory window to ready for printing */
wclear(directory_content); wclear(directory_content);
@ -469,6 +464,9 @@ void populate_files(const char *path, int ftype)
} else { } else {
perror("ccc"); perror("ccc");
} }
#if DRAW_BORDERS
draw_border_title(directory_border, true);
#endif
} }
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
@ -822,6 +820,7 @@ void init_windows()
#endif #endif
scrollok(directory_content, true); scrollok(directory_content, true);
refresh();
} }
/* /*