From e2b7ac2434cde09ec3c9737fbd993a59f7ade299 Mon Sep 17 00:00:00 2001 From: somebody_master Date: Tue, 27 Oct 2020 16:39:38 +0100 Subject: [PATCH] =?UTF-8?q?Re-factorizaci=C3=B3n=20del=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: somebody_master --- include/config.hpp | 20 ++-- include/configFile.hpp | 11 +-- include/ui.hpp | 22 ++--- src/config.cpp | 19 +++- src/configFile.cpp | 78 ++++------------ src/main.cpp | 206 ++++++++++++++++++----------------------- src/ui.cpp | 21 +---- 7 files changed, 154 insertions(+), 223 deletions(-) diff --git a/include/config.hpp b/include/config.hpp index 6938b27..ce9bf0c 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -2,16 +2,16 @@ #ifndef __CONFIG_HPP_ #define __CONFIG_HPP_ class Config { -private: - int8_t *numHilos; - std::string *tiempo; + private: + int8_t *numHilos; + std::string *tiempo; -public: - Config(int8_t numHilos, std::string tiempo); - // ~Config(); - int8_t getNumHilos(); - std::string getTiempo(); - void setNumHilos(int8_t numHilos); - void setTiempo(std::string timepo); + public: + Config(int8_t numHilos, std::string tiempo); + // ~Config(); + int8_t getNumHilos(); + std::string getTiempo(); + void setNumHilos(int8_t numHilos); + void setTiempo(std::string timepo); }; #endif // __CONFIG_HPP_ diff --git a/include/configFile.hpp b/include/configFile.hpp index 9ce75a1..7fe563c 100644 --- a/include/configFile.hpp +++ b/include/configFile.hpp @@ -4,11 +4,10 @@ #ifndef __CONFIG_FILE_HPP_ #define __CONFIG_FILE_HPP_ namespace cf { -const std::string g_configFile = - std::string(getenv("HOME")) + "/.config/stressUI.cfg"; -Config openConfig(); -void closeConfig(Config *config); -std::string getLine(std::ifstream *file); -void saveConfig(Config *config); + const std::string g_configFile = std::string(getenv("HOME")) + "/.config/stressUI.cfg"; + Config openConfig(); + void closeConfig(Config *config); + std::string getLine(std::ifstream *file); + void saveConfig(Config *config); } // namespace cf #endif // __CONFIG_FILE_HPP_ diff --git a/include/ui.hpp b/include/ui.hpp index 0f13dd1..7dcfbd0 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -5,21 +5,15 @@ #define __UI_HPP_ namespace ui { + extern int g_maxLines, g_maxCols, g_begCol, g_begLine, g_menuWith; + extern bool g_mensaje; -extern int g_maxLines, g_maxCols, g_begCol, g_begLine, g_menuWith; -extern bool g_mensaje; + int8_t initUI(const int8_t *menu_principal, const int8_t *menu_error); -int8_t initUI(const int8_t *menu_principal, const int8_t *menu_error); -void closeUI(); -void showCentralInputBox(std::vector *textos, - const int8_t *num_box, - std::vector *elems); -std::vector showCentralInputBox(std::vector *textos, - const int8_t *num_box, - std::vector *elems, - long title_size); -void showCenterMensaje(std::vector *mensaje, - const int8_t *menu_color); -void showTopTitle(const std::vector *titulo); + void closeUI(); + void showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems); + std::vector showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems, long title_size); + void showCenterMensaje(std::vector *mensaje, const int8_t *menu_color); + void showTopTitle(const std::vector *titulo); } // namespace ui #endif // __UI_HPP_ diff --git a/src/config.cpp b/src/config.cpp index 094eefa..3466479 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -9,7 +9,18 @@ Config::Config(int8_t numHilos, std::string tiempo) { // delete numHilos; // delete tiempo; // } -int8_t Config::getNumHilos() { return *numHilos; } -std::string Config::getTiempo() { return *tiempo; } -void Config::setNumHilos(int8_t numHilos) { Config::numHilos = &numHilos; } -void Config::setTiempo(std::string tiempo) { Config::tiempo = &tiempo; } +int8_t Config::getNumHilos() { + return *numHilos; +} + +std::string Config::getTiempo() { + return *tiempo; +} + +void Config::setNumHilos(int8_t numHilos) { + Config::numHilos = &numHilos; +} + +void Config::setTiempo(std::string tiempo) { + Config::tiempo = &tiempo; +} diff --git a/src/configFile.cpp b/src/configFile.cpp index 92595c9..53c6d92 100644 --- a/src/configFile.cpp +++ b/src/configFile.cpp @@ -48,68 +48,28 @@ std::string cf::getLine(std::ifstream *file) { void cf::saveConfig(Config *config) { std::ofstream file(g_configFile); - file << "####################################################################" - "###" - << std::endl; - file << "# + + + + " - " #" - << std::endl; - file << "# + + + " - " #" - << std::endl; - file << "# + + " - " #" - << std::endl; - file << "# \\ / " - " #" - << std::endl; - file << "# + _ - _+_ - " - "___ #" - << std::endl; - file << "# _=. .:. /=\\ _|===|_ || " - "::| #" - << std::endl; - file << "# | | _|. | | | | | | __===_ -=- || " - "::| #" - << std::endl; - file << "# |==| | | __ |.:.| /\\| | :.| | | | .|| : ||| " - "::| #" - << std::endl; - file << "# | |- |.:|_|. :__ |.: |--|==| | .| |_ | . |. ||. ||| " - ":.| #" - << std::endl; - file << "# __|. | |_|. | |.|...||---| |==| | | | |_--. || |||. " - "| #" - << std::endl; - file << "# | | | |. | | |::.|| :.| |==| | . : |=|===| :|| . ||| " - ".| #" - << std::endl; - file << "# |:.| .| | | | |:.:|| . | |==| | |=|===| . | | | " - "| #" - << std::endl; - file << "# | | | | | : . | ; ; " - "| #" - << std::endl; - file << "# : : . . . " - ": #" - << std::endl; - file << "# " - " #" - << std::endl; - file << "# Archivo autogenerado por Stress_UI " - " #" - << std::endl; - file << "# No editar manualmente sin saber lo que se hace " - " #" - << std::endl; - file << "####################################################################" - "###" - << std::endl; + file << "#######################################################################" << std::endl; + file << "# + + + + #" << std::endl; + file << "# + + + #" << std::endl; + file << "# + + #" << std::endl; + file << "# \\ / #" << std::endl; + file << "# + _ - _+_ - ___ #" << std::endl; + file << "# _=. .:. /=\\ _|===|_ || ::| #" << std::endl; + file << "# | | _|. | | | | | | __===_ -=- || ::| #" << std::endl; + file << "# |==| | | __ |.:.| /\\| | :.| | | | .|| : ||| ::| #" << std::endl; + file << "# | |- |.:|_|. :__ |.: |--|==| | .| |_ | . |. ||. ||| :.| #" << std::endl; + file << "# __|. | |_|. | |.|...||---| |==| | | | |_--. || |||. | #" << std::endl; + file << "# | | | |. | | |::.|| :.| |==| | . : |=|===| :|| . ||| .| #" << std::endl; + file << "# |:.| .| | | | |:.:|| . | |==| | |=|===| . | | | | #" << std::endl; + file << "# | | | | | : . | ; ; | #" << std::endl; + file << "# : : . . . : #" << std::endl; + file << "# #" << std::endl; + file << "# Archivo autogenerado por Stress_UI #" << std::endl; + file << "# No editar manualmente sin saber lo que se hace #" << std::endl; + file << "#######################################################################" << std::endl; file << std::endl; file << "Número de hilos\t\t= " << std::to_string(config->getNumHilos()); file << std::endl; file << "Tiempo de ejecución\t= " << config->getTiempo(); - // std::string str = "shit"; - // file << str; file.close(); } diff --git a/src/main.cpp b/src/main.cpp index 043c9e3..0b92295 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,26 +22,19 @@ const int8_t MENU_TITULO = 9; const int8_t PRIMER_INICIO = 10; const vector TITULO_PRINCIPAL = { - "____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ " - " _______ ____ ", - "/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| " - "_ \\ \\ / / ____| _ \\ ", - "\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| " - "| |_) \\ \\ / /| _| | |_) |", - " ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < " - "\\ V / | |___| _ < ", - "|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| " - "\\_\\ \\_/ |_____|_| \\_\\", + "____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ _______ ____ ", + "/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| _ \\ \\ / / ____| _ \\ ", + "\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| | |_) \\ \\ / /| _| | |_) |", + " ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < \\ V / | |___| _ < ", + "|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| \\_\\ \\_/ |_____|_| \\_\\", "", "_______________________________________________________________________", }; void userInterface(const int8_t *menu, Config *config); void userInterface(const int8_t *menu, vector *mensaje, Config *config); -void showMenu(vector *menu_elems, const int8_t *num_menu, - vector *mensaje, Config *config); -void showMenu(vector *menu_elems, const int8_t *num_menu, - Config *config); +void showMenu(vector *menu_elems, const int8_t *num_menu, vector *mensaje, Config *config); +void showMenu(vector *menu_elems, const int8_t *num_menu, Config *config); int main(int argc, char *argv[]) { // locale::global(locale("es_ES.utf8")); @@ -64,97 +57,84 @@ int main(int argc, char *argv[]) { } void userInterface(const int8_t *menu, Config *config) { - /*int8_t menu_num = inputToInt(menu);*/ vector dummy = {""}; userInterface(menu, &dummy, config); } -void userInterface(const int8_t *num_menu, vector *mensaje, - Config *config) { +void userInterface(const int8_t *num_menu, vector *mensaje, Config *config) { switch (*num_menu) { + case CONFIGURAR_MENU_PRINCIPAL: + case MENU_PRINCIPAL: { + vector elem_menu = {"- Iniciar Stress Test", "- Configurar", "- Salir"}; + showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config); + break; + } + case MENU_INICIAR_STRESS: { + /*TODO*/ + break; + } - case CONFIGURAR_MENU_PRINCIPAL: - case MENU_PRINCIPAL: { - vector elem_menu = {"- Iniciar Stress Test", "- Configurar", - "- Salir"}; - showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config); - break; - } + case MENU_CONFIGURACION: { + vector elem_menu = {"- Configurar todo", "- Número de hilos", "- Tiempo", "- Menú principal"}; + showMenu(&elem_menu, num_menu, mensaje, config); + break; + } - case MENU_INICIAR_STRESS: { - /*TODO*/ - break; - } + case CONFIGURAR_TODO: + case PRIMER_INICIO: { - case MENU_CONFIGURACION: { - vector elem_menu = {"- Configurar todo", "- Número de hilos", - "- Tiempo", "- Menú principal"}; + vector textos_init = {"Toda la configuración se escribirá en " + cf::g_configFile, "", ""}; + vector elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "}; + ui::showTopTitle(&TITULO_PRINCIPAL); + vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size()); + Config config2 = Config(atoi(entrada[0].c_str()), entrada[1]); + config = &config2; + cf::saveConfig(config); + userInterface(&MENU_PRINCIPAL, mensaje, config); + break; + } - showMenu(&elem_menu, num_menu, mensaje, config); - break; - } + case CONFIGURAR_HILOS: { + /*TODO*/ + break; + } - case CONFIGURAR_TODO: - case PRIMER_INICIO: { + case CONFIGURAR_TIEMPO: { + /*TODO*/ + break; + } - vector textos_init = { - "Toda la configuración se escribirá en " + cf::g_configFile, "", ""}; - vector elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "}; - ui::showTopTitle(&TITULO_PRINCIPAL); - vector entrada = ui::showCentralInputBox( - &textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size()); - Config config2 = Config(atoi(entrada[0].c_str()), entrada[1]); - config = &config2; - cf::saveConfig(config); - userInterface(&MENU_PRINCIPAL, mensaje, config); - break; - } + case MENU_TITULO: { + /*TODO*/ + break; + } - case CONFIGURAR_HILOS: { - break; - } + case SALIR: { + 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, config); - break; - } + default: { + ui::g_mensaje = true; + *mensaje = {"¿Como has llegado aquí?", "Por si acaso vamos al menú principal"}; + userInterface(&MENU_PRINCIPAL, mensaje, config); + break; + } } } -void showMenu(vector *menu_elems, const int8_t *num_menu, - vector *mensaje, Config *config) { +void showMenu(vector *menu_elems, const int8_t *num_menu, vector *mensaje, Config *config) { clear(); if (ui::g_mensaje) { ui::showCenterMensaje(mensaje, &MENU_ERROR); } - showMenu(menu_elems, num_menu, config); } -void showMenu(vector *menu_elems, const int8_t *num_menu, - Config *config) { +void showMenu(vector *menu_elems, const int8_t *num_menu, Config *config) { 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); + WINDOW *menuwin = newwin(height + 2, ui::g_menuWith, TITULO_PRINCIPAL.size() + 4, 2); box(menuwin, 0, 0); refresh(); @@ -166,28 +146,28 @@ void showMenu(vector *menu_elems, const int8_t *num_menu, int choice = 0; int8_t highlight = 0; + switch (*num_menu) { - case MENU_PRINCIPAL: { - mvwprintw(menuwin, 0, 1, "Menú Principal"); - break; + case MENU_PRINCIPAL: { + mvwprintw(menuwin, 0, 1, "Menú Principal"); + break; + } + + case MENU_CONFIGURACION: { + mvwprintw(menuwin, 0, 1, "Configuración"); + 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)) { - for (int j = menu_elems->at(i).length(); j < (ui::g_menuWith - 4); - j++) { + for (int j = menu_elems->at(i).length(); j < (ui::g_menuWith - 4); j++) { menu_elems->at(i).append(" "); } /* @@ -208,6 +188,7 @@ void showMenu(vector *menu_elems, const int8_t *num_menu, menu_elems->at(i).substr(j, 2).compare("Ú") == 0 | menu_elems->at(i).substr(j, 2).compare("ñ") == 0 | menu_elems->at(i).substr(j, 2).compare("Ñ") == 0) { + menu_elems->at(i).append(" "); } } @@ -221,24 +202,22 @@ void showMenu(vector *menu_elems, const int8_t *num_menu, choice = wgetch(menuwin); switch (choice) { - - case KEY_UP: { - if (highlight > 0) { - highlight--; + case KEY_UP: { + if (highlight > 0) { + highlight--; + } + break; } - break; - } - - case KEY_DOWN: { - if ((highlight + 1) < height) { - highlight++; + case KEY_DOWN: { + if ((highlight + 1) < height) { + highlight++; + } + break; } - break; - } - default: { - break; - } + default: { + break; + } } if (choice == 10) { @@ -247,15 +226,14 @@ void showMenu(vector *menu_elems, const int8_t *num_menu, } switch (*num_menu) { - case MENU_PRINCIPAL: { - highlight++; - break; - } - - case MENU_CONFIGURACION: { - highlight = highlight + 4; - break; - } + case MENU_PRINCIPAL: { + highlight++; + break; + } + case MENU_CONFIGURACION: { + highlight = highlight + 4; + break; + } } attroff(COLOR_PAIR(MENU_PRINCIPAL)); diff --git a/src/ui.cpp b/src/ui.cpp index 4c9b702..de565cf 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -50,26 +50,20 @@ void ui::showCenterMensaje(vector *mensaje, const int8_t *menu_color) { g_mensaje = false; } -void ui::showCentralInputBox(std::vector *textos, - const int8_t *num_box, - std::vector *elems) { +void ui::showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems) { ui::showCentralInputBox(textos, num_box, elems, 0); } -vector ui::showCentralInputBox(vector *textos, - const int8_t *num_box, - vector *elems, long title_size) { - +vector ui::showCentralInputBox(vector *textos, const int8_t *num_box, vector *elems, long title_size) { vector entrada; - int start_ver_window = 1; + if (title_size != 0) { start_ver_window = title_size + 4; } int height = ui::g_maxLines - (title_size + 4) - 1; - WINDOW *centralBOX = newwin(height, ui::g_maxCols - ui::g_menuWith - 4, - start_ver_window, ui::g_menuWith + 2); + WINDOW *centralBOX = newwin(height, ui::g_maxCols - ui::g_menuWith - 4, start_ver_window, ui::g_menuWith + 2); box(centralBOX, 0, 0); for (int i = 0; i < textos->size(); i++) { @@ -91,10 +85,6 @@ vector ui::showCentralInputBox(vector *textos, noecho(); cbreak(); - // std::chrono::milliseconds timespan(10000); // or whatever - - // std::this_thread::sleep_for(timespan); - return entrada; } @@ -104,8 +94,7 @@ void ui::showTopTitle(const vector *titulo) { attron(A_BOLD); box(topTitleWin, 0, 0); for (int i = 0; i < height; i++) { - mvwprintw(topTitleWin, i + 1, (g_maxCols - 4 - titulo->at(i).length()) / 2, - titulo->at(i).c_str()); + mvwprintw(topTitleWin, i + 1, (g_maxCols - 4 - titulo->at(i).length()) / 2, titulo->at(i).c_str()); } attroff(A_BOLD); wrefresh(topTitleWin);