/* * * gramscii: a simple editor for ASCII box-and-arrow charts * * Copyright (c) 2019 Vincenzo "KatolaZ" Nicosia * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. Please see the attached file COPYING. * Otherwise, please visit . * */ #include #include #include #include #include #include #include #include "config.h" #include "arg.h" #define MOVE 0x00 #define BOX 0x01 #define ARROW 0x02 #define TEXT 0x04 #define DEL 0x08 #define VIS 0x10 #define DIR_N 0x00 #define DIR_R 0x01 #define DIR_U 0x02 #define DIR_D 0x04 #define DIR_L 0x08 #define DIR_HOR (DIR_R | DIR_L) #define DIR_VER (DIR_D | DIR_U) #define NOFIX 0x0 #define FIX 0x1 #define BG ' ' #define PTR '+' #define UND '_' #define ARR_L '<' #define ARR_R '>' #define ARR_U '^' #define ARR_D 'v' #define HOME 0x01 #define END 0x02 #define MIDDLE 0x04 #define VIDEO_NRM 0 #define VIDEO_REV 7 #define MIN(x,y) (x) < (y) ? (x) : (y) #define MAX(x,y) (x) > (y) ? (x) : (y) /** #define DEBUG 1 **/ char **screen; int WIDTH, HEIGHT; int state; int dir; int x; int y; int step; int force_new; char cursor; char corner; int hlines_sz= sizeof(hlines) -1; int vlines_sz= sizeof(vlines) -1; int corners_sz = sizeof(corners) -1; int stmarks_sz = sizeof(st_marks) - 1; int endmarks_sz = sizeof(st_marks) - 1; int cur_hl, cur_vl, cur_corn, cur_start, cur_end; char line_h; char line_v; char mark_st; char mark_end; char modified; char fname[256]; char visual; char silent; char *argv0; struct termios t1, t2, t3; void dump_lines(){ int i; for (i=0; i=WIDTH) x = WIDTH-1; if (y<0) y=0; else if (y>=HEIGHT) y = HEIGHT -1; } void reset_styles(){ cur_corn = 0; corner = corners[0]; cur_hl = cur_vl = 0; cur_start = cur_end = 0; line_h = hlines[cur_hl]; line_v = vlines[cur_vl]; mark_st = st_marks[cur_start]; mark_end = end_marks[cur_end]; } void redraw(){ int i; if (silent) return; printf("\033[2J\033[1;1H"); for (i=0;i': toggle_end_mark(); break; case '.': reset_styles(); break; default: return 0; } return c; } /***** text, box, arrows *****/ void get_text(FILE *fc){ char c; int orig_x = x; redraw(); while((c=fgetc(fc))!=EOF && c != 27){ if(c=='\n'){ set_cur(BG); y += 1; x = orig_x; } else { set_cur(c); update_current(); modified = 1; x += 1; if (x >= WIDTH) x = orig_x; } check_bound(); status_bar(); show_cursor(); } state=MOVE; } void draw_box(int x1, int y1, int fix){ int xmin, ymin, xmax, ymax; int i; void (*f)(int, int, char); 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++){ f(i, ymin, line_h); f(i, ymax, line_h); } for(i=ymin+1; i<=ymax; i++){ f(xmin, i, line_v); f(xmax, i, line_v); } f(xmin, ymin, corner); f(xmin, ymax, corner); f(xmax, ymin, corner); f(xmax, ymax, corner); show_cursor(); } void get_box(FILE *fc){ char c; int orig_x=x, orig_y=y; redraw(); step = 1; draw_box(x,y,NOFIX); while((c=fgetc(fc))!=EOF && c != 27 && c!= 'b' && c != '\n'){ if (change_style(c)) goto update_box; if (!move_around(c)) continue; check_bound(); redraw(); step = 1; update_box: draw_box(orig_x, orig_y, NOFIX); status_bar(); show_cursor(); } if (c == 'b' || c == '\n'){ draw_box(orig_x, orig_y, FIX); modified = 1; } redraw(); state = MOVE; } void draw_arrow(int x, int y, char *a, int a_len, int fix){ int i, j, cur_dir; char line; void (*f)(int, int, char); if (fix == FIX) f = set_xy; else f = draw_xy; f(x,y,mark_st); if (!a_len){ show_cursor(); return; } cur_dir=DIR_N; for (i=0; i0) { /* If we are switching between horizontal and vertical, put a "corner" */ if (((cur_dir & DIR_HOR) && (a[i] & DIR_VER)) || ((cur_dir & DIR_VER) && (a[i] & DIR_HOR))){ f(x,y,corner); show_cursor(); } } for(j=0; j=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(FILE *fc){ char c; int orig_x = x, orig_y = y; status_bar(); show_cursor(); while((c=fgetc(fc))!=EOF && c!=27 && c!= 'x' && c != '\n'){ if (!move_around(c)) continue; check_bound(); do_delete(orig_x, orig_y); step = 1; modified = 1; orig_x = x; orig_y = y; redraw(); status_bar(); show_cursor(); } state = MOVE; } /*** File management ***/ void write_file(FILE *fc){ FILE *fout; int i; if (!fname[0] || force_new){ get_string(fc, "Write to: ", fname, 255); if ((fout=fopen(fname, "r"))!=NULL){ if (!is_yes(get_key(fc,"File exists. Overwrite [y/n]?")) ){ fclose(fout); return; } fclose(fout); } } if((fout=fopen(fname, "w"))==NULL){ get_key(fc, "Error opening file."); return; } for (i=0; i