Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Sep 15, 2024
1 parent 5c7850d commit 62ae2d1
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 81 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion far/color_picker_256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
};
Expand Down
13 changes: 7 additions & 6 deletions far/colormix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) }
};
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 22 additions & 13 deletions far/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<wchar_t>(L'0' + (Index & 0b111)));
append(Str, Color.Value & C_INTENSE? Mapping.Intense : Mapping.Normal, static_cast<wchar_t>(L'0' + (Index & 0b111)));
else
far::format_to(Str, L"{1}{0}5{0}{2}"sv, Mapping.Separator, Mapping.ExtendedColour, Index);
}
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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(
" "
Expand All @@ -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];

Expand All @@ -3221,22 +3230,22 @@ 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);
Buffer[1].Attributes.UnderlineColor = Buffer[1].Attributes.ForegroundColor;
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(
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions far/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
};
Expand Down
19 changes: 10 additions & 9 deletions far/hilight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"<exec>"sv, 0, MakeFarColor(F_LIGHTGREEN), MakeFarColor(F_LIGHTGREEN) },
{ L"<arc>"sv, 0, MakeFarColor(F_LIGHTMAGENTA), MakeFarColor(F_LIGHTMAGENTA) },
{ L"<temp>"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"<exec>"sv, 0, MakeFarColor(C_LIGHTGREEN), MakeFarColor(C_LIGHTGREEN) },
{ L"<arc>"sv, 0, MakeFarColor(C_LIGHTMAGENTA), MakeFarColor(C_LIGHTMAGENTA) },
{ L"<temp>"sv, 0, MakeFarColor(C_BROWN), MakeFarColor(C_BROWN) },
};

const auto root = cfg.CreateKey(cfg.root_key, names::Highlight);
Expand Down
91 changes: 58 additions & 33 deletions far/palette.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion far/platform.com.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DWORD>(ErrorCode), STATUS_SUCCESS }, Message }, Location)
{
Win32Error = ErrorCode;
}
Expand Down
4 changes: 2 additions & 2 deletions far/platform.reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DWORD>(Error.Code), STATUS_SUCCESS }, Error.What })
exception::exception(error const& Error, source_location const& Location):
far_exception({{ static_cast<DWORD>(Error.Code), STATUS_SUCCESS }, Error.What }, Location)
{}

result<key> key::open(string_view const SubKeyName, DWORD const SamDesired) const
Expand Down
3 changes: 2 additions & 1 deletion far/platform.reg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<typename T>
Expand Down
10 changes: 5 additions & 5 deletions far/scrbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,16 @@ void ScreenBuf::Read(rectangle Where, matrix<FAR_CHAR_INFO>& 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)
Expand Down
14 changes: 7 additions & 7 deletions far/scrsaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -163,7 +163,7 @@ namespace
const size_t Size = (Horizon - i.Z) / static_cast<double>(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);
Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6369
6370

0 comments on commit 62ae2d1

Please sign in to comment.