/* * * This is `gramscii`, a simple tool to create ascii box graphs * */ #include #include #include #include #include #include #define MOVE 0x00 #define BOX 0x01 #define ARROW 0x02 #define TEXT 0x04 #define DEL 0x08 #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 WIDTH 100 #define HEIGHT 25 #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 MIN(x,y) (x) < (y) ? (x) : (y) #define MAX(x,y) (x) > (y) ? (x) : (y) char screen[HEIGHT][WIDTH+1]; int state; int dir; int x; int y; int step; int force_new; char cursor; char corner; char hlines[] = {"-~=#@._ "}; char vlines[] = {"|H#@:;i "}; char corners[] = {"+'H#@.\""}; char st_marks[] = {"+o-|<>^v"}; char end_marks[] = {">+o-|<^v"}; 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]; struct termios t1, t2, t3; void cleanup(int s){ printf("\033[;H\033[2J"); tcsetattr(0, TCSANOW, &t1); exit(0); } void show_cursor(){ printf("\033[%d;%df", y+1, x+1); } void set(char c){ screen[y][x] = c; } void set_xy(int x, int y, char c){ /* FIXME: check if x and y are valid!!!! */ screen[y][x] = c; } void draw_xy(int x, int y, char c){ /* FIXME: check if x and y are valid!!!! */ printf("\033[%d;%df",y+1,x+1); putchar(c); } void init_screen(){ int i; for(i=0; i: %c %10s", x, y, state_str(), line_h, line_v, corner, mark_st, mark_end, ""); printf(" [%s]", fname ); printf("\033[0m"); } void redraw(){ int i; printf("\033[2J\033[1;1H"); for (i=0;i=WIDTH) x = WIDTH-1; if (y<0) y=0; else if (y>=HEIGHT) y = HEIGHT -1; } /***** text, box, arrows *****/ void get_text(){ char c; int orig_x = x; redraw(); while((c=getchar())!=EOF && c != 27){ if(c=='\n'){ set(BG); y += 1; x = orig_x; } else { set(c); update_current(); 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(); } 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; } void get_box(){ char c; int orig_x=x, orig_y=y; redraw(); step = 1; draw_box(x,y,NOFIX); while((c=getchar())!=EOF && c != 27 && c!= 'b'){ if (move_around(c)) continue; check_bound(); redraw(); step = 1; draw_box(orig_x, orig_y, NOFIX); status_bar(); show_cursor(); } if (c == 'b'){ draw_box(orig_x, orig_y, FIX); modified = 1; } redraw(); state = MOVE; } int progr_x(int dir){ return dir == DIR_L ? -1 : dir == DIR_R ? 1: 0; } int progr_y(int dir){ return dir == DIR_U ? -1 : dir == DIR_D ? 1: 0; } 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(){ 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); modified = 1; orig_x = x; orig_y = y; redraw(); status_bar(); show_cursor(); } state = MOVE; } void check_modified(){ if (modified){ if (!is_yes(get_key("Unsaved changes. Write to file [y/n]?")) ){ return; } write_file(0); } } void commands(){ char c; while((c=getchar())!=EOF){ //screen[y][x]=BG; 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; case 'i': state = TEXT; get_text(); break; case 'r': redraw(); break; case 'b': state = BOX; get_box(); break; case 'a': state = ARROW; get_arrow(); break; case 'W': force_new = 1; case 'w': write_file(); break; case 'g': go_to(HOME); break; case 'G': go_to(END); break; case 'm': go_to(MIDDLE); break; case 'x': state = DEL; delete(); break; case '-': toggle_hline(); break; case '|': toggle_vline(); break; case '+': toggle_corner(); break; case '<': toggle_st_mark(); break; case '>': toggle_end_mark(); break; case 'q': check_modified(); case 'Q': cleanup(0); exit(0); break; default:; //statu("got: %d\n", c); } check_bound(); status_bar(); show_cursor(); step = 1; force_new = 0; } } int main(int argc, char *argv[]){ init(); commands(); cleanup(0); }