From 3b754f0434554344a0f0adc2ef7b2d7250c9a055 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sat, 11 Feb 2023 22:23:19 +0800 Subject: [PATCH] add color term support and fix encoding error --- LibCmo/LibCmo.vcxproj | 4 ++++ Unvirt/ColorfulTerminal.cpp | 42 +++++++++++++++++++++++++++++++++++ Unvirt/ColorfulTerminal.hpp | 31 ++++++++++++++++++++++++++ Unvirt/Unvirt.cpp | 6 +++++ Unvirt/Unvirt.vcxproj | 6 +++++ Unvirt/Unvirt.vcxproj.filters | 6 +++++ 6 files changed, 95 insertions(+) create mode 100644 Unvirt/ColorfulTerminal.cpp create mode 100644 Unvirt/ColorfulTerminal.hpp diff --git a/LibCmo/LibCmo.vcxproj b/LibCmo/LibCmo.vcxproj index 9b39697..b0280a4 100644 --- a/LibCmo/LibCmo.vcxproj +++ b/LibCmo/LibCmo.vcxproj @@ -102,6 +102,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -119,6 +120,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -136,6 +138,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -155,6 +158,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console diff --git a/Unvirt/ColorfulTerminal.cpp b/Unvirt/ColorfulTerminal.cpp new file mode 100644 index 0000000..34e849e --- /dev/null +++ b/Unvirt/ColorfulTerminal.cpp @@ -0,0 +1,42 @@ +#include "ColorfulTerminal.hpp" +#include + +#if defined(LIBCMO_OS_WIN32) +#include +#include +#include +#endif + +namespace Unvirt { + namespace ColorfulTerminal { + + // all of these functions only works on Windows platform + // due to shitty Windows implementations. + + bool EnsureTerminalColor(void) { +#if defined(LIBCMO_OS_WIN32) + if (_isatty(_fileno(stdout))) { + HANDLE h_output; + DWORD dw_mode; + + h_output = (HANDLE)_get_osfhandle(_fileno(stdout)); + if (!GetConsoleMode(h_output, &dw_mode)) return false; + if (!SetConsoleMode(h_output, dw_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return false; + + return true; + } else return false; +#else + return true; +#endif + } + + bool EnsureTerminalEncoding(void) { +#if defined(LIBCMO_OS_WIN32) + if (!SetConsoleCP(CP_UTF8)) return false; + if (!SetConsoleOutputCP(CP_UTF8)) return false; +#endif + return true; + } + + } +} \ No newline at end of file diff --git a/Unvirt/ColorfulTerminal.hpp b/Unvirt/ColorfulTerminal.hpp new file mode 100644 index 0000000..914290f --- /dev/null +++ b/Unvirt/ColorfulTerminal.hpp @@ -0,0 +1,31 @@ +#pragma once + +namespace Unvirt{ + namespace ColorfulTerminal { + +#define UNVIRT_REMOVE_PARENS_IMPL(...) __VA_ARGS__ +#define UNVIRT_REMOVE_PARENS(T) UNVIRT_REMOVE_PARENS_IMPL T + +#define UNVIRT_TERMCOL_BLACK(T) "\033[30m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_RED(T) "\033[31m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_GREEN(T) "\033[32m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_YELLOW(T) "\033[33m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_BLUE(T) "\033[34m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_MAGENTA(T) "\033[35m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_CYAN(T) "\033[36m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_WHITE(T) "\033[37m" UNVIRT_REMOVE_PARENS(T) "\033[0m" + +#define UNVIRT_TERMCOL_LIGHT_BLACK(T) "\033[90m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_RED(T) "\033[91m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_GREEN(T) "\033[92m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_YELLOW(T) "\033[93m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_BLUE(T) "\033[94m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_MAGENTA(T) "\033[95m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_CYAN(T) "\033[96m" UNVIRT_REMOVE_PARENS(T) "\033[0m" +#define UNVIRT_TERMCOL_LIGHT_WHITE(T) "\033[97m" UNVIRT_REMOVE_PARENS(T) "\033[0m" + + bool EnsureTerminalColor(void); + bool EnsureTerminalEncoding(void); + + } +} diff --git a/Unvirt/Unvirt.cpp b/Unvirt/Unvirt.cpp index 8b0236d..ea2d90b 100644 --- a/Unvirt/Unvirt.cpp +++ b/Unvirt/Unvirt.cpp @@ -1,11 +1,17 @@ #include "AccessibleValue.hpp" +#include "ColorfulTerminal.hpp" #include "VTStruct.hpp" #include int main(int argc, char* argv[]) { + Unvirt::ColorfulTerminal::EnsureTerminalColor(); + Unvirt::ColorfulTerminal::EnsureTerminalEncoding(); + LibCmo::Utils::VirtoolsContext vtctx; LibCmo::CKFile vtfile(vtctx); vtfile.Load("Language.old.nmo", LibCmo::CK_LOAD_FLAGS::CK_LOAD_DEFAULT); + printf(UNVIRT_TERMCOL_LIGHT_YELLOW(("Hello, %s")), u8"㴮!"); + return 0; } diff --git a/Unvirt/Unvirt.vcxproj b/Unvirt/Unvirt.vcxproj index 233d63e..47ded24 100644 --- a/Unvirt/Unvirt.vcxproj +++ b/Unvirt/Unvirt.vcxproj @@ -102,6 +102,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);../LibCmo;$(SQLITE_HEADER_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -120,6 +121,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);../LibCmo;$(SQLITE_HEADER_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -138,6 +140,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);../LibCmo;$(SQLITE_HEADER_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -156,6 +159,7 @@ true $(BOOST_INCLUDE_PATH);$(ZLIB_PATH);../LibCmo;$(SQLITE_HEADER_PATH);%(AdditionalIncludeDirectories) stdcpp20 + /utf-8 %(AdditionalOptions) Console @@ -168,11 +172,13 @@ + + diff --git a/Unvirt/Unvirt.vcxproj.filters b/Unvirt/Unvirt.vcxproj.filters index a0d7f51..f1be490 100644 --- a/Unvirt/Unvirt.vcxproj.filters +++ b/Unvirt/Unvirt.vcxproj.filters @@ -24,6 +24,9 @@ Sources + + Sources + @@ -32,5 +35,8 @@ Headers + + Headers + \ No newline at end of file