Enviado em 23/09/2018 - 13:01h
Olá, andei escrevendo um simples programa usando ncurses (coisa de iniciante) e gostaria de receber opiniões de vocês em relação ao código, como, por exemplo, algumas sugestões para trabalhar corretamente com ncurses ou dicas de boas praticas de programação.
#include <string.h>
#include <ncurses.h>
#include <stdbool.h>
void delete_window(WINDOW *win){
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(win);
delwin(win);
}
void draw_dialog(char *menssage, int height, int width,
int posi_y, int posi_x, int dialogColor){
WINDOW *dialogWin=newwin(height, width, posi_y, posi_x);
init_pair(5, COLOR_WHITE, dialogColor);
wbkgd(dialogWin, COLOR_PAIR(5));
box(dialogWin, 0, 0);
wrefresh(dialogWin);
wattron(dialogWin, A_BOLD);
mvwprintw(dialogWin, posi_y-4, posi_x+4, menssage);
wattroff(dialogWin, A_BOLD);
wattron(dialogWin, A_BOLD | A_REVERSE);
mvwprintw(dialogWin, posi_y, posi_x+6, "[OK]");
wattron(dialogWin, A_BOLD | A_REVERSE);
wrefresh(dialogWin);
getch();
delete_window(dialogWin);
}
WINDOW *draw_main_window(int height, int width,
int posi_y, int posi_x){
WINDOW *win=newwin(height, width, posi_y, posi_x);
init_pair(1, COLOR_BLUE, COLOR_BLUE);
init_pair(2, COLOR_RED, COLOR_WHITE);
bkgd(COLOR_PAIR(1));
refresh();
wbkgd(win, COLOR_PAIR(2));
box(win, 0, 0);
wrefresh(win);
return win;
}
void draw_menu(WINDOW *win, int posi_y, int posi_x){
init_pair(3, COLOR_WHITE, COLOR_BLACK);
init_pair(4, COLOR_RED, COLOR_WHITE);
wattron(win, COLOR_PAIR(4) | A_BOLD);
mvwprintw(win, posi_y-4, posi_x-7, "Are you a synthetic soldier?");
wattroff(win, COLOR_PAIR(4) | A_BOLD);
keypad(win, true);
curs_set(0);
char *choices[2]={"[YES]", "[NO]"};
int highlights=0;
int choice;
while(true){
for(unsigned int i=0; i<2; i++){
if(i==highlights){
wattron(win, COLOR_PAIR(3));
}
mvwprintw(win, posi_y, (posi_x+i*8),
choices[i]);
wattroff(win, COLOR_PAIR(1));
}
choice=wgetch(win);
switch(choice){
case KEY_LEFT:
highlights--;
if(highlights==-1){
highlights=0;
}
break;
case KEY_RIGHT:
highlights++;
if(highlights==2){
highlights=1;
}
break;
default:
break;
}
if(choice==10 && strcmp(choices[highlights], "[NO]")==0){
delete_window(win);
draw_dialog("FOCK YOU!", 10, 50,
posi_y, posi_x-1, COLOR_RED);
break;
}else if(choice==10 && strcmp(choices[highlights], "[YES]")==0){
delete_window(win);
draw_dialog(" YEAP! ", 10, 50,
posi_y, posi_x-1, COLOR_BLACK);
break;
}
}
}
int main(void){
initscr();
start_color();
int screen_height, screen_width;
getmaxyx(stdscr, screen_height, screen_width);
WINDOW *win=draw_main_window(10, 50,
(screen_height/2)-5,
(screen_width/2)-24);
draw_menu(win, (screen_height/2)-5,
(screen_width/2)-23);
endwin();
return 0;
}
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Visualizar arquivos em formato markdown (ex.: README.md) pelo terminal
Dando - teoricamente - um gás no Gnome-Shell do Arch Linux
Como instalar o Google Cloud CLI no Ubuntu/Debian
Mantenha seu Sistema Leve e Rápido com a Limpeza do APT!
Procurando vídeos de YouTube pelo terminal e assistindo via mpv (2025)
Zorin OS - Virtual Box não consigo abrir maquinas virtuais (1)
O que você está ouvindo agora? [2] (180)