From 17b6aa2fb72bb9c8c550f6efb48e9bb8596f4afc Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 25 May 2020 03:50:37 -0400 Subject: [PATCH] restore the original console font if it changed --- src/main.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index a8825a8..83c0ef0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -165,8 +165,78 @@ void add_tasks() .add_task("modorganizer"); } + +// 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_)) + { + const auto e = GetLastError(); + std::wcerr + << L"failed to get console font, " + << utf8_to_utf16(error_message(e)) << L"\n"; + + return; + } + + restore_ = true; + } + + ~font_restorer() + { + if (!restore_) + return; + + CONSOLE_FONT_INFOEX now = {}; + now.cbSize = sizeof(now); + + if (!GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &now)) + { + const auto e = GetLastError(); + std::wcerr + << L"failed to get console font, " + << utf8_to_utf16(error_message(e)) << L"\n"; + + return; + } + + if (std::wcsncmp(old_.FaceName, now.FaceName, LF_FACESIZE) != 0) + restore(); + } + + void restore() + { + if (!::SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &old_)) + { + const auto e = GetLastError(); + std::wcerr + << L"failed to set console font, " + << utf8_to_utf16(error_message(e)) << L"\n"; + + return; + } + } + +private: + CONSOLE_FONT_INFOEX old_; + bool restore_; +}; + + int run(const std::vector& args) { + font_restorer fr; + add_tasks(); try