diff options
author | KatolaZ <katolaz@freaknet.org> | 2019-07-26 10:26:40 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2019-07-26 10:26:40 +0100 |
commit | f0200b8cc94cd6859ee91b7b47d1d89b41b195ed (patch) | |
tree | 30412ef93952f4d777dd113edeab4779118b4043 /gramscii.c | |
parent | cbfdf55b79d29e94b1f3d263dc74ee55140bbc66 (diff) |
add auto-arrow, multipliers, -s, -h
Diffstat (limited to 'gramscii.c')
-rw-r--r-- | gramscii.c | 114 |
1 files changed, 106 insertions, 8 deletions
@@ -27,6 +27,7 @@ #include <signal.h> #include <string.h> #include <sys/ioctl.h> +#include <ctype.h> #include "config.h" @@ -80,6 +81,7 @@ int dir; int x; int y; int step; +int mult; int force_new; char cursor; char corner; @@ -101,6 +103,7 @@ char fname[256]; char visual; char silent; +char autoend; char *argv0; @@ -147,6 +150,20 @@ char* state_str(){ return "ERR"; } +char get_mark(char dir){ + switch(dir){ + case DIR_U: + return '^'; + case DIR_D: + return 'v'; + case DIR_L: + return '<'; + case DIR_R: + return '>'; + } + return '>'; +} + void status_bar(){ @@ -355,26 +372,78 @@ void handle_goto(){ show_cursor(); } -int move_around(char c){ +int get_escape(FILE *fc){ + char c[4]; + + c[0] = fgetc(fc); + if (c[0] == '['){ + c[1] = fgetc(fc); + switch(c[1]){ + case 'D': + dir = DIR_L; + x -= step; + break; + case 'B': + dir = DIR_D; + y += step; + break; + case 'A': + dir = DIR_U; + y -= step; + break; + case 'C': + dir = DIR_R; + x += step; + break; + } + return 1; + } + else{ + ungetc(c[0], fc); + return 0; + } + +} + + +int move_around(char c, FILE *fc){ + + if (isdigit(c)){ + if (mult) + mult *=10; + mult += c - '0'; + return 0; + } switch(c){ + case 27: /* control sequence? */ + c = get_escape(fc); + break; case 'H': step = LONG_STEP;/** FALLTHROUGH **/ case 'h': dir = DIR_L; + if (mult) + step *= mult; x -= step; break; case 'J': step = LONG_STEP;/** FALLTHROUGH **/ case 'j': + if (mult) + step *= mult; dir = DIR_D; y += step; break; case 'K': step = LONG_STEP;/** FALLTHROUGH **/ case 'k': + if (mult) + step *= mult; dir = DIR_U; y -= step; break; case 'L': step = LONG_STEP;/** FALLTHROUGH **/ case 'l': + if (mult) + step *= mult; dir = DIR_R; x += step; break; @@ -384,6 +453,7 @@ int move_around(char c){ default: return 0; } + mult = 0; return c; } @@ -535,7 +605,7 @@ void get_box(FILE *fc){ while((c=fgetc(fc))!=EOF && c != 27 && c!= 'b' && c != '\n'){ if (change_style(c)) goto update_box; - if (!move_around(c)) + if (!move_around(c, fc)) continue; check_bound(); redraw(); @@ -589,7 +659,12 @@ void draw_arrow(int x, int y, char *a, int a_len, int fix){ /* f(x,y,mark_end);*/ cur_dir = a[i]; } - f(x,y,mark_end); + if (autoend){ + if (cur_dir != DIR_N) + f(x,y, get_mark(cur_dir)); + } + else + f(x,y,mark_end); show_cursor(); } @@ -613,7 +688,7 @@ void get_arrow(FILE *fc){ while((c=fgetc(fc))!=EOF && c != 27 && c!= 'a' && c != '\n'){ if (change_style(c)) goto update_arrow; - if (!move_around(c)) + if (!move_around(c, fc)) continue; check_bound(); /* FIXME: if we are out of bound, do nothing? */ @@ -664,7 +739,7 @@ void delete(FILE *fc){ status_bar(); show_cursor(); while((c=fgetc(fc))!=EOF && c!=27 && c!= 'x' && c != '\n'){ - if (!move_around(c)) continue; + if (!move_around(c, fc)) continue; check_bound(); do_delete(orig_x, orig_y); step = 1; @@ -758,7 +833,7 @@ void visual_box(FILE *fc){ set_video(VIDEO_REV); draw_box(x,y,NOFIX); while((c=fgetc(fc))!=EOF && c != 27 && c!= 'v' && c != '\n'){ - if (!move_around(c)) switch(c){ + if (!move_around(c, fc)) switch(c){ case 'f':/* fill */ f = get_key(fc, "fill char: "); /** FALLTHROUGH **/ case 'x':/* erase */ @@ -842,7 +917,7 @@ void commands(FILE *fc){ char c; while((c=fgetc(fc))!=EOF){ - if (!change_style(c) && !move_around(c)){ + if (!change_style(c) && !move_around(c, fc)){ switch(c){ case 'i': state = TEXT; @@ -855,9 +930,11 @@ void commands(FILE *fc){ state = BOX; get_box(fc); break; + case 'A': autoend=1; case 'a': state = ARROW; get_arrow(fc); + autoend = 0; break; case 'W': force_new = 1;/** FALLTHROUGH **/ @@ -897,17 +974,38 @@ void commands(FILE *fc){ } +void usage(){ + fprintf(stderr, "Usage: %s [-s] [-h] [file ...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]){ + FILE *fc; ARGBEGIN { case 's': silent = 1; break; + case 'h': /* FALLTHROUGH */ + default: + usage(); } ARGEND; init(); - + while (argc){ + fc = fopen(argv[0], "r"); + if (fc == NULL){ + fprintf(stderr, "Error opening file %s\n", argv[0]); + } + else { + commands(fc); + fclose(fc); + redraw(); + } + argv++; + argc--; + } commands(stdin); cleanup(0); return 0; |