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;
}
Atenção a quem posta conteúdo de dicas, scripts e tal (2)
Manutenção de sistemas Linux Debian e derivados com apt-get, apt, aptitude e dpkg
Melhorando o tempo de boot do Fedora e outras distribuições
Como instalar as extensões Dash To Dock e Hide Top Bar no Gnome 45/46
Como criar um robot em NTSL (Nelogica Trading System Language) para Profitchart
Use o Google PageSpeed Insights para verificar um site
Como Atualizar Fedora 39 para 40
Instalacao Manjaro em notebook com placa de video hibrida (1)
filtrar só as pastas onde tem arquivo (2)
Sistema da Coréia do Norte - Red Star OS (19)
Como adicionar módulo de saúde da bateria dos notebooks Acer ao kernel... (30)