1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/*
*
* This is `gramscii`, a simple tool to create ascii box graphs
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#define MOVE 0x00
#define BOX 0x01
#define ARROW 0x02
#define TEXT 0x04
#define DIR_N 0x00
#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) -1;
int vlines_sz= sizeof(vlines) -1;
int corners_sz = sizeof(corners) -1;
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<HEIGHT; i++){
memset(screen[i], ' ', WIDTH);
screen[i][WIDTH]='\0';
}
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 "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: %3d y: %3d -- mode: %4s hl: %c vl: %c cn: %c %10s",
x, y, state_str(), line_h, line_v, corner, "");
printf("\033[0m");
}
void redraw(){
int i;
printf("\033[2J\033[1;1H");
for (i=0;i<HEIGHT;i++){
fprintf(stdout,"%s\n",screen[i]);
}
status_bar();
show_cursor();
}
void update_current(){
printf("\033[%d'%df",y+1,x+1);
putchar(screen[y][x]);
}
void init(){
signal(SIGHUP, cleanup);
signal(SIGINT, cleanup);
signal(SIGTERM, cleanup);
signal(SIGQUIT, cleanup);
tcgetattr(0, &t1);
t2 = t1;
t2.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &t2);
init_screen();
x = WIDTH/2;
y = HEIGHT/2;
redraw();
}
void check_bound(){
if (x<0) x=0;
else if (x>=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();
step = 1;
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();
step = 1;
draw_box(orig_x, orig_y, NOFIX);
status_bar();
show_cursor();
}
if (c == 'b')
draw_box(orig_x, orig_y, FIX);
redraw();
state = MOVE;
}
int progr_x(int dir){
switch(dir){
case DIR_L:
return -1;
case DIR_R:
return +1;
default:
return 0;
}
return 0;
}
int progr_y(int dir){
switch(dir){
case DIR_D:
return +1;
case DIR_U:
return -1;
default:
return 0;
}
return 0;
}
/* FIXME: fix pointer position */
/* FIXME: set initial and final markers */
/* FIXME: draw "corner" as first char after change of dir */
void draw_arrow(int x, int y, char *a, int a_len, int fix){
int i, j;
char line;
void (*f)(int, int, char);
if (fix == FIX)
f = set_xy;
else
f = draw_xy;
if (!a_len){
f(x,y,corner);
return;
}
line = (a[0] & DIR_L) || (a[0] & DIR_R) ? line_h : line_v;
f(x,y,line);
for (i=0; i<a_len; i+=2){
for(j=0; j<a[i+1]; j++){
line = (a[i] & DIR_L) || (a[i] & DIR_R) ? line_h : line_v;
x += progr_x(a[i]);
y += progr_y(a[i]);
f(x, y, line);
}
}
show_cursor();
}
void get_arrow(){
char c;
int orig_x=x, orig_y=y, arrow_len;
static char *arrow = NULL;
static int arrow_sz;
if (!arrow){
arrow_sz = 100;
arrow = malloc(arrow_sz * sizeof(char));
}
arrow_len = 0;
dir = DIR_N;
redraw();
step = 1;
//draw_arrow(x,y,NOFIX);
while((c=getchar())!=EOF && c != 27 && c!= 'a'){
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:
continue;
}
check_bound();
/* FIXME: if we are out of bound, do nothing? */
if (arrow_len == arrow_sz){
arrow_sz *=2;
arrow = realloc(arrow, arrow_sz * sizeof(char));
}
arrow[arrow_len++] = dir;
arrow[arrow_len++] = step;
redraw();
step = 1;
draw_arrow(orig_x, orig_y, arrow, arrow_len, NOFIX);
status_bar();
show_cursor();
}
if (c == 'a')
draw_arrow(orig_x, orig_y, arrow, arrow_len, 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 'a':
state = ARROW;
get_arrow();
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);
}
|