From ef41218ffa72c63dd2e88e0cf55780957cebebe4 Mon Sep 17 00:00:00 2001 From: night0721 Date: Tue, 19 Mar 2024 16:30:36 +0000 Subject: [PATCH 1/4] header file update for add_marked --- file.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file.h b/file.h index b2890cb..57070aa 100644 --- a/file.h +++ b/file.h @@ -16,7 +16,7 @@ void clear_files(); void clear_marked(); long add_file(char *filepath, char *stats, char *type, int color); void remove_marked(file *marked_file); -long add_marked(char *filepath, char *type); +long add_marked(char *filepath, char *type, bool force); file *get_marked(long index); bool in_marked(char *path); file *get_file(long index); From a2321d548871a5eac45c021e8937ed6d5b38b4ca Mon Sep 17 00:00:00 2001 From: night0721 Date: Tue, 19 Mar 2024 21:57:25 +0000 Subject: [PATCH 2/4] write last directory to a file configured in config.h --- Makefile | 2 +- ccc.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++----- config.h | 4 ++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 809ef7f..53d25cf 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ MANDIR = $(PREFIX)/share/man/man1 LDFLAGS = $(shell pkg-config --libs ncurses) CFLAGS = -march=native -mtune=native -O3 -pipe -s -std=c99 -pedantic $(shell pkg-config --cflags ncurses) -Wall # -Wextra -Werror -SRC = ccc.c util.c file.c $(CONF) +SRC = ccc.c util.c file.c $(TARGET): $(SRC) $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@ diff --git a/ccc.c b/ccc.c index 5a9a493..3b13e58 100644 --- a/ccc.c +++ b/ccc.c @@ -4,7 +4,6 @@ #include #include #include -#include #include /* directories etc. */ #include #include @@ -25,6 +24,7 @@ long add_file_stat(char *filepath, int ftype); void highlight_current_line(); void show_file_content(); void edit_file(); +int write_last_d(); void wpprintw(const char *line); void init_windows(); void draw_border_title(WINDOW *window, bool active); @@ -101,6 +101,10 @@ int main(int argc, char** argv) switch (ch) { /* quit */ case 'q': + if (write_last_d() == -1) { + /* prompt user so error message can be shown to user */ + getch(); + } endwin(); return 0; @@ -294,7 +298,11 @@ int main(int argc, char** argv) delwin(preview_content); delwin(panel); endwin(); + half_width = COLS / 2; init_windows(); + refresh(); + populate_files(cwd, 0); + highlight_current_line(); break; default: break; @@ -320,14 +328,13 @@ void change_dir(const char *buf, int selection) /* * Recursively create directory by creating each subdirectory + * like mkdir -p */ int mkdir_p(const char *destdir) { char *path = memalloc(PATH_MAX * sizeof(char)); - char *token = strtok(path, "/"); char dir_path[PATH_MAX] = ""; - strcpy(path, destdir); if (destdir[0] == '~') { char *home = getenv("HOME"); if (home == NULL) { @@ -335,16 +342,20 @@ int mkdir_p(const char *destdir) return -1; } /* replace ~ with home */ - memmove(path + strlen(home), path + 1, strlen(path)); - memcpy(path, home, strlen(home)); + snprintf(path, PATH_MAX, "%s%s", home, destdir + 1); + } else { + strcpy(path, destdir); } + /* fix first / not appearing in the string */ if (path[0] == '/') strcat(dir_path, "/"); + char *token = strtok(path, "/"); while (token != NULL) { strcat(dir_path, token); strcat(dir_path, "/"); + if (mkdir(dir_path, 0755) == -1) { struct stat st; @@ -675,6 +686,46 @@ void edit_file() } } +int write_last_d() { + #ifdef LAST_D + char *last_d = memalloc(PATH_MAX * sizeof(char)); + strcpy(last_d, LAST_D); + #else + char *last_d = getenv("CCC_LAST_D"); + #endif + + if (last_d == NULL) { + wpprintw("Cannot get CCC_LAST_D variable, is it defined?"); + return -1; + } else { + char *last_ddup = memalloc(PATH_MAX * sizeof(char)); + char *home = getenv("HOME"); + if (home == NULL) { + wpprintw("$HOME is not defined"); + return -1; + } + /* replace ~ with home */ + snprintf(last_ddup, PATH_MAX, "%s%s", home, last_d + 1); + + char *last_d_dir = strrchr(last_d, '/'); + if (last_d_dir != NULL) { + *last_d_dir = '\0'; /* truncate string */ + } + mkdir_p(last_d); + char a[100]; + sprintf(a, "notify-send 'aaa %s'", last_ddup); + system(a); + FILE *last_d_file = fopen(last_ddup, "w"); + if (last_d_file == NULL) { + wpprintw("Cannot open last directory file"); + return -1; + } + fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); + fclose(last_d_file); + } + return 0; +} + /* * Print line to the panel */ diff --git a/config.h b/config.h index d596f02..60f4caa 100644 --- a/config.h +++ b/config.h @@ -1,6 +1,7 @@ /* Settings */ #define PH 1 /* panel height */ #define JUMP_NUM 14 /* how long ctrl + u/d jump are */ +#define PATH_MAX 4096 /* Calculate directories' sizes RECURSIVELY upon entering? */ #define DIRS_SIZE false @@ -25,6 +26,9 @@ In COLS: /* Default text editor */ #define EDITOR "nvim" +/* File location to write last directory */ +#define LAST_D "~/.cache/ccc/.ccc_d" + /* Keybindings */ #define CTRLD 0x04 #define ENTER 0xA From 1d702cd386cf5ece9ea8ea2e385a0923007f9737 Mon Sep 17 00:00:00 2001 From: night0721 Date: Tue, 19 Mar 2024 21:58:17 +0000 Subject: [PATCH 3/4] Revert "write last directory to a file configured in config.h" This reverts commit a2321d548871a5eac45c021e8937ed6d5b38b4ca. --- Makefile | 2 +- ccc.c | 61 +++++--------------------------------------------------- config.h | 4 ---- 3 files changed, 6 insertions(+), 61 deletions(-) diff --git a/Makefile b/Makefile index 53d25cf..809ef7f 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ MANDIR = $(PREFIX)/share/man/man1 LDFLAGS = $(shell pkg-config --libs ncurses) CFLAGS = -march=native -mtune=native -O3 -pipe -s -std=c99 -pedantic $(shell pkg-config --cflags ncurses) -Wall # -Wextra -Werror -SRC = ccc.c util.c file.c +SRC = ccc.c util.c file.c $(CONF) $(TARGET): $(SRC) $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@ diff --git a/ccc.c b/ccc.c index 3b13e58..5a9a493 100644 --- a/ccc.c +++ b/ccc.c @@ -4,6 +4,7 @@ #include #include #include +#include #include /* directories etc. */ #include #include @@ -24,7 +25,6 @@ long add_file_stat(char *filepath, int ftype); void highlight_current_line(); void show_file_content(); void edit_file(); -int write_last_d(); void wpprintw(const char *line); void init_windows(); void draw_border_title(WINDOW *window, bool active); @@ -101,10 +101,6 @@ int main(int argc, char** argv) switch (ch) { /* quit */ case 'q': - if (write_last_d() == -1) { - /* prompt user so error message can be shown to user */ - getch(); - } endwin(); return 0; @@ -298,11 +294,7 @@ int main(int argc, char** argv) delwin(preview_content); delwin(panel); endwin(); - half_width = COLS / 2; init_windows(); - refresh(); - populate_files(cwd, 0); - highlight_current_line(); break; default: break; @@ -328,13 +320,14 @@ void change_dir(const char *buf, int selection) /* * Recursively create directory by creating each subdirectory - * like mkdir -p */ int mkdir_p(const char *destdir) { char *path = memalloc(PATH_MAX * sizeof(char)); + char *token = strtok(path, "/"); char dir_path[PATH_MAX] = ""; + strcpy(path, destdir); if (destdir[0] == '~') { char *home = getenv("HOME"); if (home == NULL) { @@ -342,20 +335,16 @@ int mkdir_p(const char *destdir) return -1; } /* replace ~ with home */ - snprintf(path, PATH_MAX, "%s%s", home, destdir + 1); - } else { - strcpy(path, destdir); + memmove(path + strlen(home), path + 1, strlen(path)); + memcpy(path, home, strlen(home)); } - /* fix first / not appearing in the string */ if (path[0] == '/') strcat(dir_path, "/"); - char *token = strtok(path, "/"); while (token != NULL) { strcat(dir_path, token); strcat(dir_path, "/"); - if (mkdir(dir_path, 0755) == -1) { struct stat st; @@ -686,46 +675,6 @@ void edit_file() } } -int write_last_d() { - #ifdef LAST_D - char *last_d = memalloc(PATH_MAX * sizeof(char)); - strcpy(last_d, LAST_D); - #else - char *last_d = getenv("CCC_LAST_D"); - #endif - - if (last_d == NULL) { - wpprintw("Cannot get CCC_LAST_D variable, is it defined?"); - return -1; - } else { - char *last_ddup = memalloc(PATH_MAX * sizeof(char)); - char *home = getenv("HOME"); - if (home == NULL) { - wpprintw("$HOME is not defined"); - return -1; - } - /* replace ~ with home */ - snprintf(last_ddup, PATH_MAX, "%s%s", home, last_d + 1); - - char *last_d_dir = strrchr(last_d, '/'); - if (last_d_dir != NULL) { - *last_d_dir = '\0'; /* truncate string */ - } - mkdir_p(last_d); - char a[100]; - sprintf(a, "notify-send 'aaa %s'", last_ddup); - system(a); - FILE *last_d_file = fopen(last_ddup, "w"); - if (last_d_file == NULL) { - wpprintw("Cannot open last directory file"); - return -1; - } - fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); - fclose(last_d_file); - } - return 0; -} - /* * Print line to the panel */ diff --git a/config.h b/config.h index 60f4caa..d596f02 100644 --- a/config.h +++ b/config.h @@ -1,7 +1,6 @@ /* Settings */ #define PH 1 /* panel height */ #define JUMP_NUM 14 /* how long ctrl + u/d jump are */ -#define PATH_MAX 4096 /* Calculate directories' sizes RECURSIVELY upon entering? */ #define DIRS_SIZE false @@ -26,9 +25,6 @@ In COLS: /* Default text editor */ #define EDITOR "nvim" -/* File location to write last directory */ -#define LAST_D "~/.cache/ccc/.ccc_d" - /* Keybindings */ #define CTRLD 0x04 #define ENTER 0xA From f82d8044ae10cf587d8d2ae3e0d1458498c8e2ae Mon Sep 17 00:00:00 2001 From: night0721 Date: Tue, 19 Mar 2024 22:07:05 +0000 Subject: [PATCH 4/4] write last directory before quit --- Makefile | 2 +- ccc.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++------ config.h | 4 ++++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 809ef7f..53d25cf 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ MANDIR = $(PREFIX)/share/man/man1 LDFLAGS = $(shell pkg-config --libs ncurses) CFLAGS = -march=native -mtune=native -O3 -pipe -s -std=c99 -pedantic $(shell pkg-config --cflags ncurses) -Wall # -Wextra -Werror -SRC = ccc.c util.c file.c $(CONF) +SRC = ccc.c util.c file.c $(TARGET): $(SRC) $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@ diff --git a/ccc.c b/ccc.c index 5a9a493..0a4ad66 100644 --- a/ccc.c +++ b/ccc.c @@ -4,7 +4,6 @@ #include #include #include -#include #include /* directories etc. */ #include #include @@ -25,6 +24,7 @@ long add_file_stat(char *filepath, int ftype); void highlight_current_line(); void show_file_content(); void edit_file(); +int write_last_d(); void wpprintw(const char *line); void init_windows(); void draw_border_title(WINDOW *window, bool active); @@ -101,6 +101,10 @@ int main(int argc, char** argv) switch (ch) { /* quit */ case 'q': + if (write_last_d() == -1) { + /* prompt user so error message can be shown to user */ + getch(); + } endwin(); return 0; @@ -294,7 +298,11 @@ int main(int argc, char** argv) delwin(preview_content); delwin(panel); endwin(); + half_width = COLS / 2; init_windows(); + refresh(); + populate_files(cwd, 0); + highlight_current_line(); break; default: break; @@ -320,14 +328,13 @@ void change_dir(const char *buf, int selection) /* * Recursively create directory by creating each subdirectory + * like mkdir -p */ int mkdir_p(const char *destdir) { char *path = memalloc(PATH_MAX * sizeof(char)); - char *token = strtok(path, "/"); char dir_path[PATH_MAX] = ""; - strcpy(path, destdir); if (destdir[0] == '~') { char *home = getenv("HOME"); if (home == NULL) { @@ -335,13 +342,16 @@ int mkdir_p(const char *destdir) return -1; } /* replace ~ with home */ - memmove(path + strlen(home), path + 1, strlen(path)); - memcpy(path, home, strlen(home)); - } + snprintf(path, PATH_MAX, "%s%s", home, destdir + 1); + } else { + strcpy(path, destdir); + } + /* fix first / not appearing in the string */ if (path[0] == '/') strcat(dir_path, "/"); + char *token = strtok(path, "/"); while (token != NULL) { strcat(dir_path, token); strcat(dir_path, "/"); @@ -675,6 +685,42 @@ void edit_file() } } +int write_last_d() { + #ifdef LAST_D + char *last_d = memalloc(PATH_MAX * sizeof(char)); + strcpy(last_d, LAST_D); + #else + char *last_d = getenv("CCC_LAST_D"); + #endif + if (last_d == NULL) { + wpprintw("Cannot get CCC_LAST_D variable, is it defined?"); + return -1; + } else { + char *last_ddup = memalloc(PATH_MAX * sizeof(char)); + char *home = getenv("HOME"); + if (home == NULL) { + wpprintw("$HOME is not defined"); + return -1; + } + /* replace ~ with home */ + snprintf(last_ddup, PATH_MAX, "%s%s", home, last_d + 1); + + char *last_d_dir = strrchr(last_d, '/'); + if (last_d_dir != NULL) { + *last_d_dir = '\0'; /* truncate string */ + } + mkdir_p(last_d); + FILE *last_d_file = fopen(last_ddup, "w"); + if (last_d_file == NULL) { + wpprintw("Cannot open last directory file"); + return -1; + } + fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); + fclose(last_d_file); + } + return 0; +} + /* * Print line to the panel */ diff --git a/config.h b/config.h index d596f02..60f4caa 100644 --- a/config.h +++ b/config.h @@ -1,6 +1,7 @@ /* Settings */ #define PH 1 /* panel height */ #define JUMP_NUM 14 /* how long ctrl + u/d jump are */ +#define PATH_MAX 4096 /* Calculate directories' sizes RECURSIVELY upon entering? */ #define DIRS_SIZE false @@ -25,6 +26,9 @@ In COLS: /* Default text editor */ #define EDITOR "nvim" +/* File location to write last directory */ +#define LAST_D "~/.cache/ccc/.ccc_d" + /* Keybindings */ #define CTRLD 0x04 #define ENTER 0xA