different line styles - gramscii - A simple editor for ASCII box-and-arrow charts DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 5dd00eaf46bd250099c368c7b4a66e0e4dfc4b6f DIR parent 478c03adcdc7d76595a311a0dba682917ab97d62 HTML Author: KatolaZ <katolaz@freaknet.org> Date: Thu, 18 Jul 2019 18:46:58 +0100 different line styles Diffstat: M TODO | 13 +++++++++---- M gramscii.c | 158 +++++++++++++++++-------------- 2 files changed, 98 insertions(+), 73 deletions(-) --- DIR diff --git a/TODO b/TODO @@ -1,9 +1,14 @@ + optimize redraws (i.e., avoid to redraw if possible) -- change cursor shape according to action -+ implement box +- (?) change cursor shape according to action +- save to file - implement arrow -- set different box styles -+ add status bar +- manage special chars (DEL/CANC) during text insert + (also do not print unmanaged chars!) +- load from file +- insert file at position - get screen geometry - allow scrolling (both vertical and horizontal) +* set different line styles (done for hl, vl, corner) +* add status bar +* implement box DIR diff --git a/gramscii.c b/gramscii.c @@ -26,14 +26,12 @@ #define WIDTH 100 #define HEIGHT 25 +#define NOFIX 0x0 +#define FIX 0x1 + #define BG ' ' #define PTR '+' #define UND '_' -#define LINE_H '-' -#define LINE_V '|' -#define DBLINE_H '=' -#define DBLINE_V 'u' -#define BLDLINE '#' #define ARR_L '<' #define ARR_R '>' #define ARR_U '^' @@ -52,10 +50,15 @@ int y; int step; char cursor; char corner; -char box_line_h; -char box_line_v; -char arrow_line_h; -char arrow_line_v; +char hlines[] = {"-~=#@._"}; +char vlines[] = {"|H#@:;i"}; +char corners[] = {"+'H#@.\""}; +int hlines_sz= sizeof(hlines); +int vlines_sz= sizeof(vlines); +int corners_sz = sizeof(corners); +int cur_hl, cur_vl, cur_corn; +char line_h; +char line_v; struct termios t1, t2; @@ -69,8 +72,6 @@ void cleanup(int s){ void show_cursor(){ printf("\033[%d;%df", y+1, x+1); - //putchar(screen[y][x]); - //printf("\033[%d;%df", y+1, x+2); } void set(char c){ @@ -88,46 +89,46 @@ void draw_xy(int x, int y, char c){ putchar(c); } -void clear(){ - screen[y][x] = BG; -} - void init_screen(){ int i; for(i=0; i<HEIGHT; i++){ memset(screen[i], ' ', WIDTH); screen[i][WIDTH]='\0'; } - cursor = PTR; - corner = PTR; - box_line_h = LINE_H; - box_line_v = LINE_V; - arrow_line_h = LINE_H; - arrow_line_v = LINE_V; + cur_corn = 0; + corner = corners[0]; + cur_hl = cur_vl = 0; + line_h = hlines[cur_hl]; + line_v = vlines[cur_vl]; } char* state_str(){ switch(state){ case MOVE: - return "mv "; + return "mov"; case TEXT: return "txt"; case BOX: return "box"; case ARROW: return "arr"; + default: + return "ERR"; } + return "ERR"; } void status_bar(){ printf("\033[%d;1f\033[7m", HEIGHT+1); - printf(" x: %d y: %d -- mode: %s", x, y, state_str()); + printf(" x: %3d y: %3d -- mode: %4s hl: %c vl: %c cn: %c %10s", + x, y, state_str(), line_h, line_v, corner, ""); + printf("\033[0m"); } -int redraw(){ +void redraw(){ int i; printf("\033[2J\033[1;1H"); @@ -171,11 +172,12 @@ void check_bound(){ } +/***** text, box, arrows *****/ + void get_text(){ char c; - int orig_x = x, orig_y = y; + int orig_x = x; - //cursor = UND; redraw(); while((c=getchar())!=EOF && c != 27){ if(c=='\n'){ @@ -194,59 +196,38 @@ void get_text(){ status_bar(); show_cursor(); } - cursor = PTR; state=MOVE; } -void fix_box(int x1, int y1){ +void draw_box(int x1, int y1, int fix){ int xmin, ymin, xmax, ymax; int i; - - xmin = MIN(x, x1); - xmax = MAX(x, x1); - ymin = MIN(y, y1); - ymax = MAX(y, y1); + void (*f)(int, int, char); - - for(i=xmin+1; i<=xmax; i++){ - set_xy(i, ymin, box_line_h); - set_xy(i, ymax, box_line_h); - } - for(i=ymin+1; i<=ymax; i++){ - set_xy(xmin, i, box_line_v); - set_xy(xmax, i, box_line_v); - } - set_xy(xmin, ymin, corner); - set_xy(xmin, ymax, corner); - set_xy(xmax, ymin, corner); - set_xy(xmax, ymax, corner); -} - -void draw_box(int x1, int y1){ - - int xmin, ymin, xmax, ymax; - int i; + if (fix == FIX) + f = set_xy; + else + f = draw_xy; xmin = MIN(x, x1); xmax = MAX(x, x1); ymin = MIN(y, y1); ymax = MAX(y, y1); - for(i=xmin+1; i<=xmax; i++){ - draw_xy(i, ymin, box_line_h); - draw_xy(i, ymax, box_line_h); + f(i, ymin, line_h); + f(i, ymax, line_h); } for(i=ymin+1; i<=ymax; i++){ - draw_xy(xmin, i, box_line_v); - draw_xy(xmax, i, box_line_v); + f(xmin, i, line_v); + f(xmax, i, line_v); } - draw_xy(xmin, ymin, corner); - draw_xy(xmin, ymax, corner); - draw_xy(xmax, ymin, corner); - draw_xy(xmax, ymax, corner); + f(xmin, ymin, corner); + f(xmin, ymax, corner); + f(xmax, ymin, corner); + f(xmax, ymax, corner); show_cursor(); } @@ -254,10 +235,10 @@ void draw_box(int x1, int y1){ void get_box(){ char c; int orig_x=x, orig_y=y; - - set(PTR); + redraw(); - while((c=getchar())!=EOF && c != 27){ + draw_box(x,y,NOFIX); + while((c=getchar())!=EOF && c != 27 && c!= 'b'){ switch(c){ case 'H': step = 5; case 'h': @@ -278,16 +259,44 @@ void get_box(){ } check_bound(); redraw(); - draw_box(orig_x, orig_y); + draw_box(orig_x, orig_y, NOFIX); status_bar(); show_cursor(); } - fix_box(orig_x, orig_y); + if (c == 'b') + draw_box(orig_x, orig_y, FIX); redraw(); + state = MOVE; } -int commands(){ +void write_file(){ + char fname[256]; + FILE *f; +} + +void toggle_hline(){ + + cur_hl = (cur_hl + 1) % hlines_sz; + line_h = hlines[cur_hl]; + +} + +void toggle_corner(){ + + cur_corn = (cur_corn + 1 ) % corners_sz; + corner = corners[cur_corn]; + +} + +void toggle_vline(){ + + cur_vl = (cur_vl + 1) % vlines_sz; + line_v = vlines[cur_vl]; + +} + +void commands(){ char c; while((c=getchar())!=EOF){ @@ -321,8 +330,21 @@ int commands(){ redraw(); break; case 'b': + state = BOX; get_box(); break; + case 'w': + write_file(); + break; + case '-': + toggle_hline(); + break; + case '|': + toggle_vline(); + break; + case '+': + toggle_corner(); + break; case 'Q': case 'q': cleanup(0); @@ -332,8 +354,6 @@ int commands(){ //statu("got: %d\n", c); } check_bound(); - //update(); - //redraw(); status_bar(); show_cursor(); step = 1;