Thumbnail

rani/cscroll.git

Clone URL: https://git.buni.party/rani/cscroll.git

commit acda682431783393c200b363d37e78f875984e4e Author: Raniconduh <clagv.randomgames@gmail.com> Date: Fri Dec 03 18:54:54 2021 +0000 Added file cutting & pasting diff --git a/README.md b/README.md index e47c1a6..2ead1cc 100644 --- a/README.md +++ b/README.md @@ -386 +388 @@ Files that are executable but have another identifier will keep the identifier b  * `d`: Delete the file the cursor is on (a [prompt](#options-prompt) will be shown first)  * `m`: Mark the file the cursor is on  * `r`: Rename the file the cursor is on ([see file renaming](#renaming)) +* `c`: Cut the file the cursor is currently on or all the marked files. Pressing twice in the same directory will cancel the cut +* `p`: Paste all cut files into the current directory. Pasting in the same directory where the cut originated will cancel the cut  * `:`: Open a commands prompt ([see 'Command Prompt' section](#command-prompt))  * `/`: Search for a file in the current directory using a POSIX regex  * `q`: Quit diff --git a/include/commands.h b/include/commands.h index ad8e314..0cd8f96 100644 --- a/include/commands.h +++ b/include/commands.h @@ -35 +312 @@    void ext_open(char *);  long search_file(long, char *); +void create_cuts(char **); +void free_cuts(void); +void paste_cuts(char *); + +extern bool cutting; +extern char * cut_start_dir; +extern char ** cuts;    #endif /* COMMANDS_H */ diff --git a/src/commands.c b/src/commands.c index 658a5a9..901ee94 100644 --- a/src/commands.c +++ b/src/commands.c @@ -96 +910 @@  #include "dir.h"  #include "commands.h"   +bool cutting = false; +char * cut_start_dir = NULL; +char ** cuts = NULL; +  void ext_open(char * file) {   char * f = malloc(strlen(cwd) + strlen(file) + 2);   sprintf(f, "%s/%s", cwd, file); @@ -483 +5254 @@ long search_file(long c, char * s) {   return ret;  }   + +void create_cuts(char ** ls) { + if (!cuts) cuts = malloc(0); + // if not null, use passed list + if (ls) { + size_t total_cuts = 0; + for (char ** p = ls; *p; p++) total_cuts++; + cuts = realloc(cuts, sizeof(char*) * (total_cuts + 1)); + size_t i; + for (i = 0; i < total_cuts; i++) { + cuts[i] = malloc(strlen(ls[i]) + 1); + strcpy(cuts[i], ls[i]); + } + cuts[i] = NULL; + return; + } + // else use marked files + // count total number of cuts to make + size_t total_cuts = 0; + for (size_t i = 0; i < n_dir_entries; i++) + if (dir_entries[i]->marked) total_cuts++; + cuts = realloc(cuts, sizeof(char*) * (total_cuts + 1)); + // store cuts into list + char ** p = cuts; + for (size_t i = 0; i < n_dir_entries; i++) { + if (dir_entries[i]->marked) { + *p = malloc(strlen(dir_entries[i]->name) + 1); + strcpy(*p, dir_entries[i]->name); + p++; + } + } + *p = NULL; +} + + +void free_cuts(void) { + for (char ** p = cuts; *p; p++) + free(*p); + free(cuts); + cuts = NULL; +} + +void paste_cuts(char * path) { + for (char ** p = cuts; *p; p++) { + char old_path[strlen(cut_start_dir) + strlen(*p) + 1]; + char new_path[strlen(path) + strlen(*p) + 1]; + sprintf(old_path, "%s/%s", cut_start_dir, *p); + sprintf(new_path, "%s/%s", path, *p); + rename(old_path, new_path); + } +} diff --git a/src/io.c b/src/io.c index 21277d8..6179b57 100644 --- a/src/io.c +++ b/src/io.c @@ -1067 +1067 @@ void curses_write_file(struct dir_entry_t * dir_entry, bool highlight) {  char curses_getch(void) {   char c = getch();   - char seq[5]; + char seq[5] = {0};   char * ptr = seq;     if (c == 27) { diff --git a/src/main.c b/src/main.c index 34364f3..2b2bb2d 100644 --- a/src/main.c +++ b/src/main.c @@ -586 +587 @@ int main(int argc, char ** argv) {   printw("\tPermission Denied");   attroff(COLOR_PAIR(RED));   } + if (cutting) printw(" cut");   printw("\n\n");     // print files @@ -2066 +20753 @@ int main(int argc, char ** argv) {     if (last_f > n_dir_entries) last_f--;   + break; + case 'c': + // stop cutting if pressed twice + if (cutting) { + cutting = false; + free_cuts(); + free(cut_start_dir); + cut_start_dir = NULL; + break; + } + cutting = true; + // copy cwd to start directory for cuts + cut_start_dir = malloc(strlen(cwd) + 1); + strcpy(cut_start_dir, cwd); + if (!n_marked_files) { + char ** args = malloc(sizeof(char*) * 2); + args[0] = malloc(strlen(dir_entries[cursor - 1]->name) + 1); + strcpy(args[0], dir_entries[cursor - 1]->name); + args[1] = NULL; + create_cuts(args); + free(args[0]); + free(args); + } else + create_cuts(NULL); + break; + case 'p': + if (!cutting) break; + // only paste to different directories + if (!strcmp(cwd, cut_start_dir)) { + cutting = false; + free_cuts(); + free(cut_start_dir); + cut_start_dir = NULL; + break; + } + paste_cuts(cwd); + cutting = false; + free_cuts(); + free(cut_start_dir); + cut_start_dir = NULL; + + free_dir_entries(); + list_dir(cwd); + + first_f = 0; + last_f = LAST_F; + cursor = 1;   break;   case ':':;   char * inp = curses_getline(":");