/* * * 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 DIR_R 0x01 #define DIR_U 0x02 #define DIR_D 0x04 #define DIR_L 0x08 #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 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; char cursor; char corner; 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; 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=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(); } void get_box(){ char c; int orig_x=x, orig_y=y; redraw(); 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; } check_bound(); redraw(); draw_box(orig_x, orig_y, NOFIX); status_bar(); show_cursor(); } if (c == 'b') draw_box(orig_x, orig_y, FIX); redraw(); state = MOVE; } 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){ //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 'w': write_file(); break; case '-': toggle_hline(); break; case '|': toggle_vline(); break; case '+': toggle_corner(); break; case 'Q': case 'q': cleanup(0); exit(0); break; default:; //statu("got: %d\n", c); } check_bound(); status_bar(); show_cursor(); step = 1; } } int main(int argc, char *argv[]){ init(); commands(); cleanup(0); }