-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvt100.c
93 lines (76 loc) · 1.38 KB
/
vt100.c
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
//vt100 ctl sequences
#define _P(s) write(1,s,sizeof(s))
// clear screen
void cls(){
_P("\e[2J");
}
// clear line
void cll(){
_P("\e[2K");
}
// clear line, from cursor right
void cllcright(){
_P("\e[0K");
}
// clear line, from cursor left
void cllcleft(){
_P("\e[1K");
}
// save cursor position
void csave(){
_P("\e7");
}
// restore cursor position
void crest(){
_P("\e8");
}
void home(){
_P("\e[H");
}
void cgoto(int col, int row){
char buf[32];
int l = snprintf(buf,32,"\e[%d;%dH",col,row);
write(1,buf,l);
}
#define __CWRITE(s) write(1,"\e[" s, sizeof("\e[" s ) )
// macros. this spares the sprintf, but needs constants for num
#define CUP(num) __CWRITE( #num "A" );
#define CDOWN(num) __CWRITE( #num "B" );
#define CRIGHT(num) __CWRITE( #num "C" );
#define CLEFT(num) __CWRITE( #num "D" );
#define CHOME() __CWRITE( "H" );
#define CLS() __CWRITE( "2J" );
// if sprintf isn't defined.
char* uitos(char *buf, uint i){
int a=1;
while ( a*10 <= i )
a*=10;
do{
int r = ( i/a );
*buf = r+'0';
buf++;
if ( a==1 )
break;
i -= a*r;
a/=10;
} while ( 1 );
*buf = 0;
return(buf);
}
void right(uint cols){
char buf[16];
int l = snprintf(buf,16,"\e[%uC",cols);
write(1,buf,l);
}
void left(uint cols){
char buf[16];// = { '\033', '[',
int l = snprintf(buf,16,"\033[%dD",cols);
write(1,buf,l);
}
void up(){
_P("\e[1A");
}
void down(){
_P("\e[1B");
}
#undef _P