-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.c
117 lines (88 loc) · 2.09 KB
/
common.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* =====================================================================================
*
* Filename: common.c
*
* Description:
*
* Version: 1.0
* Created: 2014/07/23
* Revision: none
* Compiler: gcc
*
* Author: Occam's Razor
* Organization:
*
* =====================================================================================
*/
#include "common.h"
#define CSI "\e["
int str_ends_with(const char * str, const char * suffix) {
if(str == NULL || suffix == NULL)
return 0;
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
if(suffix_len > str_len)
return 0;
return 0 == strncmp(str + str_len - suffix_len, suffix, suffix_len);
}
void cls(void)
{
printf("\e[1;1H\e[2J");
}
int getch(void)
{
int c = 0;
struct termios org_opts, new_opts;
int res = 0;
//----- store old settings -----------
res = tcgetattr(STDIN_FILENO, &org_opts);
assert(res == 0);
//---- set new terminal parms --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c = getchar();
//------ restore old settings ---------
res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res == 0);
return c;
}
int hide_cursor(void)
{
fputs(CSI "?25l", stdout);
return 0;
}
int show_cursor(void)
{
fputs(CSI "?25h", stdout);
return 0;
}
/*
void printf_red(char *str)
{
fprintf(stdout,KRED "%s" RESET, str);
}
void printf_blue(char *str)
{
fprintf(stdout,KBLU "%s" RESET, str);
}
*/
void textcolor(char *text, char* color) {
fprintf(stdout, "%s%s" COLOR_RESET, color, text);
}
void backgroud_color(char* color) {
fprintf(stdout, "%s", color);
}
void color_reset() {
fprintf(stdout, "%s", COLOR_RESET);
}
void set_cursor_point(int x, int y) {
fprintf(stdout, "\033[%d;%dH", y, x);
}
void save_cursor() {
fprintf(stdout, CURSOR_SAVE);
}
void load_cursor() {
fprintf(stdout, CURSOR_LOAD);
}