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_
#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_

View File

@ -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_

View File

@ -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<std::string> *textos,
const int8_t *num_box,
std::vector<std::string> *elems);
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);
void closeUI();
void showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems);
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
#endif // __UI_HPP_

View File

@ -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;
}

View File

@ -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();
}

View File

@ -22,26 +22,19 @@ const int8_t MENU_TITULO = 9;
const int8_t PRIMER_INICIO = 10;
const vector<string> TITULO_PRINCIPAL = {
"____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ "
" _______ ____ ",
"/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| "
"_ \\ \\ / / ____| _ \\ ",
"\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| "
"| |_) \\ \\ / /| _| | |_) |",
" ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < "
"\\ V / | |___| _ < ",
"|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| "
"\\_\\ \\_/ |_____|_| \\_\\",
"____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ _______ ____ ",
"/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| _ \\ \\ / / ____| _ \\ ",
"\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| | |_) \\ \\ / /| _| | |_) |",
" ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < \\ V / | |___| _ < ",
"|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| \\_\\ \\_/ |_____|_| \\_\\",
"",
"_______________________________________________________________________",
};
void userInterface(const int8_t *menu, Config *config);
void userInterface(const int8_t *menu, vector<string> *mensaje, Config *config);
void showMenu(vector<string> *menu_elems, const int8_t *num_menu,
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, vector<string> *mensaje, Config *config);
void showMenu(vector<string> *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<string> dummy = {""};
userInterface(menu, &dummy, config);
}
void userInterface(const int8_t *num_menu, vector<string> *mensaje,
Config *config) {
void userInterface(const int8_t *num_menu, vector<string> *mensaje, Config *config) {
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_PRINCIPAL: {
vector<string> elem_menu = {"- Iniciar Stress Test", "- Configurar",
"- Salir"};
showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config);
break;
}
case MENU_CONFIGURACION: {
vector<string> 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<string> elem_menu = {"- Configurar todo", "- Número de hilos",
"- Tiempo", "- Menú principal"};
vector<string> textos_init = {"Toda la configuración se escribirá en " + cf::g_configFile, "", ""};
vector<string> elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "};
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);
break;
}
case CONFIGURAR_HILOS: {
/*TODO*/
break;
}
case CONFIGURAR_TODO:
case PRIMER_INICIO: {
case CONFIGURAR_TIEMPO: {
/*TODO*/
break;
}
vector<string> textos_init = {
"Toda la configuración se escribirá en " + cf::g_configFile, "", ""};
vector<string> elemens = {"- Número de hilos\t> ", "- Tiempo\t\t> "};
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 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<string> *menu_elems, const int8_t *num_menu,
vector<string> *mensaje, Config *config) {
void showMenu(vector<string> *menu_elems, const int8_t *num_menu, vector<string> *mensaje, Config *config) {
clear();
if (ui::g_mensaje) {
ui::showCenterMensaje(mensaje, &MENU_ERROR);
}
showMenu(menu_elems, num_menu, 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) {
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<string> *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<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).append(" ");
}
}
@ -221,24 +202,22 @@ void showMenu(vector<string> *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<string> *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));

View File

@ -50,26 +50,20 @@ void ui::showCenterMensaje(vector<string> *mensaje, const int8_t *menu_color) {
g_mensaje = false;
}
void ui::showCentralInputBox(std::vector<std::string> *textos,
const int8_t *num_box,
std::vector<std::string> *elems) {
void ui::showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems) {
ui::showCentralInputBox(textos, num_box, elems, 0);
}
vector<string> ui::showCentralInputBox(vector<string> *textos,
const int8_t *num_box,
vector<string> *elems, long title_size) {
vector<string> ui::showCentralInputBox(vector<string> *textos, const int8_t *num_box, vector<string> *elems, long title_size) {
vector<string> 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<string> ui::showCentralInputBox(vector<string> *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<string> *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);