implemented delete and rename

This commit is contained in:
nova
2025-07-02 22:59:47 +02:00
parent aedfdd1ed5
commit 0a6509310d
6 changed files with 152 additions and 40 deletions

View File

@ -8,16 +8,17 @@
#include "config.h"
extern unsigned long selected_file_current;
extern unsigned long selected_file_last;
extern unsigned int file_modifiers;
unsigned long selected_file_current;
unsigned long selected_file_last;
extern pthread_mutex_t mutex_selection;
extern pthread_mutex_t mutex_rgt;
extern pthread_mutex_t mutex_mid;
extern file *mid_content;
extern file *lft_content;
extern file *rgt_content;
extern file file_current;
extern file *file_current;
extern unsigned int terminal_height;
extern unsigned int terminal_width;
@ -26,6 +27,7 @@ extern WINDOW *win_b;
extern char *rgt_buffer;
extern char *btm_buffer;
extern unsigned long mid_file_count;
extern unsigned int status;
int read_string(WINDOW *win, int y, int x, char *str);
@ -43,6 +45,15 @@ void user_interactions(char *input, WINDOW *win_b) {
void quit_program(){
status = STATUS_QUIT_PROGRAM;
}
void toggle_selection(){
pthread_mutex_lock(&mutex_selection);
pthread_mutex_lock(&mutex_mid);
mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED;
file_current->status ^= FILE_STATUS_SELECTED;
status |= (STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_mid);
pthread_mutex_unlock(&mutex_selection);
}
void move_down(){
pthread_mutex_lock(&mutex_selection);
/*capping the maximum file is done inside thread_mid */
@ -63,16 +74,16 @@ void move_right(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void move_left(){
if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) {
chdir(file_current.file_name);
if (file_current->file_type == FILE_TYPE_DIR || file_current->file_type == FILE_TYPE_SYMLINK) {
chdir(file_current->file_name);
} else {
unsigned long i = 0;
char *mime = get_mimetype(file_current.file_name);
char *mime = get_mimetype(file_current->file_name);
for (i = 0; i < mimetype_default_count; i++) {
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
cmd = concat(cmd, file_current.file_name);
cmd = concat(cmd, file_current->file_name);
cmd = concat(cmd, "\"");
btm_buffer = malloc(strlen(cmd));
@ -146,7 +157,7 @@ int read_string(WINDOW *win, int y, int x, char *str){
return err;
}
void open_with(){
btm_buffer = concat("open \"", file_current.file_name);
btm_buffer = concat("open \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\" with:");
status = STATUS_UPDATE_SCREEN_0;
@ -168,7 +179,7 @@ void open_with(){
if (!err) {
char *cmd = concat(str, " ./\"");
cmd = concat(cmd, file_current.file_name);
cmd = concat(cmd, file_current->file_name);
cmd = concat(cmd, "\"");
system(cmd);
@ -185,7 +196,7 @@ void open_with(){
void rename_hovered(){
btm_buffer = concat("rename \"", file_current.file_name);
btm_buffer = concat("rename \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\" to:");
status = STATUS_UPDATE_SCREEN_0;
@ -206,7 +217,7 @@ void rename_hovered(){
if (!err) {
char *cmd = concat("mv ./\"", file_current.file_name);
char *cmd = concat("mv ./\"", file_current->file_name);
cmd = concat(cmd, "\" ./\"");
cmd = concat(cmd, str);
cmd = concat(cmd, "\"");
@ -217,8 +228,86 @@ void rename_hovered(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
free(btm_buffer);
free(str);
}
void delete(){
unsigned int i = 0;
unsigned int hits = 0;
char *file_str = " ";
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
file_str = concat(file_str, "\"");
file_str = concat(file_str, mid_content[i].file_name);
file_str = concat(file_str, "\" ");
hits++;
}
}
if (hits) {
btm_buffer = concat("delete:", file_str);
} else {
btm_buffer = concat("delete: \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\"");
}
btm_buffer = concat(btm_buffer, "?");
btm_buffer = concat(btm_buffer, "\n\n");
btm_buffer = concat(btm_buffer, "(y/N)");
status = STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_width;
unsigned long local_height;
getmaxyx(win_b, local_height, local_width);
timeout(-1); /* negative numbers block until enter is pressed */
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
char ch = wgetch(win_b);
if (ch == 'y' || ch == 'Y') {
/* the second loop is used to add "./", wich is not being printed" */
char *cmd;
/* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
if (hits) {
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
cmd = concat("rm -rf ./\"", mid_content[i].file_name);
cmd = concat(cmd, "\"");
system(cmd);
}
}
free(btm_buffer);
btm_buffer = concat("deleted: ", file_str);
} else {
free(btm_buffer);
btm_buffer = concat("deleted: \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\"");
cmd = concat("rm -rf ./\"", file_current->file_name);
cmd = concat(cmd, "\"");
system(cmd);
}
/*system(cmd);*/
free(cmd);
} else {
free(btm_buffer);
btm_buffer = "cancled deletion";
}
timeout(10);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
if (hits) {
free(file_str);
}
}