Stress_UI/src/main.cpp
2020-10-20 14:16:46 +02:00

253 lines
6.1 KiB
C++

#include "configFile.hpp"
#include "ui.hpp"
#include <thread>
using namespace std;
const int8_t MENU_PRINCIPAL = 0;
const int8_t MENU_INICIAR_STRESS = 1;
const int8_t MENU_CONFIGURACION = 2;
const int8_t SALIR = 3;
const int8_t CONFIGURAR_TODO = 4;
const int8_t CONFIGURAR_HILOS = 5;
const int8_t CONFIGURAR_TIEMPO = 6;
const int8_t CONFIGURAR_MENU_PRINCIPAL = 7;
const int8_t MENU_ERROR = 8;
const int8_t MENU_TITULO = 9;
const int8_t PRIMER_INICIO = 10;
const vector<string> TITULO_PRINCIPAL = {
"____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ "
" _______ ____ ",
"/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| "
"_ \\ \\ / / ____| _ \\ ",
"\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| "
"| |_) \\ \\ / /| _| | |_) |",
" ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < "
"\\ V / | |___| _ < ",
"|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| "
"\\_\\ \\_/ |_____|_| \\_\\",
"",
"_______________________________________________________________________",
};
void userInterface(const int8_t *menu);
void userInterface(const int8_t *menu, vector<string> *mensaje);
void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
vector<string> *mensaje);
void showMenu(vector<string> *menu_elems, const int8_t *num_menu);
int main(int argc, char *argv[]) {
if (ui::initUI(&MENU_PRINCIPAL, &MENU_ERROR) != 0) {
return -1;
}
// menu = 12;
Config *config = cf::openConfig();
if (config == nullptr) {
userInterface(&PRIMER_INICIO);
} else {
userInterface(&MENU_PRINCIPAL);
}
ui::closeUI();
return 0;
}
void userInterface(const int8_t *menu) {
/*int8_t menu_num = inputToInt(menu);*/
vector<string> dummy = {""};
userInterface(menu, &dummy);
}
void userInterface(const int8_t *num_menu, vector<string> *mensaje) {
switch (*num_menu) {
case CONFIGURAR_MENU_PRINCIPAL:
case MENU_PRINCIPAL: {
vector<string> elem_menu = {"- Iniciar Stress Test", "- Configurar",
"- Salir"};
showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje);
break;
}
case MENU_INICIAR_STRESS: {
/*TODO*/
break;
}
case MENU_CONFIGURACION: {
vector<string> elem_menu = {"- Configurar todo", "- Número de hilos",
"- Tiempo", "- Menú principal"};
showMenu(&elem_menu, num_menu, mensaje);
break;
}
case CONFIGURAR_TODO:
case PRIMER_INICIO: {
vector<string> textos_init = {
"Toda la configuración se escribirá en " + cf::g_cofigFile, "", ""};
vector<string> elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "};
ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens,
TITULO_PRINCIPAL.size());
userInterface(&MENU_CONFIGURACION, mensaje);
break;
}
case CONFIGURAR_HILOS: {
break;
}
case CONFIGURAR_TIEMPO: {
break;
}
case MENU_TITULO: {
break;
}
case SALIR: {
break;
}
default: {
ui::g_mensaje = true;
*mensaje = {"¿Como has llegado aquí?",
"Por si acaso vamos al menú principal"};
userInterface(&MENU_PRINCIPAL, mensaje);
break;
}
}
}
void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
vector<string> *mensaje) {
clear();
if (ui::g_mensaje) {
ui::showCenterMensaje(mensaje, &MENU_ERROR);
}
showMenu(menu_elems, num_menu);
}
void showMenu(vector<string> *menu_elems, const int8_t *num_menu) {
size_t height = menu_elems->size();
attron(COLOR_PAIR(MENU_PRINCIPAL));
WINDOW *menuwin =
// newwin(height + 2, g_maxCols - 4, g_maxLines - height - 3, 2);
newwin(height + 2, ui::g_menuWith, TITULO_PRINCIPAL.size() + 4, 2);
box(menuwin, 0, 0);
refresh();
ui::showTopTitle(&TITULO_PRINCIPAL);
wrefresh(menuwin);
keypad(menuwin, true);
int choice = 0;
int8_t highlight = 0;
switch (*num_menu) {
case MENU_PRINCIPAL: {
mvwprintw(menuwin, 0, 1, "Menú Principal");
break;
}
case MENU_CONFIGURACION: {
mvwprintw(menuwin, 0, 1, "Configuración");
break;
}
}
while (true) {
for (int i = 0; i < height; i++) {
if (i == highlight) {
wattron(menuwin, A_REVERSE);
}
if (menu_elems->at(i).length() < (ui::g_menuWith - 4)) {
string spaces = "";
for (int j = menu_elems->at(i).length(); j < (ui::g_menuWith - 4);
j++) {
spaces.append(" ");
}
// Por algún motivo los acentos y tildes cuentan como un caracter
// aparte por lo que hay que añadir un espacio extra.
if (menu_elems->at(i).find("á") != std::string::npos |
menu_elems->at(i).find("é") != std::string::npos |
menu_elems->at(i).find("í") != std::string::npos |
menu_elems->at(i).find("ó") != std::string::npos |
menu_elems->at(i).find("ú") != std::string::npos |
menu_elems->at(i).find("Á") != std::string::npos |
menu_elems->at(i).find("É") != std::string::npos |
menu_elems->at(i).find("Í") != std::string::npos |
menu_elems->at(i).find("Ó") != std::string::npos |
menu_elems->at(i).find("Ú") != std::string::npos |
menu_elems->at(i).find("ñ") != std::string::npos |
menu_elems->at(i).find("Ñ") != std::string::npos) {
spaces.append(" ");
}
menu_elems->at(i).append(spaces);
}
mvwprintw(menuwin, i + 1, 2, menu_elems->at(i).c_str());
wattroff(menuwin, A_REVERSE);
}
choice = wgetch(menuwin);
switch (choice) {
case KEY_UP: {
if (highlight > 0) {
highlight--;
}
break;
}
case KEY_DOWN: {
if ((highlight + 1) < height) {
highlight++;
}
break;
}
default: {
break;
}
}
if (choice == 10) {
break;
}
}
switch (*num_menu) {
case MENU_PRINCIPAL: {
highlight++;
break;
}
case MENU_CONFIGURACION: {
highlight = highlight + 4;
break;
}
}
attroff(COLOR_PAIR(MENU_PRINCIPAL));
userInterface(&highlight);
}