Make file pickers work and add replace_home function

This commit is contained in:
Night Kaly 2024-04-06 20:41:36 +00:00
parent c9a289c837
commit 7afbdafd49
Signed by: night0721
GPG key ID: 957D67B8DB7A119B

65
ccc.c
View file

@ -32,6 +32,7 @@ void highlight_current_line();
void show_file_content(); void show_file_content();
void edit_file(); void edit_file();
void toggle_executable(); void toggle_executable();
void replace_home(char *str);
int write_last_d(); int write_last_d();
void create_file(); void create_file();
void delete_files(); void delete_files();
@ -42,6 +43,7 @@ void draw_border_title(WINDOW *window, bool active);
/* global variables */ /* global variables */
unsigned int focus = 0; unsigned int focus = 0;
long current_selection = 0; long current_selection = 0;
bool file_picker = false;
bool to_open_file = false; bool to_open_file = false;
bool dirs_size = DIRS_SIZE; bool dirs_size = DIRS_SIZE;
bool show_hidden = SHOW_HIDDEN; bool show_hidden = SHOW_HIDDEN;
@ -63,8 +65,12 @@ unsigned long total_dir_size;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc == 2) { if (argc == 3) {
if (strcmp(argv[1], "-h") == 0) if (strncmp(argv[2], "-p", 2) == 0)
file_picker = true;
}
if (argc <= 3 && argc != 1) {
if (strncmp(argv[1], "-h", 2) == 0)
die("Usage: ccc filename"); die("Usage: ccc filename");
struct stat st; struct stat st;
@ -182,13 +188,25 @@ int main(int argc, char** argv)
case 'l': case 'l':
strcpy(p_cwd, cwd); strcpy(p_cwd, cwd);
file c_file = files->items[current_selection]; file c_file = files->items[current_selection];
/* 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, 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) {
/* write opened file to a file for file pickers */
if (file_picker) {
char *opened_file_path = memalloc(PATH_MAX * sizeof(char));strcpy(opened_file_path, "~/.cache/ccc/opened_file");
replace_home(opened_file_path);
FILE *opened_file = fopen(opened_file_path, "w+");
fprintf(opened_file, "%s\n", c_file.path);
fclose(opened_file);
endwin();
return 0;
} else {
edit_file(); edit_file();
} }
}
break; break;
/* jump up (ctrl u) */ /* jump up (ctrl u) */
@ -402,6 +420,7 @@ char *check_trash_dir()
char *trash_dir; char *trash_dir;
#ifdef TRASH_DIR #ifdef TRASH_DIR
trash_dir = TRASH_DIR; trash_dir = TRASH_DIR;
strcpy(path, trash_dir);
#endif #endif
/* check if there is trash_dir */ /* check if there is trash_dir */
@ -412,16 +431,7 @@ char *check_trash_dir()
/* if trash_dir has ~ then make it $HOME */ /* if trash_dir has ~ then make it $HOME */
/* use path as trash_dir */ /* use path as trash_dir */
if (trash_dir[0] == '~') { if (trash_dir[0] == '~') {
char *home = getenv("HOME"); replace_home(path);
if (home == NULL) {
wpprintw("$HOME is not defined, can't read the trash directory");
return NULL;
}
/* replace ~ with home */
snprintf(path, PATH_MAX, "%s%s", home, trash_dir + 1);
}
else {
strcpy(path, trash_dir);
} }
/* if has access to trash_dir */ /* if has access to trash_dir */
@ -878,6 +888,19 @@ void toggle_executable()
} }
void replace_home(char *str)
{
char *home = getenv("HOME");
if (home == NULL) {
wpprintw("$HOME is not defined");
return;
}
char *after_tilde = estrdup(str + 1);
/* replace ~ with home */
snprintf(str, PATH_MAX, "%s%s", home, after_tilde);
free(after_tilde);
}
int write_last_d() int write_last_d()
{ {
#ifdef LAST_D #ifdef LAST_D
@ -890,27 +913,25 @@ int write_last_d()
wpprintw("Cannot get CCC_LAST_D variable, is it defined?"); wpprintw("Cannot get CCC_LAST_D variable, is it defined?");
return -1; return -1;
} else { } else {
char *last_ddup = memalloc(PATH_MAX * sizeof(char)); if (last_d[0] == '~') {
char *home = getenv("HOME"); replace_home(last_d);
if (home == NULL) {
wpprintw("$HOME is not defined");
return -1;
} }
/* replace ~ with home */ char *last_ddup = estrdup(last_d);
snprintf(last_ddup, PATH_MAX, "%s%s", home, last_d + 1);
char *last_d_dir = strrchr(last_d, '/'); char *last_d_dir = strrchr(last_ddup, '/');
if (last_d_dir != NULL) { if (last_d_dir != NULL) {
*last_d_dir = '\0'; /* truncate string */ *last_d_dir = '\0'; /* truncate string */
} }
mkdir_p(last_d); mkdir_p(last_ddup);
FILE *last_d_file = fopen(last_ddup, "w"); FILE *last_d_file = fopen(last_d, "w");
if (last_d_file == NULL) { if (last_d_file == NULL) {
wpprintw("Cannot open last directory file"); wpprintw("Cannot open last directory file");
return -1; return -1;
} }
fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); fwrite(cwd, strlen(cwd), sizeof(char), last_d_file);
fclose(last_d_file); fclose(last_d_file);
free(last_ddup);
free(last_d);
} }
return 0; return 0;
} }