-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.c
More file actions
109 lines (91 loc) · 2.05 KB
/
screen.c
File metadata and controls
109 lines (91 loc) · 2.05 KB
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
// in this file we are going to define a number of functions for screen
// manipulation. These functions include erase screen, set color attributes,
// set sursor location, etc.. using VT100 escape sequences.
// follow this reference: http://www.termsys.demon.co.uk/vtansi.htm
// to paste to putty: shift_insert key
#include <stdio.h>
#include "screen.h"
//function definition
void setfgcolor(int fg)
{
printf("%c[1;%dm", ESC, fg);
}
void setbgcolor(int bg)
{
printf("%c[1;%dm", ESC, bg);
}
void setcolors(int f, int b)
{
setfgcolor(f);
setbgcolor(bg(b));
}
void resetcolors(void)
{
printf("%c[0m", ESC);
}
void clearscreen(void)
{
printf("%c[2J", ESC);
}
void gotoXY(int row, int col)
{
printf("%c[%d;%dH",ESC, row, col);
}
void drawbar(int col, int height)
{
int i;
for(i=1; i<=height; i++){
gotoXY(35-i, col);
#ifdef UNICODE // following codes are in conditional compilation
printf("%s", BAR);
#else
printf("%c", '#');
#endif
}
}
void drawrect(int row, int col, int height, int width, int fg)
{
int i,j;
for(i=row;i<row+height;i++){
for(j=col; j<col+width; j++){
setfgcolor(fg);
gotoXY(i,j);
#ifdef UNICODE
printf("%s", HEART);
#else
printf("%c","#");
#endif
}
printf("\n");
}
}
Position getscreensize(void)
{
//In this function, use terminal query function to query cursor pos
//The terminal should return a string back to the program
//If a query string "ESC[6n" is issued to the terminal
Position p;
int r,c; //for decoding the report string
char re[100] = "\0"; //empty string to get report
gotoXY(999,999); //force the cursor to the bottom right corner
printf("%c[6n",ESC); //send the query string for cursor position
scanf("%s",re); //get report from terminal
#ifdef DEBUG
printf("%s\n",re);
#endif
//we will decode the returned string
#include <string.h>
if(strlen(re)>0) //in this case we will get a cursor position report
{
char dum; //dummy char to consume those symbols
sscanf(re,"%c%c%d%c%d%c", &dum,&dum,&r,&dum,&c,&dum);
p.row=r;
p.col=c;
}
else
{
p.row=0;
p.col=0;
}
return p;
}