diff --git a/src/main.cpp b/src/main.cpp index ab2101c..898e62f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,77 +10,6 @@ namespace mob { -std::shared_ptr handle_command_line(const std::vector& args) -{ - auto help = std::make_shared(); - auto build = std::make_shared(); - - std::vector> commands = - { - help, - std::make_unique(), - std::make_unique(), - build, - std::make_unique(), - std::make_unique(), - std::make_unique(), - std::make_unique(), - std::make_unique(), - std::make_unique() - }; - - help->set_commands(commands); - - std::vector command_groups; - for (auto& c : commands) - command_groups.push_back(c->group()); - - clipp::group all_groups; - all_groups.scoped(false); - all_groups.exclusive(true); - for (auto& c : command_groups) - all_groups.push_back(c); - -#pragma warning(suppress: 4548) - auto cli = (all_groups, command::common_options_group()); - - auto pr = clipp::parse(args, cli); - - if (!pr) - { - // if a command was picked, show its help instead of the main one - for (auto&& c : commands) - { - if (c->picked()) - { - c->force_help(); - return std::move(c); - } - } - - // bad command line - help->force_exit_code(1); - return help; - } - - - for (auto&& c : commands) - { - if (c->picked()) - return std::move(c); - } - - return {}; -} - - -BOOL WINAPI signal_handler(DWORD) noexcept -{ - gcx().debug(context::generic, "caught sigint"); - task::interrupt_all(); - return TRUE; -} - void add_tasks() { add_task(false) @@ -184,48 +113,76 @@ void add_tasks() add_task(); } - -// see https://github.com/isanae/mob/issues/4 -// -// this restores the original console font if it changed -// -class font_restorer +std::shared_ptr handle_command_line(const std::vector& args) { -public: - font_restorer() - : restore_(false) - { - std::memset(&old_, 0, sizeof(old_)); - old_.cbSize = sizeof(old_); + auto help = std::make_shared(); + auto build = std::make_shared(); - if (GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &old_)) - restore_ = true; + std::vector> commands = + { + help, + std::make_unique(), + std::make_unique(), + build, + std::make_unique(), + std::make_unique(), + std::make_unique(), + std::make_unique(), + std::make_unique(), + std::make_unique() + }; + + help->set_commands(commands); + + std::vector command_groups; + for (auto& c : commands) + command_groups.push_back(c->group()); + + clipp::group all_groups; + all_groups.scoped(false); + all_groups.exclusive(true); + for (auto& c : command_groups) + all_groups.push_back(c); + +#pragma warning(suppress: 4548) + auto cli = (all_groups, command::common_options_group()); + + auto pr = clipp::parse(args, cli); + + if (!pr) + { + // if a command was picked, show its help instead of the main one + for (auto&& c : commands) + { + if (c->picked()) + { + c->force_help(); + return std::move(c); + } + } + + // bad command line + help->force_exit_code(1); + return help; } - ~font_restorer() + + for (auto&& c : commands) { - if (!restore_) - return; - - CONSOLE_FONT_INFOEX now = {}; - now.cbSize = sizeof(now); - - if (!GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &now)) - return; - - if (std::wcsncmp(old_.FaceName, now.FaceName, LF_FACESIZE) != 0) - restore(); + if (c->picked()) + return std::move(c); } - void restore() - { - ::SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &old_); - } + return {}; +} -private: - CONSOLE_FONT_INFOEX old_; - bool restore_; -}; + +BOOL WINAPI signal_handler(DWORD) noexcept +{ + gcx().debug(context::generic, "caught sigint"); + task::interrupt_all(); + return TRUE; +} void set_sigint_handler() { diff --git a/src/utility.h b/src/utility.h index 6bf9b58..b56ea3d 100644 --- a/src/utility.h +++ b/src/utility.h @@ -620,4 +620,47 @@ private: bool try_add(fun thread_fun); }; + +// see https://github.com/isanae/mob/issues/4 +// +// this restores the original console font if it changed +// +class font_restorer +{ +public: + font_restorer() + : restore_(false) + { + std::memset(&old_, 0, sizeof(old_)); + old_.cbSize = sizeof(old_); + + if (GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &old_)) + restore_ = true; + } + + ~font_restorer() + { + if (!restore_) + return; + + CONSOLE_FONT_INFOEX now = {}; + now.cbSize = sizeof(now); + + if (!GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &now)) + return; + + if (std::wcsncmp(old_.FaceName, now.FaceName, LF_FACESIZE) != 0) + restore(); + } + + void restore() + { + ::SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &old_); + } + +private: + CONSOLE_FONT_INFOEX old_; + bool restore_; +}; + } // namespace