From a2321d548871a5eac45c021e8937ed6d5b38b4ca Mon Sep 17 00:00:00 2001 From: night0721 Date: Tue, 19 Mar 2024 21:57:25 +0000 Subject: [PATCH] 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