Untitled
unknown
plain_text
9 months ago
2.3 kB
13
Indexable
//Селектор выбор через цифры. Enter подтверждение
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
/*Перевод в режим non-canonical. getch работает без Enter. Взято в Интернете*/
static struct termios stored_settings;
void set_keypress(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
/* Disable canonical mode, and set buffer size to 1 byte */
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
return;
}
void reset_keypress(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
void print_select(int select) {
char colored[14] = "\033[43m";
/*Сброс цвета*/
char uncolored[14] = "\033[0m";
int i = 0;
system("clear");
char comand[3][10] = {"ls", "pwd", "beep"};
for (i =1; i <= 3; i++) {
if (i == select)
printf("%d %s %s %s\n", i, colored, comand[i-1], uncolored);
if (i != select)
printf("%d %s \n", i, comand[i-1]);
}
}
int main(void)
{
//Цвет
char colored[14] = "\033[43m";
/*Сброс цвета*/
char uncolored[14] = "\033[0m";
int select = 0;
//Вызываем селектор 1 раз, пустой
print_select(select);
//С помощью terminos переключим вывод терминала в non-canonical mode.
//Это позволит не нажимать Enter
set_keypress();
//Для чтения с терминала
char value = getchar();
while (1) {
if (value > '0') {
//Приведем к int
select= value -'0';
//Вызываем селектор с выбором
print_select(select);
}
if (value == '4')
break;
//Читаем символ для выбора и выделения
value = getchar();
if (value == '\n' && select == 1)
system("ls");
if (value == '\n' && select == 2)
system("pwd");
if (value == '\n' && select == 3)
system("beep");
}
printf("%s Выход %s", colored, uncolored);
return 0;
}
Editor is loading...
Leave a Comment