From 0adbc2a776c36b736bb5acb7184ba559d9428bc4 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Fri, 26 Jul 2019 22:13:33 +0100 Subject: towards dynamic screen management --- Makefile | 5 +++-- TODO | 3 +-- gramscii.c | 67 +++++++++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index a59ead3..55959ce 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,9 @@ options: @echo "-+-+-+-+-+-+-+-+-+-+-" -gramscii: ${INC} - +gramscii: ${SRC} ${INC} + $(CC) $(CFLAGS) -o gramscii ${SRC} + clean: @echo cleaning @rm -f $(SRC:.c=) diff --git a/TODO b/TODO index f6087c7..7b324b8 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,5 @@ + optimize redraws (redraw only the modified rectangle) -- change screen management (i.e., dynamic array of lines) ++ change screen management (i.e., dynamic array of lines) - add screen geometry option (-g 25x80?) - read file at point - read output of command (!) @@ -48,4 +48,3 @@ * set different line styles (done for hl, vl, corner) * add status bar * implement box - diff --git a/gramscii.c b/gramscii.c index 3bed08b..8a89fc9 100644 --- a/gramscii.c +++ b/gramscii.c @@ -33,6 +33,13 @@ #include "arg.h" +typedef struct{ + int sz; + int lst; + char *s; +} line_t; + + #define MOVE 0x00 #define BOX 0x01 #define ARROW 0x02 @@ -71,9 +78,10 @@ #define MIN(x,y) (x) < (y) ? (x) : (y) #define MAX(x,y) (x) > (y) ? (x) : (y) -/** #define DEBUG 1 **/ +#define DEBUG 1 -char **screen; +line_t *screen; +int num_lines; int WIDTH, HEIGHT; int state; @@ -113,7 +121,7 @@ struct termios t1, t2, t3; void dump_lines(){ int i; for (i=0; i Date: Fri, 26 Jul 2019 22:55:56 +0100 Subject: change screen management -- dynamic array of lines --- TODO | 6 +++--- gramscii.c | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index 7b324b8..222bad1 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ + optimize redraws (redraw only the modified rectangle) -+ change screen management (i.e., dynamic array of lines) - add screen geometry option (-g 25x80?) - read file at point - read output of command (!) @@ -10,7 +9,7 @@ + parse arrows (text-mode will allow movements as well) - (?) implement CTRL+G as abort (aside ESC) - add crop command (c) -- remove extra blanks until EOL when saving to file +- (?) remove extra blanks until EOL when saving to file + visual selection - crop-to - yank/put @@ -20,8 +19,9 @@ - manage special chars (DEL/CANC) during text insert (also do not print unmanaged chars!) - allow scrolling (both vertical and horizontal) -- catch SIGWINCH and react appropriately (after scroll is +- catch SIGWINCH and react appropriately (after scrolling is enabled) +* change screen management (i.e., dynamic array of lines) * add action multiplier (e.g., "7h" moves left by 7 cols) * add scripting mode option ("-s"?) * auto-arrow 'A' (automatic end-char) diff --git a/gramscii.c b/gramscii.c index 8a89fc9..4342da9 100644 --- a/gramscii.c +++ b/gramscii.c @@ -136,6 +136,11 @@ void cleanup(int s){ exit(0); } +void exit_cleanup(void){ + cleanup(0); +} + + /*** Status bar ***/ char* state_str(){ @@ -201,6 +206,7 @@ char get_key(FILE *fc, char *msg){ printf("%*s", WIDTH, ""); printf("\033[%d;1f\033[7m", HEIGHT+1); printf("%s", msg); + fflush(stdout); printf("\033[0m"); fflush(stdout); return fgetc(fc); @@ -242,14 +248,33 @@ void show_cursor(){ void set_xy(int _x, int _y, char c){ - /* FIXME: check if x and y are valid!!!! */ + line_t *tmp; + if (_y >= num_lines){ + tmp = realloc(screen, (_y + LONG_STEP)* sizeof(line_t)); + if (tmp == NULL){ + fprintf(stderr, "Unable to allocate memory for more lines"); + exit(1); + } + else while ( num_lines < _y + LONG_STEP){ + screen[num_lines].sz = WIDTH+1; + screen[num_lines].s = malloc((screen[num_lines].sz) * sizeof(char)); + if (screen[num_lines].s == NULL){ + perror("allocating screen[num_lines].s"); + exit(1); + } + memset(screen[num_lines].s, BG, screen[num_lines].sz); + screen[num_lines].lst = 0; + screen[num_lines].s[screen[num_lines].lst+1]='\0'; + num_lines ++; + } + } if (screen[_y].sz < _x + 2){ screen[_y].sz = (_x +2) * 2; screen[_y].s = realloc(screen[_y].s, screen[_y].sz * sizeof(char)); } while (screen[_y].lst<_x){ + screen[_y].lst ++; screen[_y].s[screen[_y].lst] = BG; - screen[_y].lst += 1; } screen[_y].s[_x] = c; if (_x == screen[_y].lst) @@ -806,7 +831,7 @@ void check_modified(FILE *fc){ if (!is_yes(get_key(fc, "Unsaved changes. Write to file [y/n]?")) ){ return; } - write_file(0); + write_file(fc); } } @@ -905,7 +930,7 @@ void init_screen(){ exit(1); } memset(screen[i].s, BG, screen[i].sz); - screen[i].lst = WIDTH; + screen[i].lst = 0; screen[i].s[screen[i].lst+1]='\0'; } reset_styles(); @@ -917,6 +942,7 @@ void init(){ signal(SIGINT, cleanup); signal(SIGTERM, cleanup); signal(SIGQUIT, cleanup); + atexit(exit_cleanup); tcgetattr(0, &t1); t2 = t1; @@ -981,7 +1007,6 @@ void commands(FILE *fc){ case 'q': check_modified(fc);/** FALLTHROUGH **/ case 'Q': - cleanup(0); exit(0); break; } @@ -1028,6 +1053,5 @@ int main(int argc, char *argv[]){ argc--; } commands(stdin); - cleanup(0); return 0; } -- cgit v1.2.3