Re-factorización del código

Signed-off-by: somebody_master <somebody_master@somebodyserver.mooo.com>
This commit is contained in:
Somebody Master 2020-10-27 16:39:38 +01:00
parent b0aa7eea06
commit e2b7ac2434
Signed by: somebody_master
GPG Key ID: 78315CFDF0B25505
7 changed files with 154 additions and 223 deletions

View File

@ -2,16 +2,16 @@
#ifndef __CONFIG_HPP_ #ifndef __CONFIG_HPP_
#define __CONFIG_HPP_ #define __CONFIG_HPP_
class Config { class Config {
private: private:
int8_t *numHilos; int8_t *numHilos;
std::string *tiempo; std::string *tiempo;
public: public:
Config(int8_t numHilos, std::string tiempo); Config(int8_t numHilos, std::string tiempo);
// ~Config(); // ~Config();
int8_t getNumHilos(); int8_t getNumHilos();
std::string getTiempo(); std::string getTiempo();
void setNumHilos(int8_t numHilos); void setNumHilos(int8_t numHilos);
void setTiempo(std::string timepo); void setTiempo(std::string timepo);
}; };
#endif // __CONFIG_HPP_ #endif // __CONFIG_HPP_

View File

@ -4,11 +4,10 @@
#ifndef __CONFIG_FILE_HPP_ #ifndef __CONFIG_FILE_HPP_
#define __CONFIG_FILE_HPP_ #define __CONFIG_FILE_HPP_
namespace cf { namespace cf {
const std::string g_configFile = const std::string g_configFile = std::string(getenv("HOME")) + "/.config/stressUI.cfg";
std::string(getenv("HOME")) + "/.config/stressUI.cfg"; Config openConfig();
Config openConfig(); void closeConfig(Config *config);
void closeConfig(Config *config); std::string getLine(std::ifstream *file);
std::string getLine(std::ifstream *file); void saveConfig(Config *config);
void saveConfig(Config *config);
} // namespace cf } // namespace cf
#endif // __CONFIG_FILE_HPP_ #endif // __CONFIG_FILE_HPP_

View File

@ -5,21 +5,15 @@
#define __UI_HPP_ #define __UI_HPP_
namespace ui { 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; int8_t initUI(const int8_t *menu_principal, const int8_t *menu_error);
extern bool g_mensaje;
int8_t initUI(const int8_t *menu_principal, const int8_t *menu_error); void closeUI();
void closeUI(); void showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems);
void showCentralInputBox(std::vector<std::string> *textos, std::vector<std::string> showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems, long title_size);
const int8_t *num_box, void showCenterMensaje(std::vector<std::string> *mensaje, const int8_t *menu_color);
std::vector<std::string> *elems); void showTopTitle(const std::vector<std::string> *titulo);
std::vector<std::string> showCentralInputBox(std::vector<std::string> *textos,
const int8_t *num_box,
std::vector<std::string> *elems,
long title_size);
void showCenterMensaje(std::vector<std::string> *mensaje,
const int8_t *menu_color);
void showTopTitle(const std::vector<std::string> *titulo);
} // namespace ui } // namespace ui
#endif // __UI_HPP_ #endif // __UI_HPP_

View File

@ -9,7 +9,18 @@ Config::Config(int8_t numHilos, std::string tiempo) {
// delete numHilos; // delete numHilos;
// delete tiempo; // delete tiempo;
// } // }
int8_t Config::getNumHilos() { return *numHilos; } int8_t Config::getNumHilos() {
std::string Config::getTiempo() { return *tiempo; } return *numHilos;
void Config::setNumHilos(int8_t numHilos) { Config::numHilos = &numHilos; } }
void Config::setTiempo(std::string tiempo) { Config::tiempo = &tiempo; }
std::string Config::getTiempo() {
return *tiempo;
}
void Config::setNumHilos(int8_t numHilos) {
Config::numHilos = &numHilos;
}
void Config::setTiempo(std::string tiempo) {
Config::tiempo = &tiempo;
}

View File

@ -48,68 +48,28 @@ std::string cf::getLine(std::ifstream *file) {
void cf::saveConfig(Config *config) { void cf::saveConfig(Config *config) {
std::ofstream file(g_configFile); std::ofstream file(g_configFile);
file << "####################################################################" file << "#######################################################################" << std::endl;
"###" file << "# + + + + #" << std::endl;
<< std::endl; file << "# + + + #" << std::endl;
file << "# + + + + " file << "# + + #" << std::endl;
" #" file << "# \\ / #" << std::endl;
<< std::endl; file << "# + _ - _+_ - ___ #" << std::endl;
file << "# + + + " file << "# _=. .:. /=\\ _|===|_ || ::| #" << std::endl;
" #" file << "# | | _|. | | | | | | __===_ -=- || ::| #" << std::endl;
<< std::endl; file << "# |==| | | __ |.:.| /\\| | :.| | | | .|| : ||| ::| #" << std::endl;
file << "# + + " file << "# | |- |.:|_|. :__ |.: |--|==| | .| |_ | . |. ||. ||| :.| #" << std::endl;
" #" file << "# __|. | |_|. | |.|...||---| |==| | | | |_--. || |||. | #" << std::endl;
<< std::endl; file << "# | | | |. | | |::.|| :.| |==| | . : |=|===| :|| . ||| .| #" << std::endl;
file << "# \\ / " file << "# |:.| .| | | | |:.:|| . | |==| | |=|===| . | | | | #" << std::endl;
" #" file << "# | | | | | : . | ; ; | #" << std::endl;
<< std::endl; file << "# : : . . . : #" << std::endl;
file << "# + _ - _+_ - " file << "# #" << std::endl;
"___ #" file << "# Archivo autogenerado por Stress_UI #" << std::endl;
<< std::endl; file << "# No editar manualmente sin saber lo que se hace #" << std::endl;
file << "# _=. .:. /=\\ _|===|_ || " file << "#######################################################################" << std::endl;
"::| #"
<< 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 << "Número de hilos\t\t= " << std::to_string(config->getNumHilos()); file << "Número de hilos\t\t= " << std::to_string(config->getNumHilos());
file << std::endl; file << std::endl;
file << "Tiempo de ejecución\t= " << config->getTiempo(); file << "Tiempo de ejecución\t= " << config->getTiempo();
// std::string str = "shit";
// file << str;
file.close(); file.close();
} }

View File

@ -22,26 +22,19 @@ const int8_t MENU_TITULO = 9;
const int8_t PRIMER_INICIO = 10; const int8_t PRIMER_INICIO = 10;
const vector<string> TITULO_PRINCIPAL = { const vector<string> TITULO_PRINCIPAL = {
"____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ " "____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ _______ ____ ",
" _______ ____ ", "/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| _ \\ \\ / / ____| _ \\ ",
"/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| " "\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| | |_) \\ \\ / /| _| | |_) |",
"_ \\ \\ / / ____| _ \\ ", " ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < \\ V / | |___| _ < ",
"\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| " "|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| \\_\\ \\_/ |_____|_| \\_\\",
"| |_) \\ \\ / /| _| | |_) |",
" ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < "
"\\ V / | |___| _ < ",
"|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| "
"\\_\\ \\_/ |_____|_| \\_\\",
"", "",
"_______________________________________________________________________", "_______________________________________________________________________",
}; };
void userInterface(const int8_t *menu, Config *config); void userInterface(const int8_t *menu, Config *config);
void userInterface(const int8_t *menu, vector<string> *mensaje, Config *config); void userInterface(const int8_t *menu, vector<string> *mensaje, Config *config);
void showMenu(vector<string> *menu_elems, const int8_t *num_menu, void showMenu(vector<string> *menu_elems, const int8_t *num_menu, vector<string> *mensaje, Config *config);
vector<string> *mensaje, Config *config); void showMenu(vector<string> *menu_elems, const int8_t *num_menu, Config *config);
void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
Config *config);
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// locale::global(locale("es_ES.utf8")); // locale::global(locale("es_ES.utf8"));
@ -64,97 +57,84 @@ int main(int argc, char *argv[]) {
} }
void userInterface(const int8_t *menu, Config *config) { void userInterface(const int8_t *menu, Config *config) {
/*int8_t menu_num = inputToInt(menu);*/
vector<string> dummy = {""}; vector<string> dummy = {""};
userInterface(menu, &dummy, config); userInterface(menu, &dummy, config);
} }
void userInterface(const int8_t *num_menu, vector<string> *mensaje, void userInterface(const int8_t *num_menu, vector<string> *mensaje, Config *config) {
Config *config) {
switch (*num_menu) { 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, config);
break;
}
case MENU_INICIAR_STRESS: {
/*TODO*/
break;
}
case CONFIGURAR_MENU_PRINCIPAL: case MENU_CONFIGURACION: {
case MENU_PRINCIPAL: { vector<string> elem_menu = {"- Configurar todo", "- Número de hilos", "- Tiempo", "- Menú principal"};
vector<string> elem_menu = {"- Iniciar Stress Test", "- Configurar", showMenu(&elem_menu, num_menu, mensaje, config);
"- Salir"}; break;
showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config); }
break;
}
case MENU_INICIAR_STRESS: { case CONFIGURAR_TODO:
/*TODO*/ case PRIMER_INICIO: {
break;
}
case MENU_CONFIGURACION: { vector<string> textos_init = {"Toda la configuración se escribirá en " + cf::g_configFile, "", ""};
vector<string> elem_menu = {"- Configurar todo", "- Número de hilos", vector<string> elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "};
"- Tiempo", "- Menú principal"}; ui::showTopTitle(&TITULO_PRINCIPAL);
vector<string> 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); case CONFIGURAR_HILOS: {
break; /*TODO*/
} break;
}
case CONFIGURAR_TODO: case CONFIGURAR_TIEMPO: {
case PRIMER_INICIO: { /*TODO*/
break;
}
vector<string> textos_init = { case MENU_TITULO: {
"Toda la configuración se escribirá en " + cf::g_configFile, "", ""}; /*TODO*/
vector<string> elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "}; break;
ui::showTopTitle(&TITULO_PRINCIPAL); }
vector<string> 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 CONFIGURAR_HILOS: { case SALIR: {
break; break;
} }
case CONFIGURAR_TIEMPO: { default: {
break; ui::g_mensaje = true;
} *mensaje = {"¿Como has llegado aquí?", "Por si acaso vamos al menú principal"};
userInterface(&MENU_PRINCIPAL, mensaje, config);
case MENU_TITULO: { break;
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;
}
} }
} }
void showMenu(vector<string> *menu_elems, const int8_t *num_menu, void showMenu(vector<string> *menu_elems, const int8_t *num_menu, vector<string> *mensaje, Config *config) {
vector<string> *mensaje, Config *config) {
clear(); clear();
if (ui::g_mensaje) { if (ui::g_mensaje) {
ui::showCenterMensaje(mensaje, &MENU_ERROR); ui::showCenterMensaje(mensaje, &MENU_ERROR);
} }
showMenu(menu_elems, num_menu, config); showMenu(menu_elems, num_menu, config);
} }
void showMenu(vector<string> *menu_elems, const int8_t *num_menu, void showMenu(vector<string> *menu_elems, const int8_t *num_menu, Config *config) {
Config *config) {
size_t height = menu_elems->size(); size_t height = menu_elems->size();
attron(COLOR_PAIR(MENU_PRINCIPAL)); attron(COLOR_PAIR(MENU_PRINCIPAL));
WINDOW *menuwin = WINDOW *menuwin = newwin(height + 2, ui::g_menuWith, TITULO_PRINCIPAL.size() + 4, 2);
// 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); box(menuwin, 0, 0);
refresh(); refresh();
@ -166,28 +146,28 @@ void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
int choice = 0; int choice = 0;
int8_t highlight = 0; int8_t highlight = 0;
switch (*num_menu) { switch (*num_menu) {
case MENU_PRINCIPAL: { case MENU_PRINCIPAL: {
mvwprintw(menuwin, 0, 1, "Menú Principal"); mvwprintw(menuwin, 0, 1, "Menú Principal");
break; break;
}
case MENU_CONFIGURACION: {
mvwprintw(menuwin, 0, 1, "Configuración");
break;
}
} }
case MENU_CONFIGURACION: {
mvwprintw(menuwin, 0, 1, "Configuración");
break;
}
}
while (true) { while (true) {
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
if (i == highlight) { if (i == highlight) {
wattron(menuwin, A_REVERSE); wattron(menuwin, A_REVERSE);
} }
if (menu_elems->at(i).length() < (ui::g_menuWith - 4)) { if (menu_elems->at(i).length() < (ui::g_menuWith - 4)) {
for (int j = menu_elems->at(i).length(); j < (ui::g_menuWith - 4); for (int j = menu_elems->at(i).length(); j < (ui::g_menuWith - 4); j++) {
j++) {
menu_elems->at(i).append(" "); menu_elems->at(i).append(" ");
} }
/* /*
@ -208,6 +188,7 @@ void showMenu(vector<string> *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).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(" "); menu_elems->at(i).append(" ");
} }
} }
@ -221,24 +202,22 @@ void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
choice = wgetch(menuwin); choice = wgetch(menuwin);
switch (choice) { switch (choice) {
case KEY_UP: {
case KEY_UP: { if (highlight > 0) {
if (highlight > 0) { highlight--;
highlight--; }
break;
} }
break; case KEY_DOWN: {
} if ((highlight + 1) < height) {
highlight++;
case KEY_DOWN: { }
if ((highlight + 1) < height) { break;
highlight++;
} }
break;
}
default: { default: {
break; break;
} }
} }
if (choice == 10) { if (choice == 10) {
@ -247,15 +226,14 @@ void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
} }
switch (*num_menu) { switch (*num_menu) {
case MENU_PRINCIPAL: { case MENU_PRINCIPAL: {
highlight++; highlight++;
break; break;
} }
case MENU_CONFIGURACION: {
case MENU_CONFIGURACION: { highlight = highlight + 4;
highlight = highlight + 4; break;
break; }
}
} }
attroff(COLOR_PAIR(MENU_PRINCIPAL)); attroff(COLOR_PAIR(MENU_PRINCIPAL));

View File

@ -50,26 +50,20 @@ void ui::showCenterMensaje(vector<string> *mensaje, const int8_t *menu_color) {
g_mensaje = false; g_mensaje = false;
} }
void ui::showCentralInputBox(std::vector<std::string> *textos, void ui::showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems) {
const int8_t *num_box,
std::vector<std::string> *elems) {
ui::showCentralInputBox(textos, num_box, elems, 0); ui::showCentralInputBox(textos, num_box, elems, 0);
} }
vector<string> ui::showCentralInputBox(vector<string> *textos, vector<string> ui::showCentralInputBox(vector<string> *textos, const int8_t *num_box, vector<string> *elems, long title_size) {
const int8_t *num_box,
vector<string> *elems, long title_size) {
vector<string> entrada; vector<string> entrada;
int start_ver_window = 1; int start_ver_window = 1;
if (title_size != 0) { if (title_size != 0) {
start_ver_window = title_size + 4; start_ver_window = title_size + 4;
} }
int height = ui::g_maxLines - (title_size + 4) - 1; int height = ui::g_maxLines - (title_size + 4) - 1;
WINDOW *centralBOX = newwin(height, ui::g_maxCols - ui::g_menuWith - 4, WINDOW *centralBOX = newwin(height, ui::g_maxCols - ui::g_menuWith - 4, start_ver_window, ui::g_menuWith + 2);
start_ver_window, ui::g_menuWith + 2);
box(centralBOX, 0, 0); box(centralBOX, 0, 0);
for (int i = 0; i < textos->size(); i++) { for (int i = 0; i < textos->size(); i++) {
@ -91,10 +85,6 @@ vector<string> ui::showCentralInputBox(vector<string> *textos,
noecho(); noecho();
cbreak(); cbreak();
// std::chrono::milliseconds timespan(10000); // or whatever
// std::this_thread::sleep_for(timespan);
return entrada; return entrada;
} }
@ -104,8 +94,7 @@ void ui::showTopTitle(const vector<string> *titulo) {
attron(A_BOLD); attron(A_BOLD);
box(topTitleWin, 0, 0); box(topTitleWin, 0, 0);
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
mvwprintw(topTitleWin, i + 1, (g_maxCols - 4 - titulo->at(i).length()) / 2, mvwprintw(topTitleWin, i + 1, (g_maxCols - 4 - titulo->at(i).length()) / 2, titulo->at(i).c_str());
titulo->at(i).c_str());
} }
attroff(A_BOLD); attroff(A_BOLD);
wrefresh(topTitleWin); wrefresh(topTitleWin);