implemented delete and rename
This commit is contained in:
20
backend.c
20
backend.c
@ -142,6 +142,7 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c
|
||||
|
||||
unsigned long i = 0;
|
||||
unsigned long j = 0;
|
||||
char is_selected = 0;
|
||||
|
||||
unsigned long offset_front = 2;
|
||||
if (*dir_file_count > 9) {
|
||||
@ -151,7 +152,14 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c
|
||||
|
||||
unsigned long offset_back = *line_width - (snprintf(NULL,0,"%ld",dir_content[i].file_size) + 1);
|
||||
unsigned long allowed_width = *line_width+1;
|
||||
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
is_selected = 1;
|
||||
wattron(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
is_selected = 0;
|
||||
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
}
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_HOVER) {
|
||||
wattron(win, A_REVERSE);
|
||||
@ -163,7 +171,7 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c
|
||||
mvwaddch(win, i, j, '~');
|
||||
break;
|
||||
}
|
||||
mvwaddch(win, i, offset_front+j, dir_content[i].file_name[j]);
|
||||
mvwaddch(win, i, offset_front+j+is_selected, dir_content[i].file_name[j]);
|
||||
}
|
||||
mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size);
|
||||
wattroff(win, A_REVERSE);
|
||||
@ -174,11 +182,15 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c
|
||||
mvwaddch(win, i, j, '~');
|
||||
break;
|
||||
}
|
||||
mvwaddch(win, i, offset_front+j, dir_content[i].file_name[j]);
|
||||
mvwaddch(win, i, offset_front+j+is_selected, dir_content[i].file_name[j]);
|
||||
}
|
||||
mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size);
|
||||
}
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
wattroff(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
}
|
||||
}
|
||||
free(hover_bg);
|
||||
}
|
||||
|
4
config.h
4
config.h
@ -19,6 +19,8 @@ static mimetype mimetype_default_cmd[] = {
|
||||
static binding key_binding[] = {
|
||||
/*key action */
|
||||
{ "q", quit_program },
|
||||
{ " ", toggle_selection }, /* on hovered file/directory */
|
||||
|
||||
{ "h", move_right }, /* moves one dir up */
|
||||
{ "t", move_down },
|
||||
{ "n", move_up },
|
||||
@ -26,6 +28,8 @@ static binding key_binding[] = {
|
||||
|
||||
{ "\n", open_with }, /* opens the hovered file with an arbitrary command */
|
||||
{ "r", rename_hovered }, /* renames currently hovered file/directory */
|
||||
{ "d", delete }, /* deletes currently hovered OR selected file/directory
|
||||
* this means that it does not delete the hovered files if files are already selected */
|
||||
|
||||
{ "gg", jump_top },
|
||||
{ "G", jump_bottom },
|
||||
|
@ -21,7 +21,7 @@
|
||||
/*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/
|
||||
|
||||
#define FILE_STATUS_HOVER 1
|
||||
#define FILE_STATUS_SELECTED 2;
|
||||
#define FILE_STATUS_SELECTED 2
|
||||
#define FILE_STATUS_IS_REGULAR_FILE 4
|
||||
#define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */
|
||||
|
||||
|
113
interactions.c
113
interactions.c
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
void user_interactions(char *input, WINDOW *win_b);
|
||||
void quit_program();
|
||||
void toggle_selection();
|
||||
void move_right();
|
||||
void move_up();
|
||||
void move_down();
|
||||
@ -17,3 +18,4 @@ void jump_top();
|
||||
void toggle_hidden_files();
|
||||
void open_with();
|
||||
void rename_hovered();
|
||||
void delete();
|
||||
|
51
threading.c
51
threading.c
@ -23,7 +23,7 @@ file *lft_content;
|
||||
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
||||
char *btm_buffer;
|
||||
|
||||
file file_current;
|
||||
file *file_current;
|
||||
|
||||
char *top_content; /* current path */
|
||||
|
||||
@ -34,9 +34,9 @@ unsigned long top_width;
|
||||
|
||||
|
||||
|
||||
unsigned long selected_file_current=0;
|
||||
unsigned long selected_file_last=0;
|
||||
extern unsigned int status;
|
||||
extern unsigned long selected_file_current;
|
||||
extern unsigned long selected_file_last;
|
||||
|
||||
void *thread_rgt(void *data);
|
||||
|
||||
@ -63,20 +63,23 @@ void *thread_mid(void *data){
|
||||
|
||||
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
while(1) { /* this useless while loop exists for the singular purpose of making valgrind happy */
|
||||
if (selected_file_current >= mid_file_count) {
|
||||
selected_file_current = mid_file_count-1;
|
||||
}
|
||||
mid_content[selected_file_current].status = FILE_STATUS_HOVER;
|
||||
if (selected_file_current != selected_file_last) {
|
||||
mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER;
|
||||
}
|
||||
selected_file_last = selected_file_current;
|
||||
} else {
|
||||
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
|
||||
if (selected_file_current != selected_file_last) {
|
||||
mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER;
|
||||
}
|
||||
selected_file_last = selected_file_current;
|
||||
|
||||
file_current.file_name = malloc(mid_content[selected_file_current].file_name_width + 1);
|
||||
strcpy(file_current.file_name, mid_content[selected_file_current].file_name);
|
||||
file_current.file_name_width = mid_content[selected_file_current].file_name_width;
|
||||
file_current.file_size = mid_content[selected_file_current].file_size;
|
||||
file_current.file_type = mid_content[selected_file_current].file_type;
|
||||
file_current->file_name = mid_content[selected_file_current].file_name;
|
||||
file_current->file_name_width = mid_content[selected_file_current].file_name_width;
|
||||
file_current->file_size = mid_content[selected_file_current].file_size;
|
||||
file_current->file_type = mid_content[selected_file_current].file_type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
|
||||
}
|
||||
@ -119,11 +122,11 @@ void *thread_rgt(void *data){
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
free(rgt_content);
|
||||
rgt_content = malloc(sizeof(file));
|
||||
rgt_content[0].file_name = malloc(file_current.file_name_width + 1);
|
||||
strcpy(rgt_content[0].file_name, file_current.file_name);
|
||||
rgt_content[0].file_name_width = file_current.file_name_width;
|
||||
rgt_content[0].file_size = file_current.file_size;
|
||||
rgt_content[0].file_type = file_current.file_type;
|
||||
rgt_content[0].file_name = malloc(file_current->file_name_width + 1);
|
||||
strcpy(rgt_content[0].file_name, file_current->file_name);
|
||||
rgt_content[0].file_name_width = file_current->file_name_width;
|
||||
rgt_content[0].file_size = file_current->file_size;
|
||||
rgt_content[0].file_type = file_current->file_type;
|
||||
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
|
||||
@ -183,10 +186,12 @@ void threading_init(){
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
btm_buffer = malloc(sizeof(char));
|
||||
|
||||
file_current.file_type = 0;
|
||||
file_current.file_size = 1;
|
||||
file_current.file_name_width = 1;
|
||||
file_current.file_name = "a";
|
||||
file_current = malloc(sizeof(file));
|
||||
file_current->file_type = 0;
|
||||
file_current->file_size = 1;
|
||||
file_current->file_name_width = 1;
|
||||
file_current->file_name = malloc(sizeof("a"));
|
||||
strcpy(file_current->file_name, "a");
|
||||
|
||||
pthread_mutex_init(&mutex_top, NULL);
|
||||
pthread_mutex_init(&mutex_mid, NULL);
|
||||
|
Reference in New Issue
Block a user