From b38ed132a7df231fc08ce384d8559e6648fdd0cc Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Sat, 27 Jul 2019 08:06:27 +0100 Subject: first step towards crop + some fixes to erase --- TODO | 3 +- files.c | 2 +- gramscii.h | 5 ++- main.c | 3 ++ screen.c | 138 ++++++++++++++++++++++++++++++++++++++++++++----------------- 5 files changed, 110 insertions(+), 41 deletions(-) diff --git a/TODO b/TODO index d521256..59534ac 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,6 @@ + optimize redraws (redraw only the modified rectangle) ++ add crop command (C) +- fix bug with 'g' commands in arrow mode - add screen geometry option (-g 25x80?) - read file at point - read output of command (!) @@ -11,7 +13,6 @@ + parse control characters + 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 + visual selection - crop-to diff --git a/files.c b/files.c index 811a9bc..c007eac 100644 --- a/files.c +++ b/files.c @@ -53,7 +53,7 @@ void load_file(FILE *fc){ while((fgets(screen[i].s, WIDTH+2, fin)) != NULL && i= num_lines){ - tmp = realloc(screen, (_y + LONG_STEP)* sizeof(line_t)); + + if (n > num_lines){ + tmp = realloc(screen, (n + 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'; + else while ( num_lines < n + LONG_STEP){ + alloc_line(num_lines); 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)); - } +} + + +void show_cursor(){ + if (silent) + return; + printf("\033[%d;%df", y+1, x+1); + fflush(stdout); +} + + +void set_xy(int _x, int _y, char c){ + ensure_num_lines(_y + 1); + ensure_line_length(_y, _x + 1); while (screen[_y].lst<_x){ screen[_y].lst ++; screen[_y].s[screen[_y].lst] = BG; @@ -172,11 +197,9 @@ void update_current(){ fflush(stdout); } -void erase_line(char *s){ - while(*s){ - *s = BG; - s++; - } +void erase_line(int i){ + screen[i].lst = -1; + screen[i].s[0] = '\0'; } void erase_box(int x1, int y1, char c){ @@ -196,7 +219,7 @@ void erase_box(int x1, int y1, char c){ void erase_screen(){ int i; for(i=0;i *x2) + *x2 = screen[i].lst; + j = 0; + while(j <= screen[i].lst && isblank(first=screen[i].s[j])) + j++; + if (j < *x1) + *x1 = j; + } +} + +void crop_to_rect(int x1, int y1, int x2, int y2){ + int i; + + for (i=0; i<= y2-y1; i ++){ + ensure_line_length(i, screen[i+y1].lst); + sprintf(screen[i].s, "%s", screen[i+y1].s + x1); + screen[i].lst = screen[i+y1].lst - x1; + } + while (i<=y2){ + screen[i].lst = -1; + screen[i].s[0]= '\0'; + i ++; + } +} + +void crop_to_nonblank(){ + int x1, x2, y1, y2; + find_nonblank_rect(&x1, &y1, &x2, &y2); +#ifdef DEBUG + fprintf(stderr, "crop rectangle: (%d, %d)-(%d, %d)\n", x1, y1, x2, y2); +#endif + crop_to_rect(x1, y1, x2, y2); + redraw(); +} + -- cgit v1.2.3