write last directory to a file configured in config.h

This commit is contained in:
Night Kaly 2024-03-19 21:57:25 +00:00
parent ef41218ffa
commit a2321d5488
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
3 changed files with 61 additions and 6 deletions

View file

@ -14,7 +14,7 @@ MANDIR = $(PREFIX)/share/man/man1
LDFLAGS = $(shell pkg-config --libs ncurses) 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 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) $(TARGET): $(SRC)
$(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@ $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@

61
ccc.c
View file

@ -4,7 +4,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <linux/limits.h>
#include <dirent.h> /* directories etc. */ #include <dirent.h> /* directories etc. */
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
@ -25,6 +24,7 @@ long add_file_stat(char *filepath, int ftype);
void highlight_current_line(); void highlight_current_line();
void show_file_content(); void show_file_content();
void edit_file(); void edit_file();
int write_last_d();
void wpprintw(const char *line); void wpprintw(const char *line);
void init_windows(); void init_windows();
void draw_border_title(WINDOW *window, bool active); void draw_border_title(WINDOW *window, bool active);
@ -101,6 +101,10 @@ int main(int argc, char** argv)
switch (ch) { switch (ch) {
/* quit */ /* quit */
case 'q': case 'q':
if (write_last_d() == -1) {
/* prompt user so error message can be shown to user */
getch();
}
endwin(); endwin();
return 0; return 0;
@ -294,7 +298,11 @@ int main(int argc, char** argv)
delwin(preview_content); delwin(preview_content);
delwin(panel); delwin(panel);
endwin(); endwin();
half_width = COLS / 2;
init_windows(); init_windows();
refresh();
populate_files(cwd, 0);
highlight_current_line();
break; break;
default: default:
break; break;
@ -320,14 +328,13 @@ void change_dir(const char *buf, int selection)
/* /*
* Recursively create directory by creating each subdirectory * Recursively create directory by creating each subdirectory
* like mkdir -p
*/ */
int mkdir_p(const char *destdir) int mkdir_p(const char *destdir)
{ {
char *path = memalloc(PATH_MAX * sizeof(char)); char *path = memalloc(PATH_MAX * sizeof(char));
char *token = strtok(path, "/");
char dir_path[PATH_MAX] = ""; char dir_path[PATH_MAX] = "";
strcpy(path, destdir);
if (destdir[0] == '~') { if (destdir[0] == '~') {
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home == NULL) { if (home == NULL) {
@ -335,16 +342,20 @@ int mkdir_p(const char *destdir)
return -1; return -1;
} }
/* replace ~ with home */ /* replace ~ with home */
memmove(path + strlen(home), path + 1, strlen(path)); snprintf(path, PATH_MAX, "%s%s", home, destdir + 1);
memcpy(path, home, strlen(home)); } else {
strcpy(path, destdir);
} }
/* fix first / not appearing in the string */ /* fix first / not appearing in the string */
if (path[0] == '/') if (path[0] == '/')
strcat(dir_path, "/"); strcat(dir_path, "/");
char *token = strtok(path, "/");
while (token != NULL) { while (token != NULL) {
strcat(dir_path, token); strcat(dir_path, token);
strcat(dir_path, "/"); strcat(dir_path, "/");
if (mkdir(dir_path, 0755) == -1) { if (mkdir(dir_path, 0755) == -1) {
struct stat st; 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 * Print line to the panel
*/ */

View file

@ -1,6 +1,7 @@
/* Settings */ /* Settings */
#define PH 1 /* panel height */ #define PH 1 /* panel height */
#define JUMP_NUM 14 /* how long ctrl + u/d jump are */ #define JUMP_NUM 14 /* how long ctrl + u/d jump are */
#define PATH_MAX 4096
/* Calculate directories' sizes RECURSIVELY upon entering? */ /* Calculate directories' sizes RECURSIVELY upon entering? */
#define DIRS_SIZE false #define DIRS_SIZE false
@ -25,6 +26,9 @@ In COLS:
/* Default text editor */ /* Default text editor */
#define EDITOR "nvim" #define EDITOR "nvim"
/* File location to write last directory */
#define LAST_D "~/.cache/ccc/.ccc_d"
/* Keybindings */ /* Keybindings */
#define CTRLD 0x04 #define CTRLD 0x04
#define ENTER 0xA #define ENTER 0xA