change screen management -- dynamic array of lines - gramscii - A simple editor for ASCII box-and-arrow charts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 3d53fcefe3ca3e6f25d1731c2caa0c1c0e676453
 (DIR) parent 0adbc2a776c36b736bb5acb7184ba559d9428bc4
 (HTM) Author: KatolaZ <katolaz@freaknet.org>
       Date:   Fri, 26 Jul 2019 22:55:56 +0100
       
       change screen management -- dynamic array of lines
       
       Diffstat:
         M TODO                                |       6 +++---
         M gramscii.c                          |      36 ++++++++++++++++++++++++++------
       
       2 files changed, 33 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git 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)
 (DIR) diff --git 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;
        }