-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsoleutils.c
More file actions
53 lines (45 loc) · 1.08 KB
/
consoleutils.c
File metadata and controls
53 lines (45 loc) · 1.08 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <conio.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
void set_color(int color)
{
printf("\033[0%um", color);
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
void clear_screen()
{
system("cls");
}
#else
void clear_screen()
{
system("clear");
}
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
// Windows-specific implementation
char getch(void)
{
return _getch();
}
#else
// Unix-like implementation
char getch(void)
{
struct termios oldt, newt;
char ch;
tcgetattr(STDIN_FILENO, &oldt); // Get current terminal settings
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // Apply new settings
ch = getchar(); // Read a single character
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // Restore old terminal settings
return ch;
}
#endif