From 62ae2d17d13c4bacb39346ced8caeaeb985de56e Mon Sep 17 00:00:00 2001 From: Alex Alabuzhev Date: Sun, 15 Sep 2024 14:01:46 +0100 Subject: [PATCH] Refactoring --- far/changelog | 5 +++ far/color_picker_256.cpp | 2 +- far/colormix.cpp | 13 +++--- far/console.cpp | 35 ++++++++++------ far/exception.hpp | 4 +- far/hilight.cpp | 19 +++++---- far/palette.hpp | 91 +++++++++++++++++++++++++--------------- far/platform.com.hpp | 2 +- far/platform.reg.cpp | 4 +- far/platform.reg.hpp | 3 +- far/scrbuf.cpp | 10 ++--- far/scrsaver.cpp | 14 +++---- far/vbuild.m4 | 2 +- 13 files changed, 123 insertions(+), 81 deletions(-) diff --git a/far/changelog b/far/changelog index 95fb398b18..7e73edd2d2 100644 --- a/far/changelog +++ b/far/changelog @@ -1,3 +1,8 @@ +-------------------------------------------------------------------------------- +drkns 2024-09-15 14:01:29+01:00 - build 6370 + +1. Refactoring. + -------------------------------------------------------------------------------- drkns 2024-09-11 17:11:09+01:00 - build 6369 diff --git a/far/color_picker_256.cpp b/far/color_picker_256.cpp index 370ea9a0f3..97519f4487 100644 --- a/far/color_picker_256.cpp +++ b/far/color_picker_256.cpp @@ -70,7 +70,7 @@ static FarColor Console256ColorToFarColor(index_color_256 const Color) { return { - FCF_FG_INDEX | FCF_BG_INDEX, + FCF_INDEXMASK, { colors::opaque(colors::index_bits(Color.ForegroundIndex)) }, { colors::opaque(colors::index_bits(Color.BackgroundIndex)) } }; diff --git a/far/colormix.cpp b/far/colormix.cpp index cab6b6b556..9f87cd44db 100644 --- a/far/colormix.cpp +++ b/far/colormix.cpp @@ -535,10 +535,10 @@ namespace colors static WORD emulate_styles(index_color_16 Color, FARCOLORFLAGS const Flags) { if (Flags & FCF_FG_BOLD) - Color.ForegroundIndex |= FOREGROUND_INTENSITY; + Color.ForegroundIndex |= F_INTENSE; if (Flags & FCF_FG_FAINT) - Color.ForegroundIndex &= ~FOREGROUND_INTENSITY; + Color.ForegroundIndex &= ~F_INTENSE; // COMMON_LVB_REVERSE_VIDEO is a better way, but it only works on Windows 10. // Manual swap works everywhere. @@ -638,7 +638,7 @@ WORD FarColorToConsoleColor(const FarColor& Color) { // oops, unreadable // since background is more pronounced we adjust the foreground only - flags::invert(Result.ForegroundIndex, FOREGROUND_INTENSITY); + flags::invert(Result.ForegroundIndex, C_INTENSE); } Last.emplace(Color, Result); @@ -676,7 +676,7 @@ FarColor NtColorToFarColor(WORD Color) return { - FCF_FG_INDEX | FCF_BG_INDEX | FCF_INHERIT_STYLE | (Color & FCF_RAWATTR_MASK), + FCF_INDEXMASK | FCF_INHERIT_STYLE | (Color & FCF_RAWATTR_MASK), { opaque(Color16.ForegroundIndex) }, { opaque(Color16.BackgroundIndex) } }; @@ -842,8 +842,9 @@ unsigned long long ColorStringToFlags(string_view const Flags) static FarColor s_ResolvedDefaultColor { FCF_INDEXMASK, - { opaque(F_LIGHTGRAY) }, - { opaque(F_BLACK) }, + { opaque(C_LIGHTGRAY) }, + { opaque(C_BLACK) }, + { transparent(C_BLACK) } }; COLORREF resolve_default(COLORREF Color, bool IsForeground) diff --git a/far/console.cpp b/far/console.cpp index 7dead2680a..a9a91299af 100644 --- a/far/console.cpp +++ b/far/console.cpp @@ -1480,7 +1480,7 @@ namespace console_detail if (colors::is_default(Color.Value)) append(Str, Mapping.Default); else if (const auto Index = vt_color_index(colors::index_value(Color.Value)); Index < colors::index::nt_size && Mapping.PreferBasicIndex) - append(Str, Color.Value & FOREGROUND_INTENSITY? Mapping.Intense : Mapping.Normal, static_cast(L'0' + (Index & 0b111))); + append(Str, Color.Value & C_INTENSE? Mapping.Intense : Mapping.Normal, static_cast(L'0' + (Index & 0b111))); else far::format_to(Str, L"{1}{0}5{0}{2}"sv, Mapping.Separator, Mapping.ExtendedColour, Index); } @@ -1530,6 +1530,15 @@ namespace console_detail UnderlineStyle(Color.GetUnderline()), UnderlineColor(single_color::underline(Color)) { + if (Color.Flags & COMMON_LVB_GRID_HORIZONTAL) + Style |= FCF_FG_OVERLINE; + + if (Color.Flags & COMMON_LVB_REVERSE_VIDEO) + Style |= FCF_FG_INVERSE; + + if (Color.Flags & COMMON_LVB_UNDERSCORE && UnderlineStyle == UNDERLINE_STYLE::UNDERLINE_NONE) + UnderlineStyle = UNDERLINE_STYLE::UNDERLINE_SINGLE; + if ( // If there's no underline, no point in emitting its color UnderlineStyle == UNDERLINE_NONE || @@ -3191,8 +3200,8 @@ TEST_CASE("console.vt_sequence") { FAR_CHAR_INFO Buffer[]{ def, def, def, def }; - Buffer[1].Attributes.BackgroundColor = colors::opaque(F_MAGENTA); - Buffer[2].Attributes.ForegroundColor = colors::opaque(F_GREEN); + Buffer[1].Attributes.BackgroundColor = colors::opaque(C_MAGENTA); + Buffer[2].Attributes.ForegroundColor = colors::opaque(C_GREEN); Buffer[3].Attributes.Flags |= FCF_FG_BOLD; check(Buffer, VTSTR( " " @@ -3205,8 +3214,8 @@ TEST_CASE("console.vt_sequence") { FAR_CHAR_INFO Buffer[]{ def, def, def }; Buffer[0].Attributes.Flags |= FCF_FG_BOLD; - Buffer[0].Attributes.BackgroundColor = colors::opaque(F_BLUE); - Buffer[0].Attributes.ForegroundColor = colors::opaque(F_LIGHTGREEN); + Buffer[0].Attributes.BackgroundColor = colors::opaque(C_BLUE); + Buffer[0].Attributes.ForegroundColor = colors::opaque(C_LIGHTGREEN); Buffer[1] = Buffer[0]; @@ -3221,8 +3230,8 @@ TEST_CASE("console.vt_sequence") { FAR_CHAR_INFO Buffer[]{ def, def, def, def, def }; - Buffer[0].Attributes.BackgroundColor = colors::opaque(F_BLUE); - Buffer[0].Attributes.ForegroundColor = colors::opaque(F_YELLOW); + Buffer[0].Attributes.BackgroundColor = colors::opaque(C_BLUE); + Buffer[0].Attributes.ForegroundColor = colors::opaque(C_YELLOW); Buffer[1] = Buffer[0]; Buffer[1].Attributes.SetUnderline(UNDERLINE_CURLY); @@ -3230,13 +3239,13 @@ TEST_CASE("console.vt_sequence") Buffer[1].Attributes.SetUnderlineIndex(Buffer[1].Attributes.IsFgIndex()); Buffer[2] = Buffer[1]; - Buffer[2].Attributes.UnderlineColor = colors::opaque(F_RED); + Buffer[2].Attributes.UnderlineColor = colors::opaque(C_RED); Buffer[3] = Buffer[1]; Buffer[4] = Buffer[3]; - Buffer[4].Attributes.ForegroundColor = colors::opaque(F_MAGENTA); - Buffer[4].Attributes.UnderlineColor = colors::opaque(F_MAGENTA); + Buffer[4].Attributes.ForegroundColor = colors::opaque(C_MAGENTA); + Buffer[4].Attributes.UnderlineColor = colors::opaque(C_MAGENTA); check(Buffer, VTSTR( @@ -3251,13 +3260,13 @@ TEST_CASE("console.vt_sequence") { FAR_CHAR_INFO Buffer[]{ def, def, def, def, def, def }; - Buffer[0].Attributes.BackgroundColor = colors::opaque(F_BLUE); - Buffer[0].Attributes.ForegroundColor = colors::opaque(F_LIGHTGREEN); + Buffer[0].Attributes.BackgroundColor = colors::opaque(C_BLUE); + Buffer[0].Attributes.ForegroundColor = colors::opaque(C_LIGHTGREEN); Buffer[0].Attributes.SetUnderline(UNDERLINE_DOUBLE); Buffer[1] = Buffer[0]; Buffer[1].Attributes.SetUnderline(UNDERLINE_CURLY); - Buffer[1].Attributes.UnderlineColor = colors::opaque(F_YELLOW); + Buffer[1].Attributes.UnderlineColor = colors::opaque(C_YELLOW); Buffer[2] = Buffer[1]; Buffer[2].Attributes.SetUnderline(UNDERLINE_DOT); diff --git a/far/exception.hpp b/far/exception.hpp index 24041eca60..56ee95b6a5 100644 --- a/far/exception.hpp +++ b/far/exception.hpp @@ -140,8 +140,8 @@ class far_exception: public detail::far_std_exception class far_known_exception final: public far_exception { public: - explicit far_known_exception(string_view const Message): - far_exception(Message, false, {}) + explicit far_known_exception(string_view const Message, source_location const& Location = source_location::current()): + far_exception(Message, false, Location) { } }; diff --git a/far/hilight.cpp b/far/hilight.cpp index 1b982e43ce..dd7e0a77b9 100644 --- a/far/hilight.cpp +++ b/far/hilight.cpp @@ -78,9 +78,10 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. static const FarColor DefaultColor { - FCF_FG_INDEX | FCF_BG_INDEX | FCF_INHERIT_STYLE, - {colors::transparent(F_BLACK)}, - {colors::transparent(F_BLACK)} + FCF_INDEXMASK | FCF_INHERIT_STYLE, + {colors::transparent(C_BLACK)}, + {colors::transparent(C_BLACK)}, + {colors::transparent(C_BLACK)} }; namespace names @@ -167,12 +168,12 @@ static void SetHighlighting(bool DeleteOld, HierarchicalConfig& cfg) } DefaultHighlighting[] { - { {}, FILE_ATTRIBUTE_HIDDEN, MakeFarColor(F_CYAN), MakeFarColor(F_DARKGRAY) }, - { {}, FILE_ATTRIBUTE_SYSTEM, MakeFarColor(F_CYAN), MakeFarColor(F_DARKGRAY) }, - { {}, FILE_ATTRIBUTE_DIRECTORY, MakeFarColor(F_WHITE), MakeFarColor(F_WHITE) }, - { L""sv, 0, MakeFarColor(F_LIGHTGREEN), MakeFarColor(F_LIGHTGREEN) }, - { L""sv, 0, MakeFarColor(F_LIGHTMAGENTA), MakeFarColor(F_LIGHTMAGENTA) }, - { L""sv, 0, MakeFarColor(F_BROWN), MakeFarColor(F_BROWN) }, + { {}, FILE_ATTRIBUTE_HIDDEN, MakeFarColor(C_CYAN), MakeFarColor(C_DARKGRAY) }, + { {}, FILE_ATTRIBUTE_SYSTEM, MakeFarColor(C_CYAN), MakeFarColor(C_DARKGRAY) }, + { {}, FILE_ATTRIBUTE_DIRECTORY, MakeFarColor(C_WHITE), MakeFarColor(C_WHITE) }, + { L""sv, 0, MakeFarColor(C_LIGHTGREEN), MakeFarColor(C_LIGHTGREEN) }, + { L""sv, 0, MakeFarColor(C_LIGHTMAGENTA), MakeFarColor(C_LIGHTMAGENTA) }, + { L""sv, 0, MakeFarColor(C_BROWN), MakeFarColor(C_BROWN) }, }; const auto root = cfg.CreateKey(cfg.root_key, names::Highlight); diff --git a/far/palette.hpp b/far/palette.hpp index a4a3a0b38d..98f4273b93 100644 --- a/far/palette.hpp +++ b/far/palette.hpp @@ -79,41 +79,66 @@ namespace detail } } +// Use C_* notation in FarColor and other types where Foreground and Background are different fields enum { - F_BLACK = detail::fg(detail::black), - F_BLUE = detail::fg(detail::blue), - F_GREEN = detail::fg(detail::green), - F_CYAN = detail::fg(detail::cyan), - F_RED = detail::fg(detail::red), - F_MAGENTA = detail::fg(detail::magenta), - F_BROWN = detail::fg(detail::yellow), - F_LIGHTGRAY = detail::fg(detail::white), - F_DARKGRAY = detail::fg(detail::channel::intense | detail::black), - F_LIGHTBLUE = detail::fg(detail::channel::intense | detail::blue), - F_LIGHTGREEN = detail::fg(detail::channel::intense | detail::green), - F_LIGHTCYAN = detail::fg(detail::channel::intense | detail::cyan), - F_LIGHTRED = detail::fg(detail::channel::intense | detail::red), - F_LIGHTMAGENTA = detail::fg(detail::channel::intense | detail::magenta), - F_YELLOW = detail::fg(detail::channel::intense | detail::yellow), - F_WHITE = detail::fg(detail::channel::intense | detail::white), - - B_BLACK = detail::bg(detail::black), - B_BLUE = detail::bg(detail::blue), - B_GREEN = detail::bg(detail::green), - B_CYAN = detail::bg(detail::cyan), - B_RED = detail::bg(detail::red), - B_MAGENTA = detail::bg(detail::magenta), - B_BROWN = detail::bg(detail::yellow), - B_LIGHTGRAY = detail::bg(detail::white), - B_DARKGRAY = detail::bg(detail::channel::intense | detail::black), - B_LIGHTBLUE = detail::bg(detail::channel::intense | detail::blue), - B_LIGHTGREEN = detail::bg(detail::channel::intense | detail::green), - B_LIGHTCYAN = detail::bg(detail::channel::intense | detail::cyan), - B_LIGHTRED = detail::bg(detail::channel::intense | detail::red), - B_LIGHTMAGENTA = detail::bg(detail::channel::intense | detail::magenta), - B_YELLOW = detail::bg(detail::channel::intense | detail::yellow), - B_WHITE = detail::bg(detail::channel::intense | detail::white), + C_BLACK = detail::black, + C_BLUE = detail::blue, + C_GREEN = detail::green, + C_CYAN = detail::cyan, + C_RED = detail::red, + C_MAGENTA = detail::magenta, + C_BROWN = detail::yellow, + C_LIGHTGRAY = detail::white, + C_INTENSE = detail::channel::intense, + C_DARKGRAY = detail::channel::intense | detail::black, + C_LIGHTBLUE = detail::channel::intense | detail::blue, + C_LIGHTGREEN = detail::channel::intense | detail::green, + C_LIGHTCYAN = detail::channel::intense | detail::cyan, + C_LIGHTRED = detail::channel::intense | detail::red, + C_LIGHTMAGENTA = detail::channel::intense | detail::magenta, + C_YELLOW = detail::channel::intense | detail::yellow, + C_WHITE = detail::channel::intense | detail::white, +}; + +// Use F_* and B_* notations in CHAR_INFO.Attributes and other types where Foreground and Background are bits in the same field +enum +{ + F_BLACK = detail::fg(C_BLACK), + F_BLUE = detail::fg(C_BLUE), + F_GREEN = detail::fg(C_GREEN), + F_CYAN = detail::fg(C_CYAN), + F_RED = detail::fg(C_RED), + F_MAGENTA = detail::fg(C_MAGENTA), + F_BROWN = detail::fg(C_BROWN), + F_LIGHTGRAY = detail::fg(C_LIGHTGRAY), + F_INTENSE = detail::fg(C_INTENSE), + F_DARKGRAY = detail::fg(C_DARKGRAY), + F_LIGHTBLUE = detail::fg(C_LIGHTBLUE), + F_LIGHTGREEN = detail::fg(C_LIGHTGREEN), + F_LIGHTCYAN = detail::fg(C_LIGHTCYAN), + F_LIGHTRED = detail::fg(C_LIGHTRED), + F_LIGHTMAGENTA = detail::fg(C_LIGHTMAGENTA), + F_YELLOW = detail::fg(C_YELLOW), + F_WHITE = detail::fg(C_WHITE), + + B_BLACK = detail::bg(C_BLACK), + B_BLUE = detail::bg(C_BLUE), + B_GREEN = detail::bg(C_GREEN), + B_CYAN = detail::bg(C_CYAN), + B_RED = detail::bg(C_RED), + B_MAGENTA = detail::bg(C_MAGENTA), + B_BROWN = detail::bg(C_BROWN), + B_LIGHTGRAY = detail::bg(C_LIGHTGRAY), + B_INTENSE = detail::bg(C_INTENSE), + B_DARKGRAY = detail::bg(C_DARKGRAY), + B_LIGHTBLUE = detail::bg(C_LIGHTBLUE), + B_LIGHTGREEN = detail::bg(C_LIGHTGREEN), + B_LIGHTCYAN = detail::bg(C_LIGHTCYAN), + B_LIGHTRED = detail::bg(C_LIGHTRED), + B_LIGHTMAGENTA = detail::bg(C_LIGHTMAGENTA), + B_YELLOW = detail::bg(C_YELLOW), + B_WHITE = detail::bg(C_WHITE), }; struct FarColor; diff --git a/far/platform.com.hpp b/far/platform.com.hpp index 586720c244..1055375b3f 100644 --- a/far/platform.com.hpp +++ b/far/platform.com.hpp @@ -87,7 +87,7 @@ namespace os::com { public: explicit exception(HRESULT const ErrorCode, string_view const Message, source_location const& Location = source_location::current()): - far_exception(Message, false, Location) + far_exception({{ static_cast(ErrorCode), STATUS_SUCCESS }, Message }, Location) { Win32Error = ErrorCode; } diff --git a/far/platform.reg.cpp b/far/platform.reg.cpp index 8fdfad67c0..33d4f8ff16 100644 --- a/far/platform.reg.cpp +++ b/far/platform.reg.cpp @@ -96,8 +96,8 @@ namespace os::reg const key key::current_user = key(HKEY_CURRENT_USER); const key key::local_machine = key(HKEY_LOCAL_MACHINE); - exception::exception(error const& Error): - far_exception({{ static_cast(Error.Code), STATUS_SUCCESS }, Error.What }) + exception::exception(error const& Error, source_location const& Location): + far_exception({{ static_cast(Error.Code), STATUS_SUCCESS }, Error.What }, Location) {} result key::open(string_view const SubKeyName, DWORD const SamDesired) const diff --git a/far/platform.reg.hpp b/far/platform.reg.hpp index 55cd5f2786..40845b1fcf 100644 --- a/far/platform.reg.hpp +++ b/far/platform.reg.hpp @@ -42,6 +42,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "common/expected.hpp" #include "common/enumerator.hpp" #include "common/noncopyable.hpp" +#include "common/source_location.hpp" #include "common/type_traits.hpp" // External: @@ -63,7 +64,7 @@ namespace os::reg class exception: public far_exception { public: - explicit exception(error const& Error); + explicit exception(error const& Error, source_location const& Location = source_location::current()); }; template diff --git a/far/scrbuf.cpp b/far/scrbuf.cpp index 75ee7ac009..3845d56a87 100644 --- a/far/scrbuf.cpp +++ b/far/scrbuf.cpp @@ -222,16 +222,16 @@ void ScreenBuf::Read(rectangle Where, matrix& Dest) static unsigned char apply_nt_index_shadow(unsigned char const Color) { // If it's intense then remove the intensity. - if (Color & FOREGROUND_INTENSITY) - return Color & ~FOREGROUND_INTENSITY; + if (Color & C_INTENSE) + return Color & ~C_INTENSE; // 0x07 (silver) is technically "non-intense white", so it should become black as all the other non-intense colours. // However, making it 0x08 (grey or "intense black") instead gives better results. - if (Color == F_LIGHTGRAY) - return F_DARKGRAY; + if (Color == C_LIGHTGRAY) + return C_DARKGRAY; // Non-intense can't get any darker, so just return black. - return F_BLACK; + return C_BLACK; } static bool apply_index_shadow(FarColor& Color, COLORREF FarColor::* ColorAccessor, bool const Is256ColorAvailable) diff --git a/far/scrsaver.cpp b/far/scrsaver.cpp index fcbdfc31d3..dc1f4fd221 100644 --- a/far/scrsaver.cpp +++ b/far/scrsaver.cpp @@ -69,14 +69,14 @@ namespace static const inline int Colours[] { - F_BLUE, - F_CYAN, - F_RED, - F_MAGENTA, - F_BROWN + C_BLUE, + C_CYAN, + C_RED, + C_MAGENTA, + C_BROWN }; - static const int DefaultColour = F_LIGHTGRAY; + static const int DefaultColour = C_LIGHTGRAY; static const int DefaultColorPercentage = 95; @@ -163,7 +163,7 @@ namespace const size_t Size = (Horizon - i.Z) / static_cast(Horizon * std::size(StarSprites)); assert(Size < std::size(StarSprites)); - const auto Bright = Size >= std::size(StarSprites) / 2? FOREGROUND_INTENSITY : 0; + const auto Bright = Size >= std::size(StarSprites) / 2? C_INTENSE : 0; SetColor(i.Color | Bright | B_BLACK); GotoXY(X, Y); diff --git a/far/vbuild.m4 b/far/vbuild.m4 index b1acb6e4a8..e062cfdd05 100644 --- a/far/vbuild.m4 +++ b/far/vbuild.m4 @@ -1 +1 @@ -6369 +6370