Thumbnail

rani/cscroll.git

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

commit f8c714fd5c5832de31820bedba50f4d4631b1b60 Author: Raniconduh <clagv.randomgames@gmail.com> Date: Fri Dec 03 09:21:03 2021 +0000 added file renaming fixed argument parsing diff --git a/README.md b/README.md index daa4682..e47c1a6 100644 --- a/README.md +++ b/README.md @@ -376 +377 @@ Files that are executable but have another identifier will keep the identifier b  * `.`: Toggle whether or not to show dot files  * `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))  * `:`: Open a commands prompt ([see 'Command Prompt' section](#command-prompt))  * `/`: Search for a file in the current directory using a POSIX regex  * `q`: Quit @@ -526 +5310 @@ The command prompt will show up upon pressing `:` and the prompt itself is prefi  * `ma`: **M**ark **A**ll files in the directory  * `mu`: **M**ark **U**nmark: Unmarks all files on the directory   +#### Renaming + +The `r` command will show a prompt (similar to a command prompt but without a prefix) where the new file name is to be expected. A file may only be renamed within the same directory. E.g. if the directory cscroll is in is `/home/user/downloads` and the file `image.png` is renamed to `/home/user/image.png`, it will fail. The file can only be renamed to something like `my_image.png`. (Attempting to move a file across directories is not possible with the rename function.) Mass renaming is not possible. +  ## Installation    The cscroll binary has one dependency to run: ncurses. To compile cscroll you must install `libncurses`. However, compilation through the makefile requires `pkg-config` as well. diff --git a/src/io.c b/src/io.c index 9b63a38..21277d8 100644 --- a/src/io.c +++ b/src/io.c @@ -2437 +2438 @@ char * curses_getline(char * p) {   echo();   noraw();   - printw("%s", p); + if (p) + printw("%s", p);   refresh();     char * inp = malloc(128); diff --git a/src/main.c b/src/main.c index e8228ac..34364f3 100644 --- a/src/main.c +++ b/src/main.c @@ -167 +167 @@ int main(int argc, char ** argv) {   if (!strcmp(argv[i], "-p")) {   print_path = true;   } else { - cwd = realpath(argv[1], NULL); + cwd = realpath(argv[i], NULL);   chdir(cwd);   }   } @@ -1776 +17735 @@ int main(int argc, char ** argv) {   dir_entries[cursor - 1]->marked = false;   if (n_marked_files) n_marked_files--;   } + break; + case 'r': + if (n_marked_files) + break; + char * nn = curses_getline(NULL); // new name + if (strlen(nn) < 1) { + free(nn); + break; + } + // old path + char * op = malloc(strlen(cwd) + strlen(dir_entries[cursor - 1]->name) + 2); + // new path + char * np = malloc(strlen(cwd) + strlen(nn) + 2); + sprintf(op, "%s/%s", cwd, dir_entries[cursor - 1]->name); + sprintf(np, "%s/%s", cwd, nn); + rename(op, np); + + free(op); + free(np); + free(nn); + + free_dir_entries(); + list_dir(cwd); + + if (cursor > n_dir_entries) cursor--; + else if (cursor < 1) cursor++; + + if (last_f > n_dir_entries) last_f--; +   break;   case ':':;   char * inp = curses_getline(":");