Implentación de control de entrada. Arregla #2
Signed-off-by: somebody_master <somebody_master@somebodyserver.mooo.com> Se ha implementado el control de entrada por teclado en la configuración para asegurar que se introducen datos adecuados. Se usan expresiones regulares, mediante la librería regex, para conseguir tal efecto.
This commit is contained in:
@ -88,7 +88,7 @@ void userInterface(const int8_t *num_menu, vector<string> *mensaje, Config *conf
|
||||
vector<string> textos_init = {txt::g_confTodo + cf::g_configFile, "", ""};
|
||||
vector<string> elemens = {txt::g_confHilosInput , txt::g_confTiempoInput};
|
||||
ui::showTopTitle(&TITULO_PRINCIPAL);
|
||||
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size());
|
||||
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size(), &MENU_ERROR);
|
||||
Config config2 = Config(atoi(entrada[0].c_str()), entrada[1]);
|
||||
config = &config2;
|
||||
cf::saveConfig(config);
|
||||
@ -99,7 +99,7 @@ void userInterface(const int8_t *num_menu, vector<string> *mensaje, Config *conf
|
||||
case CONFIGURAR_HILOS: {
|
||||
vector<string> textos_init = {txt::g_confHilos + to_string(config->getNumHilos()), "", ""};
|
||||
vector<string> elemens = {txt::g_confHilosInput};
|
||||
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size());
|
||||
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size(), &MENU_ERROR);
|
||||
config->setNumHilos(atoi(entrada[0].c_str()));
|
||||
cf::saveConfig(config);
|
||||
userInterface(&MENU_CONFIGURACION, mensaje, config);
|
||||
@ -109,7 +109,7 @@ void userInterface(const int8_t *num_menu, vector<string> *mensaje, Config *conf
|
||||
case CONFIGURAR_TIEMPO: {
|
||||
vector<string> textos_init = {txt::g_confTiempo + config->getTiempo(), "", ""};
|
||||
vector<string> elemens = {txt::g_confTiempoInput};
|
||||
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size());
|
||||
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size(), &MENU_ERROR);
|
||||
config->setTiempo(entrada[0]);
|
||||
cf::saveConfig(config);
|
||||
userInterface(&MENU_CONFIGURACION, mensaje, config);
|
||||
|
@ -20,6 +20,8 @@ std::string txt::g_confHilos = "";
|
||||
std::string txt::g_confTiempo = "";
|
||||
std::string txt::g_confHilosInput = "";
|
||||
std::string txt::g_confTiempoInput = "";
|
||||
std::string txt::g_confHilosError = "";
|
||||
std::string txt::g_confTiempoError = "";
|
||||
|
||||
void txt::inicializarIdioma(std::string idioma){
|
||||
idioma = idioma.substr(0, idioma.find("_"));
|
||||
@ -43,6 +45,8 @@ void txt::inicializarIdioma(std::string idioma){
|
||||
txt::g_confTiempo = confTiempo;
|
||||
txt::g_confHilosInput = confHilosInput;
|
||||
txt::g_confTiempoInput = confTiempoInput;
|
||||
txt::g_confHilosError = confHilosError;
|
||||
txt::g_confTiempoError = confTiempoError;
|
||||
}else if(idioma == "es"){
|
||||
using namespace es;
|
||||
txt::g_noColor = noColor;
|
||||
@ -62,6 +66,8 @@ void txt::inicializarIdioma(std::string idioma){
|
||||
txt::g_confTiempo = confTiempo;
|
||||
txt::g_confHilosInput = confHilosInput;
|
||||
txt::g_confTiempoInput = confTiempoInput;
|
||||
txt::g_confHilosError = confHilosError;
|
||||
txt::g_confTiempoError = confTiempoError;
|
||||
}else if(idioma == "en"){
|
||||
using namespace en;
|
||||
txt::g_noColor = noColor;
|
||||
@ -81,6 +87,8 @@ void txt::inicializarIdioma(std::string idioma){
|
||||
txt::g_confTiempo = confTiempo;
|
||||
txt::g_confHilosInput = confHilosInput;
|
||||
txt::g_confTiempoInput = confTiempoInput;
|
||||
txt::g_confHilosError = confHilosError;
|
||||
txt::g_confTiempoError = confTiempoError;
|
||||
}else{
|
||||
using namespace en;
|
||||
txt::g_noColor = noColor;
|
||||
@ -100,6 +108,8 @@ void txt::inicializarIdioma(std::string idioma){
|
||||
txt::g_confTiempo = confTiempo;
|
||||
txt::g_confHilosInput = confHilosInput;
|
||||
txt::g_confTiempoInput = confTiempoInput;
|
||||
txt::g_confHilosError = confHilosError;
|
||||
txt::g_confTiempoError = confTiempoError;
|
||||
}
|
||||
|
||||
}
|
||||
|
35
src/ui.cpp
35
src/ui.cpp
@ -1,6 +1,7 @@
|
||||
#include "ui.hpp"
|
||||
#include "configFile.hpp"
|
||||
#include "textos.hpp"
|
||||
#include <regex>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -51,11 +52,11 @@ 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) {
|
||||
ui::showCentralInputBox(textos, num_box, elems, 0);
|
||||
vector<string> ui::showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems, const int8_t *color_error) {
|
||||
return ui::showCentralInputBox(textos, num_box, elems, 0, color_error);
|
||||
}
|
||||
|
||||
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, const int8_t *color_error) {
|
||||
vector<string> entrada;
|
||||
int start_ver_window = 1;
|
||||
|
||||
@ -75,12 +76,36 @@ vector<string> ui::showCentralInputBox(vector<string> *textos, const int8_t *num
|
||||
echo();
|
||||
nocbreak();
|
||||
curs_set(1);
|
||||
char input[10];
|
||||
string cleaner;
|
||||
|
||||
for (int i = 0; i < elems->size(); i++) {
|
||||
cleaner = elems->at(i) + " ";
|
||||
mvwprintw(centralBOX, i + 1 + textos->size(), 1, elems->at(i).c_str());
|
||||
wrefresh(centralBOX);
|
||||
char input[10];
|
||||
wgetnstr(centralBOX, input, 10);
|
||||
entrada.push_back(input);
|
||||
if (elems->at(i) == txt::g_confHilosInput){
|
||||
regex integer_expr("(\\+|-)?[[:digit:]]+");
|
||||
if(regex_match(input, integer_expr)){
|
||||
entrada.push_back(input);
|
||||
} else {
|
||||
mvwprintw(centralBOX, i + 1 + textos->size(), 1, cleaner.c_str());
|
||||
wattron(centralBOX, COLOR_PAIR(*color_error));
|
||||
mvwprintw(centralBOX, i + textos->size(), 1, txt::g_confHilosError.c_str());
|
||||
wattroff(centralBOX, COLOR_PAIR(*color_error));
|
||||
i--;
|
||||
}
|
||||
} else if(elems->at(i) == txt::g_confTiempoInput){
|
||||
if(regex_match(input,regex("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"))){
|
||||
entrada.push_back(input);
|
||||
} else {
|
||||
mvwprintw(centralBOX, i + 1 + textos->size(), 1, cleaner.c_str());
|
||||
wattron(centralBOX, COLOR_PAIR(*color_error));
|
||||
mvwprintw(centralBOX, i + textos->size(), 1, txt::g_confTiempoError.c_str());
|
||||
wattroff(centralBOX, COLOR_PAIR(*color_error));
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
curs_set(0);
|
||||
noecho();
|
||||
|
Reference in New Issue
Block a user