Thumbnail

rani/cscroll.git

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

commit 103114e859ac7ca50002751fc30d9f9a138976bc Author: rani <clagv.randomgames@gmail.com> Date: Fri Jan 27 19:25:31 2023 +0000 Added scrolling in curses_getline() when input goes past screen diff --git a/src/io.c b/src/io.c index a74507e..134b02f 100644 --- a/src/io.c +++ b/src/io.c @@ -76 +77 @@  #include <unistd.h>  #include <locale.h>  #include <fcntl.h> +#include <ctype.h>  #include <time.h>  #include <pwd.h>  #include <grp.h> @@ -37233 +37355 @@ done:;    char * curses_getline(char * p) {   curs_set(1); - echo(); - noraw();   - if (p) - printw("%s", p); - refresh(); + size_t plen = 0; + if (p) { + addstr(p); + plen = strlen(p); + refresh(); + }     char * inp = malloc(128);   size_t l = 0;   char c;   while ((c = getch()) != '\n') { - if (l % 127 == 0) { - // add 1 to size as l starts at 0 + if ((l + 1) % 127 == 0) { + // add 1 byte to prevent buffer overrun on deletion   inp = realloc(inp, l + 129);   }     if (c == 127) {   if (l > 0) l--; - addch(' '); + } else if (isprint(c)) { + // do not bother with non-printable characters + // line editing may be added later + inp[l++] = c; + } else continue; + + int y, x; + getyx(stdscr, y, x); + + // it is time to scroll + if (l >= COLS - plen - 1) { + move(y, plen); + for (size_t i = l - COLS + plen + 1; i < l; i++) { + addch(inp[i]); + } + } else if ((unsigned)x != plen && c == 127) { + mvaddch(y, x - 1, ' '); + move(y, x - 1); + } else if (c != 127) { + addch(c);   } - inp[l++] = c; + + refresh();   } - inp[l] = '\0';   + if (l == 0) { + free(inp); + inp = NULL; + } else inp[l] = '\0';   curs_set(0); - noecho(); - raw();     return inp;  } diff --git a/src/main.c b/src/main.c index f687897..ba2df53 100644 --- a/src/main.c +++ b/src/main.c @@ -31610 +3167 @@ int main(int argc, char ** argv) {   if (n_marked_files)   break;   char * nn = curses_getline(NULL); // new name - if (strlen(nn) < 1) { - free(nn); - break; - } + if (!nn) break;   // old path   char * op = malloc(strlen(cwd) + strlen(dir_entries[cursor - 1]->name) + 2);   // new path @@ -3896 +3868 @@ int main(int argc, char ** argv) {   break;   case ':':;   char * inp = curses_getline(":"); + if (!inp) break; +   char * sp = strchr(inp, ' ');   if (sp) {   *sp = 0; @@ -4106 +4098 @@ int main(int argc, char ** argv) {   break;   case '/':;   char * search_str = curses_getline("/"); + if (!search_str) break; +   long c = search_file(cursor, search_str);   free(search_str);   @@ -4207 +4217 @@ int main(int argc, char ** argv) {   break;   case '!':;   char * cmd = curses_getline("!"); - if (!cmd[0]) break; + if (!cmd) break;   // file name & length to sub   char * f = dir_entries[cursor - 1]->name;   size_t flen = strlen(f);