Skip to content

Commit fe21096

Browse files
Update input.c
1 parent 873fbef commit fe21096

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

code/logic/input.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,115 @@ void fossil_io_trim(char *str) {
5050
}
5151
}
5252

53+
char fossil_io_getch(void) {
54+
#ifdef __WIN32
55+
DWORD mode;
56+
HANDLE hCon = GetStdHandle(STD_INPUT_HANDLE);
57+
GetConsoleMode(hCon, &mode);
58+
SetConsoleMode(hCon, mode & ~ENABLE_ECHO_INPUT);
59+
char ch = _getch();
60+
SetConsoleMode(hCon, mode);
61+
return ch;
62+
#else
63+
struct termios oldt, newt;
64+
char ch;
65+
tcgetattr(STDIN_FILENO, &oldt);
66+
newt = oldt;
67+
newt.c_lflag &= ~(ICANON | ECHO); // Disable echo and canonical mode
68+
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
69+
ch = getchar();
70+
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // Restore terminal settings
71+
return ch;
72+
#endif
73+
}
74+
75+
int fossil_io_read_password(char *buffer, size_t size) {
76+
if (buffer == NULL || size == 0) {
77+
return 0;
78+
}
79+
80+
size_t index = 0;
81+
char ch;
82+
while (index < size - 1) {
83+
ch = fossil_io_getch(); // Get a single character without echo
84+
if (ch == '\n' || ch == '\r') {
85+
break;
86+
}
87+
if (ch == 127 || ch == 8) { // Handle backspace
88+
if (index > 0) {
89+
index--;
90+
printf("\b \b");
91+
}
92+
} else {
93+
buffer[index++] = ch;
94+
printf("*"); // Mask the input
95+
}
96+
}
97+
buffer[index] = '\0';
98+
printf("\n"); // Move to the next line
99+
return 1;
100+
}
101+
102+
int fossil_io_display_menu(const char *prompt, const char *choices[], int num_choices) {
103+
if (prompt != NULL) {
104+
printf("%s\n", prompt);
105+
}
106+
107+
for (int i = 0; i < num_choices; i++) {
108+
printf("%d. %s\n", i + 1, choices[i]);
109+
}
110+
111+
int choice;
112+
do {
113+
printf("Please choose an option (1-%d): ", num_choices);
114+
if (fossil_io_scanf("%d", &choice) != 1 || choice < 1 || choice > num_choices) {
115+
printf("Invalid choice. Please try again.\n");
116+
}
117+
} while (choice < 1 || choice > num_choices);
118+
119+
return choice - 1; // Return the index of the chosen option
120+
}
121+
122+
int fossil_io_read_multiline_input(char *buffer, size_t size) {
123+
if (buffer == NULL || size == 0) {
124+
return 0;
125+
}
126+
127+
size_t index = 0;
128+
char ch;
129+
printf("Enter multiple lines of text (Press Enter twice to finish):\n");
130+
131+
while (index < size - 1) {
132+
ch = fossil_io_getch(); // Get a single character
133+
if (ch == '\n') {
134+
if (index > 0 && buffer[index - 1] == '\n') {
135+
break; // Two Enter presses to finish
136+
}
137+
}
138+
buffer[index++] = ch;
139+
printf("%c", ch);
140+
}
141+
buffer[index] = '\0'; // Null-terminate the string
142+
return 1;
143+
}
144+
145+
void fossil_io_show_progress(int progress) {
146+
int width = 50; // Width of the progress bar
147+
int pos = (progress * width) / 100;
148+
printf("[");
149+
for (int i = 0; i < width; i++) {
150+
if (i < pos) {
151+
printf("=");
152+
} else if (i == pos) {
153+
printf(">");
154+
} else {
155+
printf(" ");
156+
}
157+
}
158+
printf("] %d%%\r", progress);
159+
fflush(stdout);
160+
}
161+
53162
// Function to get a sanitized line of input from a provided stream (or stdin by default)
54163
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
55164
if (buf == NULL || size == 0 || input_stream == NULL) {

0 commit comments

Comments
 (0)