summaryrefslogtreecommitdiff
path: root/gramscii.c
blob: 40d1f1e0c9efd6080000889184997fa34c55cb25 (plain)
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
/*
*
* 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_R  0x01
#define DIR_U  0x02
#define DIR_D  0x04
#define DIR_L  0x08


#define WIDTH  100
#define HEIGHT  25

#define BG        ' '
#define PTR       '+'
#define UND       '_'
#define LINE_H    '-'
#define LINE_V    '|'
#define DBLINE_H  '='
#define DBLINE_V  'u'
#define BLDLINE   '#'
#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 box_line_h;
char box_line_v;
char arrow_line_h;
char arrow_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);
	//putchar(screen[y][x]);
	//printf("\033[%d;%df", y+1, x+2);
}

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 clear(){
	screen[y][x] = BG;
}

void init_screen(){
	int i;
	for(i=0; i<HEIGHT; i++){
		memset(screen[i], ' ', WIDTH);
		screen[i][WIDTH]='\0';
	}
	cursor = PTR;
	corner = PTR;
	box_line_h = LINE_H;
	box_line_v = LINE_V;
	arrow_line_h = LINE_H;	
	arrow_line_v = LINE_V;	
}

char* state_str(){
	switch(state){
		case MOVE:
			return "mv ";
		case TEXT:
			return "txt";
		case BOX:
			return "box";
		case ARROW:
			return "arr";
	}
}


void status_bar(){
	
	printf("\033[%d;1f\033[7m", HEIGHT+1);
	printf(" x: %d y: %d -- mode: %s", x, y, state_str());
	printf("\033[0m");
}

int 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();
	step = 1;
}

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; 
}


void get_text(){
	char c;
	int orig_x = x, orig_y = y;
	
	//cursor = UND;
	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();
	}
	cursor = PTR;
	state=MOVE;
}


void fix_box(int x1, int y1){

	int xmin, ymin, xmax, ymax;
	int i;
	
	xmin = MIN(x, x1);
	xmax = MAX(x, x1);
	ymin = MIN(y, y1);
	ymax = MAX(y, y1);

	
	for(i=xmin+1; i<=xmax; i++){
		set_xy(i, ymin, box_line_h);
		set_xy(i, ymax, box_line_h);
	}
	for(i=ymin+1; i<=ymax; i++){
		set_xy(xmin, i, box_line_v);
		set_xy(xmax, i, box_line_v);
	}
	set_xy(xmin, ymin, corner);
	set_xy(xmin, ymax, corner);
	set_xy(xmax, ymin, corner);
	set_xy(xmax, ymax, corner);
}

void draw_box(int x1, int y1){

	int xmin, ymin, xmax, ymax;
	int i;
	
	xmin = MIN(x, x1);
	xmax = MAX(x, x1);
	ymin = MIN(y, y1);
	ymax = MAX(y, y1);

	
	for(i=xmin+1; i<=xmax; i++){
		draw_xy(i, ymin, box_line_h);
		draw_xy(i, ymax, box_line_h);
	}
	for(i=ymin+1; i<=ymax; i++){
		draw_xy(xmin, i, box_line_v);
		draw_xy(xmax, i, box_line_v);
	}
	draw_xy(xmin, ymin, corner);
	draw_xy(xmin, ymax, corner);
	draw_xy(xmax, ymin, corner);
	draw_xy(xmax, ymax, corner);
	show_cursor();
}


void get_box(){
	char c;
	int orig_x=x, orig_y=y;

	set(PTR);
	redraw();
	while((c=getchar())!=EOF && c != 27){
		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);
		status_bar();
		show_cursor();
	}
	fix_box(orig_x, orig_y);
	redraw();
}


int 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':
				get_box();
				break;
			case 'Q':
			case 'q':
				cleanup(0);
				exit(0);
				break;
			default:;
				//statu("got: %d\n", c);
		}
		check_bound();
		//update();
		//redraw();
		status_bar();
		show_cursor();
		step = 1;
	}

}




int main(int argc, char *argv[]){

	init();

	commands();
	cleanup(0);
}