1
0
mirror of https://gittea.dev/nova/th.git synced 2025-10-21 10:20:15 -04:00

added copy/paste function

This commit is contained in:
nova
2025-08-16 21:02:13 +02:00
parent 37d82bebf8
commit 9ea82511e6
4 changed files with 90 additions and 2 deletions

View File

@@ -50,6 +50,9 @@ static const binding key_binding[] = {
{ "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */
{ "dD", delete, NULL, "delete file" }, /* deletes currently hovered OR selected file/directory
* this means that it does not delete the hovered files if files are already selected */
{ "yy", yank_file, "copy", "copy/yank file/directory" },
{ "dd", yank_file, "cut", "cut file/directory" },
{ "pp", paste, NULL, "paste" },
{ "G", jump_bottom, NULL, "jump to last file in dir" },
{ "gg", jump_top, NULL, "jump to first file in dir" },

View File

@@ -8,6 +8,7 @@
#define STATUS_UPDATE_SCREEN_RESIZE 16
#define STATUS_UPDATE_SCREEN_RELOAD_FULL 32
#define STATUS_USER_ROOT 64
#define STATUS_INPUT_MATCH 128
#define SETTINGS_HAS_COLOR 1
@@ -51,6 +52,10 @@
#define FILE_TYPE_ORPHAN COLOR_ORPHAN
#define FILE_TYPE_OPEN_FILE 128 /* this is only used in rgt_content to print a file preview, not the dir */
#define YANK_IS_USED 1
#define YANK_CUT 2
#define YANK_COPY 4
#ifndef STRUCT_GUARD
#define STRUCT_GUARD
/* complex types are good actually */
@@ -80,6 +85,12 @@ typedef struct Binding {
void* black_magic;
char* comment;
} binding;
typedef struct Yank {
char status;
char *path;
char **list;
unsigned long count;
} yank;
#endif

View File

@@ -34,12 +34,13 @@ extern char *btm_buffer;
extern unsigned long mid_file_count;
extern unsigned int status;
extern char *start_path;
extern char *input;
unsigned int timeout_time = 0;
extern char *input;
unsigned int input_pass;
int parsed_input_number;
extern char *start_path;
yank yank_files = { 0 };
int read_string(WINDOW *win, int y, int x, char *str);
int strcmp_offset(char *in0, char *in1, char offset);
@@ -658,3 +659,73 @@ void cmd_on_selected(int passes, int index){
free(file_str);
}
}
void yank_file(int passes, int index){
unsigned long i;
if (yank_files.status & YANK_IS_USED) {
free(yank_files.path);
for (i = 0; i < yank_files.count; i++) {
free(yank_files.list[i]);
}
free(yank_files.list);
yank_files.count = 0;
}
yank_files.path=getcwd(NULL, 0);
yank_files.count = 0;
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
yank_files.count++;
}
}
if (yank_files.count == 0) {
yank_files.count = 1;
yank_files.list = (char**)malloc(yank_files.count * sizeof(char*));
*yank_files.list = malloc(strlen(mid_content[selected_file_current].file_name)+1);
strcpy(*yank_files.list, mid_content[selected_file_current].file_name);
} else {
yank_files.list = malloc(yank_files.count * sizeof(char*));
for (i = 0; i < mid_file_count && i < yank_files.count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
*yank_files.list = malloc(strlen(mid_content[i].file_name)+1);
strcpy(*yank_files.list, mid_content[i].file_name);
yank_files.list += 1;
}
}
yank_files.list -= yank_files.count;
}
yank_files.status |= YANK_IS_USED;
if (strncmp((char*)key_binding[index].black_magic, "cut", 3) == 0) {
yank_files.status |= YANK_CUT;
yank_files.status &= ~YANK_COPY;
} else {
yank_files.status |= YANK_COPY;
yank_files.status &= ~YANK_CUT;
}
}
void paste(){
unsigned long i;
for (i = 0; i < yank_files.count; i++) {
/*TODO(2025-08-14T22:10:44) escape path*/
char *cmd;
if (yank_files.status & YANK_COPY) {
cmd = concat("false | cp -r -i ", yank_files.path);
} else {
cmd = concat("mv ", yank_files.path);
}
cmd = concat(cmd, "/");
cmd = concat(cmd, *yank_files.list);
cmd = concat(cmd, " ./");
mvprintw(i, 0, cmd);
if (system(cmd) != 0) {
cmd = concat(cmd, *yank_files.list);
while (1) {
cmd = concat(cmd, "_");
if (system(cmd) == 0) {
break;
} }
}
yank_files.list++;
}
yank_files.list -= yank_files.count;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}

View File

@@ -22,7 +22,10 @@ void delete();
void makedir();
void makefile();
void update();
void enter_shell(int passes, int index);
void not_implemented(int passes, int index);
void jump_to_dir(int passes, int index);
void order_by(int passes, int index);
void cmd_on_selected(int passes, int index);
void yank_file();
void paste();