Skip to content

Commit

Permalink
集成bool值输入功能,优化输入效果
Browse files Browse the repository at this point in the history
  • Loading branch information
cngege committed May 13, 2024
1 parent b21dc18 commit 61d8e4a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 23 deletions.
50 changes: 27 additions & 23 deletions GitHubDesktop2Chinese.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ int main(int argc, char* argv[])
spdlog::info("远程读取成功");
}
else {
spdlog::warn("远程获取失败: {}, 已创建框架,请先编辑创建翻译映射", "localization.json");
std::ofstream io(LocalizationJSON);
io << std::setw(4) << localization << std::endl;
io.close();
spdlog::warn("远程获取失败 - 是否创建json框架,手动编辑汉化映射");
if (utils::ReadUserInput_bool({ "n","y" }, 0)) {
spdlog::warn("远程获取失败: {}, 已创建框架,请先编辑创建翻译映射", "localization.json");
std::ofstream io(LocalizationJSON);
io << std::setw(4) << localization << std::endl;
io.close();
}
PAUSE;
return 0;
}
Expand Down Expand Up @@ -478,42 +481,43 @@ void DeveloperOptions() {
std::cout << std::endl;

int sys = 0;
int sw = 0; //功能开关
//int sw = 0; //功能开关
spdlog::info("请输入你要修改的功能:");
std::cin >> sys;
switch (sys)
{
case 0:
return;
case 1:
spdlog::info("输入你要切换的状态(0关 1开):");
std::cin >> sw;
_debug_error_check_mode_main = (bool)sw;
//spdlog::info("输入你要切换的状态(0关 1开):");
//std::cin >> sw;
//_debug_error_check_mode_main = (bool)sw;
_debug_error_check_mode_main = utils::ReadUserInput_bool({ "false", "true" });
break;
case 2:
spdlog::info("输入你要切换的状态(0关 1开):");
std::cin >> sw;
_debug_error_check_mode_renderer = (bool)sw;
//spdlog::info("输入你要切换的状态(0关 1开):");
//std::cin >> sw;
_debug_error_check_mode_renderer = utils::ReadUserInput_bool({ "false", "true" });
break;
case 3:
spdlog::info("输入你要切换的状态(0关 1开):");
std::cin >> sw;
_debug_invalid_check_mode = (bool)sw;
//spdlog::info("输入你要切换的状态(0关 1开):");
//std::cin >> sw;
_debug_invalid_check_mode = utils::ReadUserInput_bool({ "false", "true" });
break;
case 4:
spdlog::info("输入你要切换的状态(0关 1开):");
std::cin >> sw;
_debug_no_replace_res = (bool)sw;
//spdlog::info("输入你要切换的状态(0关 1开):");
//std::cin >> sw;
_debug_no_replace_res = utils::ReadUserInput_bool({ "false", "true" });
break;
case 5:
spdlog::info("输入你要切换的状态(0关 1开):");
std::cin >> sw;
_debug_translation_from_bak = (bool)sw;
//spdlog::info("输入你要切换的状态(0关 1开):");
//std::cin >> sw;
_debug_translation_from_bak = utils::ReadUserInput_bool({ "false", "true" });
break;
case 6:
spdlog::info("输入你要切换的状态(0关 1开):");
std::cin >> sw;
_debug_dev_replace = (bool)sw;
//spdlog::info("输入你要切换的状态(0关 1开):");
//std::cin >> sw;
_debug_dev_replace = utils::ReadUserInput_bool({ "false", "true" });
break;
}
}
Expand Down
65 changes: 65 additions & 0 deletions Utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,71 @@ class utils {
}
}


static auto ReadUserInput_string(std::vector<std::string> input, int defaultval = -1) -> std::string {

while (true)
{
// 输出提示
//if (defaultval == -1)
// spdlog::info("请输入一个表示字符串的值({})", input);
//else
// spdlog::info("请输入一个表示字符串的值({} 默认 {})", input, input[defaultval]);

std::string instr;
std::cin >> instr;

for (std::string& item : input)
{
if (item == instr) {
return item;
}
}

if (defaultval == -1) {
// 循环
continue;
}
else {
return input[defaultval];
}
}
}

static auto ReadUserInput_bool(std::vector<std::string> input = {"false", "true"}, int defaultval = -1) -> bool {
if (input.size() != 2) throw std::exception("读取 bool 类型值时 input 数组长度必须为两个");
if (defaultval > (int)input.size() - 1 || defaultval < -1) throw std::exception(std::format("defaultval 必须能够指向 input数组,或者为 -1, defaultval:{}" , defaultval).c_str());
while (true)
{
// 输出提示
if(defaultval == -1)
spdlog::info("请输入一个表示bool的值({}/{})", input[0], input[1]);
else
spdlog::info("请输入一个表示bool的值({}/{} 默认 {})", input[0], input[1], input[defaultval]);

std::string instr;
std::cin >> instr;

if (instr == input[0]) {
return false;
}
else if (instr == input[1]) {
return true;
}
else {
if (defaultval == -1) {
// 循环
continue;
}
else if (defaultval == 0){
return false;
}
else {
return true;
}
}
}
}
};


Expand Down

0 comments on commit 61d8e4a

Please sign in to comment.