Fix rename, and add yank filename to clipboard, and make list files run everytime a key press

This commit is contained in:
Night Kaly 2024-11-17 18:10:26 +00:00
parent c774511889
commit a830c69e78
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

81
ccc.c
View file

@ -69,9 +69,10 @@ void create_dir(void);
void create_file(void); void create_file(void);
void delete_files(void); void delete_files(void);
void start_shell(void); void start_shell(void);
void yank_clipboard(void);
void wpprintw(const char *fmt, ...); void wpprintw(const char *fmt, ...);
void move_cursor(int row, int col); void move_cursor(int row, int col);
int read_key(void); int readch(void);
int get_window_size(int *row, int *col); int get_window_size(int *row, int *col);
void bprintf(const char *fmt, ...); void bprintf(const char *fmt, ...);
@ -168,19 +169,18 @@ int main(int argc, char **argv)
if (to_open_file) { if (to_open_file) {
sel_file = arraylist_search(files, argv_cp, 1); sel_file = arraylist_search(files, argv_cp, 1);
list_files();
} }
int ch, ch2; int ch, run = 1;
int run = 1;
while (run) { while (run) {
ch = read_key(); list_files();
ch = readch();
switch (ch) { switch (ch) {
/* quit */ /* quit */
case 'q': case 'q':
if (write_last_d() == -1) { if (write_last_d() == -1) {
/* prompt user so error message can be shown to user */ /* prompt user so error message can be shown to user */
read_key(); readch();
} }
cleanup(); cleanup();
run = 0; run = 0;
@ -242,8 +242,6 @@ int main(int argc, char **argv)
sel_file -= jump_num; sel_file -= jump_num;
else else
sel_file = 0; sel_file = 0;
list_files();
break; break;
/* go up */ /* go up */
@ -251,8 +249,6 @@ int main(int argc, char **argv)
case 'k': case 'k':
if (sel_file > 0) if (sel_file > 0)
sel_file--; sel_file--;
list_files();
break; break;
/* jump down */ /* jump down */
@ -261,8 +257,6 @@ int main(int argc, char **argv)
sel_file += jump_num; sel_file += jump_num;
else else
sel_file = (files->length - 1); sel_file = (files->length - 1);
list_files();
break; break;
/* go down */ /* go down */
@ -270,20 +264,16 @@ int main(int argc, char **argv)
case 'j': case 'j':
if (sel_file < (files->length - 1)) if (sel_file < (files->length - 1))
sel_file++; sel_file++;
list_files();
break; break;
/* jump to the bottom */ /* jump to the bottom */
case 'G': case 'G':
sel_file = (files->length - 1); sel_file = (files->length - 1);
list_files();
break; break;
/* jump to the top */ /* jump to the top */
case 'g': case 'g':
sel_file = 0; sel_file = 0;
list_files();
break; break;
/* '~' go to $HOME */ /* '~' go to $HOME */
@ -292,6 +282,7 @@ int main(int argc, char **argv)
if (home == NULL) { if (home == NULL) {
wpprintw("$HOME not defined"); wpprintw("$HOME not defined");
} else { } else {
strcpy(p_cwd, cwd);
change_dir(home, 0, 0); change_dir(home, 0, 0);
} }
break; break;
@ -369,10 +360,13 @@ int main(int argc, char **argv)
start_shell(); start_shell();
break; break;
case 'y':
yank_clipboard();
break;
/* mark one file */ /* mark one file */
case SPACE: case SPACE:
add_file_stat(files->items[sel_file].name, files->items[sel_file].path, 1); add_file_stat(files->items[sel_file].name, files->items[sel_file].path, 1);
list_files();
break; break;
/* mark all files in directory */ /* mark all files in directory */
@ -464,8 +458,10 @@ void show_help(void)
"r: rename\n\nspace: mark file\na: mark all files in directory\nd: trash" "r: rename\n\nspace: mark file\na: mark all files in directory\nd: trash"
"\n\n?: show help\nq: exit with last dir written to file\n" "\n\n?: show help\nq: exit with last dir written to file\n"
"ctrl+c exit without writing last dir" "ctrl+c exit without writing last dir"
"\nPress any key to continue"
); );
wpprintw("Visit https://github.com/night0721/ccc or use 'man ccc' for help"); wpprintw("Visit https://github.com/night0721/ccc or use 'man ccc' for help");
readch();
} }
/* /*
@ -600,8 +596,6 @@ void populate_files(const char *path, int ftype, ArrayList **list)
free(tmp2); free(tmp2);
} }
closedir(dp); closedir(dp);
if (list == &files)
list_files();
} else { } else {
wpprintw("stat failed: %s", strerror(errno)); wpprintw("stat failed: %s", strerror(errno));
} }
@ -1005,7 +999,6 @@ void edit_file(void)
} else if (pid > 0) { } else if (pid > 0) {
/* Parent process */ /* Parent process */
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
list_files();
} else { } else {
/* Fork failed */ /* Fork failed */
wpprintw("fork failed: %s", strerror(errno)); wpprintw("fork failed: %s", strerror(errno));
@ -1048,7 +1041,7 @@ int write_last_d(void)
if (!strcmp(last_d, "")) { if (!strcmp(last_d, "")) {
strcpy(last_d, getenv("CCC_LAST_D")); strcpy(last_d, getenv("CCC_LAST_D"));
if (!strcmp(last_d, "")) { if (!strcmp(last_d, "")) {
wpprintw("$CCC_LAST_D not defined (Press enter to continue)"); wpprintw("$CCC_LAST_D not defined (Press any key to continue)");
return -1; return -1;
} }
} else { } else {
@ -1065,7 +1058,7 @@ int write_last_d(void)
mkdir_p(last_ddup); mkdir_p(last_ddup);
FILE *last_d_file = fopen(last_d, "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 (Press enter to continue)"); wpprintw("Cannot open last directory file (Press any key to continue)");
return -1; return -1;
} }
fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); fwrite(cwd, strlen(cwd), sizeof(char), last_d_file);
@ -1082,7 +1075,6 @@ int sort_compare(const void *a, const void *b)
void sort_files(void) void sort_files(void)
{ {
qsort(files->items, files->length, sizeof(file), sort_compare); qsort(files->items, files->length, sizeof(file), sort_compare);
list_files();
} }
char *get_panel_string(char *prompt) char *get_panel_string(char *prompt)
@ -1091,9 +1083,10 @@ char *get_panel_string(char *prompt)
char *buf = memalloc(bufsize); char *buf = memalloc(bufsize);
size_t buflen = 0; size_t buflen = 0;
buf[0] = '\0'; buf[0] = '\0';
bprintf("\033[?25h");
while (1) { while (1) {
wpprintw(prompt); wpprintw("%s%s", prompt, buf);
int c = read_key(); int c = readch();
if (c == BACKSPACE) { if (c == BACKSPACE) {
if (buflen != 0) { if (buflen != 0) {
buf[--buflen] = '\0'; buf[--buflen] = '\0';
@ -1101,10 +1094,12 @@ char *get_panel_string(char *prompt)
} else if (c == '\033') { } else if (c == '\033') {
wpprintw(""); wpprintw("");
free(buf); free(buf);
bprintf("\033[?25l");
return NULL; return NULL;
} else if (c == '\r') { } else if (c == '\r') {
wpprintw(""); wpprintw("");
if (buflen != 0) { if (buflen != 0) {
bprintf("\033[?25l");
return buf; return buf;
} }
} else if (!iscntrl(c) && c < 128) { } else if (!iscntrl(c) && c < 128) {
@ -1120,6 +1115,7 @@ char *get_panel_string(char *prompt)
if (buf[0] == '~') if (buf[0] == '~')
replace_home(buf); replace_home(buf);
bprintf("\033[?25l");
return buf; return buf;
} }
@ -1127,6 +1123,9 @@ void rename_file(void)
{ {
char *filename = files->items[sel_file].path; char *filename = files->items[sel_file].path;
char *input = get_panel_string("Rename file: "); char *input = get_panel_string("Rename file: ");
if (!input) {
return;
}
char *newfilename = estrdup(filename); char *newfilename = estrdup(filename);
/* remove basename of newfilename */ /* remove basename of newfilename */
char *last_slash = strrchr(newfilename, '/'); char *last_slash = strrchr(newfilename, '/');
@ -1135,8 +1134,8 @@ void rename_file(void)
strcat(newfilename, "/"); strcat(newfilename, "/");
strcat(newfilename, input); strcat(newfilename, input);
if (rename(filename, newfilename)) { if (rename(filename, newfilename)) {
wpprintw("rename failed: %s (Press enter to continue)", strerror(errno)); wpprintw("rename failed: %s (Press any key to continue)", strerror(errno));
read_key(); readch();
} else { } else {
change_dir(cwd, 0, 0); change_dir(cwd, 0, 0);
wpprintw("Renamed %s to %s", filename, newfilename); wpprintw("Renamed %s to %s", filename, newfilename);
@ -1150,13 +1149,13 @@ void goto_dir(void)
char *input = get_panel_string("Goto dir: "); char *input = get_panel_string("Goto dir: ");
struct stat st; struct stat st;
if (lstat(input, &st)) { if (lstat(input, &st)) {
wpprintw("lstat failed: %s (Press enter to continue)", strerror(errno)); wpprintw("lstat failed: %s (Press any key to continue)", strerror(errno));
read_key(); readch();
} }
/* chdir to directory from argument */ /* chdir to directory from argument */
if (S_ISDIR(st.st_mode) && chdir(input)) { if (S_ISDIR(st.st_mode) && chdir(input)) {
wpprintw("chdir failed: %s (Press enter to continue)", strerror(errno)); wpprintw("chdir failed: %s (Press any key to continue)", strerror(errno));
read_key(); readch();
} }
getcwd(cwd, PATH_MAX); getcwd(cwd, PATH_MAX);
change_dir(cwd, 0, 0); change_dir(cwd, 0, 0);
@ -1229,7 +1228,6 @@ void start_shell(void)
} else if (pid > 0) { } else if (pid > 0) {
/* Parent process */ /* Parent process */
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
list_files();
bprintf("\033[?25l"); bprintf("\033[?25l");
} else { } else {
/* Fork failed */ /* Fork failed */
@ -1238,6 +1236,23 @@ void start_shell(void)
} }
} }
void yank_clipboard(void)
{
pid_t pid = fork();
if (pid == 0) {
/* Child process */
execlp(clipboard, clipboard, files->items[sel_file].name, NULL);
_exit(1); /* Exit if exec fails */
} else if (pid > 0) {
/* Parent process */
waitpid(pid, NULL, 0);
bprintf("\033[?25l");
} else {
/* Fork failed */
wpprintw("fork failed: %s", strerror(errno));
}
}
/* /*
* Print line to the panel * Print line to the panel
*/ */
@ -1260,7 +1275,7 @@ void move_cursor(int row, int col)
bprintf("\033[%d;%dH", row, col); bprintf("\033[%d;%dH", row, col);
} }
int read_key(void) int readch(void)
{ {
int nread; int nread;
char c; char c;