From 3d53fcefe3ca3e6f25d1731c2caa0c1c0e676453 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Fri, 26 Jul 2019 22:55:56 +0100 Subject: change screen management -- dynamic array of lines --- gramscii.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'gramscii.c') 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