From 61d8e4a3a864580e4ffaa4cb2d58d088b3808a35 Mon Sep 17 00:00:00 2001 From: muzili <2586850402@qq.com> Date: Tue, 14 May 2024 00:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90bool=E5=80=BC=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GitHubDesktop2Chinese.cpp | 50 ++++++++++++++++-------------- Utils/utils.hpp | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 23 deletions(-) diff --git a/GitHubDesktop2Chinese.cpp b/GitHubDesktop2Chinese.cpp index 65a5f97..34e42c0 100644 --- a/GitHubDesktop2Chinese.cpp +++ b/GitHubDesktop2Chinese.cpp @@ -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; } @@ -478,7 +481,7 @@ void DeveloperOptions() { std::cout << std::endl; int sys = 0; - int sw = 0; //功能开关 + //int sw = 0; //功能开关 spdlog::info("请输入你要修改的功能:"); std::cin >> sys; switch (sys) @@ -486,34 +489,35 @@ void DeveloperOptions() { 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; } } diff --git a/Utils/utils.hpp b/Utils/utils.hpp index ec919a0..808e094 100644 --- a/Utils/utils.hpp +++ b/Utils/utils.hpp @@ -75,6 +75,71 @@ class utils { } } + + static auto ReadUserInput_string(std::vector 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 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; + } + } + } + } };