-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.c
72 lines (56 loc) · 1.12 KB
/
terminal.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
#include "terminal.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "colors.h"
struct termios oldt, newt;
static int flags;
void init_terminal(terminal_t *terminal) {
terminal->configure = terminal_configure;
terminal->restore = terminal_restore;
}
void refresh_screen()
{
fflush(stdout);
}
void clear_screen()
{
printf("\033[2J");
}
void set_cursor_position(int row, int col)
{
printf("\033[%d;%dH", col, row);
}
char *color(int bg, int fg)
{
char *text = malloc(100);
sprintf(text, "\033[%d;%dm", fg, bg + 10);
return text;
}
void play_bell()
{
printf("%c", '\a');
}
void make_block()
{
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
}
void make_unblock()
{
fcntl(STDIN_FILENO, F_SETFL, flags & ~O_NONBLOCK);
}
void terminal_configure()
{
flags = fcntl(STDIN_FILENO, F_GETFL, 0);
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}
void terminal_restore()
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
clear_screen();
}