From 575703d810415c9f427104ab0225a1e5916983ec Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Fri, 19 Jul 2019 19:23:38 +0100 Subject: delete and default write file --- TODO | 3 +- gramscii.c | 143 ++++++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 92 insertions(+), 54 deletions(-) diff --git a/TODO b/TODO index b4d36cd..feac502 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ + optimize redraws (i.e., avoid to redraw if possible) - (?) change cursor shape according to action - auto-arrow 'A' (automatic end-char) -- delete -- 'x' or 'd' - load from file - read file at point - visual selection @@ -14,6 +13,8 @@ (also do not print unmanaged chars!) - get screen geometry - allow scrolling (both vertical and horizontal) +* write to new file / default file +* delete -- 'x' * save to file * implement arrow * set different line styles (done for hl, vl, corner) diff --git a/gramscii.c b/gramscii.c index 6011842..0d94d4a 100644 --- a/gramscii.c +++ b/gramscii.c @@ -15,7 +15,7 @@ #define BOX 0x01 #define ARROW 0x02 #define TEXT 0x04 - +#define DEL 0x08 #define DIR_N 0x00 #define DIR_R 0x01 @@ -55,6 +55,7 @@ int dir; int x; int y; int step; +int force_new; char cursor; char corner; char hlines[] = {"-~=#@._ "}; @@ -75,6 +76,9 @@ char line_v; char mark_st; char mark_end; +char modified; +char fname[256]; + struct termios t1, t2, t3; @@ -130,6 +134,8 @@ char* state_str(){ return "box"; case ARROW: return "arr"; + case DEL: + return "del"; default: return "ERR"; } @@ -178,6 +184,8 @@ void init(){ init_screen(); x = WIDTH/2; y = HEIGHT/2; + modified = 0; + fname[0] = '\0'; redraw(); } @@ -249,10 +257,33 @@ void draw_box(int x1, int y1, int fix){ } -void move_around(char c){ - - +int move_around(char c){ + switch(c){ + case 'H': step = 5; + case 'h': + dir = DIR_L; + x -= step; + break; + case 'J': step = 5; + case 'j': + dir = DIR_D; + y += step; + break; + case 'K': step = 5; + case 'k': + dir = DIR_U; + y -= step; + break; + case 'L': step = 5; + case 'l': + dir = DIR_R; + x += step; + break; + default: + return 1; + } + return 0; } @@ -263,24 +294,7 @@ void get_box(){ step = 1; draw_box(x,y,NOFIX); while((c=getchar())!=EOF && c != 27 && c!= 'b'){ - switch(c){ - case 'H': step = 5; - case 'h': - x -= step; - break; - case 'J': step = 5; - case 'j': - y += step; - break; - case 'K': step = 5; - case 'k': - y -= step; - break; - case 'L': step = 5; - case 'l': - x += step; - break; - } + if (move_around(c)) continue; check_bound(); redraw(); step = 1; @@ -363,30 +377,7 @@ void get_arrow(){ step = 1; draw_arrow(x,y, arrow, 0, NOFIX); while((c=getchar())!=EOF && c != 27 && c!= 'a'){ - switch(c){ - case 'H': step = 5; - case 'h': - dir = DIR_L; - x -= step; - break; - case 'J': step = 5; - case 'j': - dir = DIR_D; - y += step; - break; - case 'K': step = 5; - case 'k': - dir = DIR_U; - y -= step; - break; - case 'L': step = 5; - case 'l': - dir = DIR_R; - x += step; - break; - default: - continue; - } + if (move_around(c)) continue; check_bound(); /* FIXME: if we are out of bound, do nothing? */ if (arrow_len == arrow_sz){ @@ -438,17 +429,18 @@ int is_yes(char c){ } void write_file(){ - char fname[256]; FILE *f; int i; - - get_string(fname, 255); - if (f=fopen(fname, "r")){ - if (!is_yes(get_key("File exists. Overwrite [y/N]?")) ){ + + if (!fname[0] || force_new){ + get_string(fname, 255); + if (f=fopen(fname, "r")){ + if (!is_yes(get_key("File exists. Overwrite [y/N]?")) ){ + fclose(f); + return; + } fclose(f); - return; } - fclose(f); } if((f=fopen(fname, "w"))==NULL){ get_key("Error opening file."); @@ -512,6 +504,44 @@ void go_to(int where){ show_cursor(); } +void do_delete(int x1, int y1){ + int i; + switch(dir){ + case DIR_R: + for(i=x1; i<=x; i++) set_xy(i,y,BG); + break; + case DIR_L: + for(i=x1; i>=x; i--) set_xy(i,y,BG); + break; + case DIR_U: + for(i=y1; i>=y; i--) set_xy(x,i,BG); + break; + case DIR_D: + for(i=y1; i<=y; i++) set_xy(x,i,BG); + break; + } +} + + +void delete(){ + char c; + int orig_x = x, orig_y = y; + status_bar(); + show_cursor(); + while((c=getchar())!=EOF && c!=27 && c!= 'x'){ + if (move_around(c)) continue; + check_bound(); + step = 1; + do_delete(orig_x, orig_y); + orig_x = x; + orig_y = y; + redraw(); + status_bar(); + show_cursor(); + } + state = MOVE; +} + void commands(){ @@ -554,6 +584,8 @@ void commands(){ state = ARROW; get_arrow(); break; + case 'W': + force_new = 1; case 'w': write_file(); break; @@ -566,6 +598,10 @@ void commands(){ case 'm': go_to(MIDDLE); break; + case 'x': + state = DEL; + delete(); + break; case '-': toggle_hline(); break; @@ -593,6 +629,7 @@ void commands(){ status_bar(); show_cursor(); step = 1; + force_new = 0; } } -- cgit v1.2.3