Skip to content

Commit

Permalink
Modify fore/background color handling and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-n-t-h-o-n-y committed Jan 24, 2024
1 parent bb8f3d0 commit 87ce0eb
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 208 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ target_include_directories(escape

target_compile_features(escape
PUBLIC
cxx_std_17
cxx_std_20
)

target_compile_options(escape
Expand Down
25 changes: 6 additions & 19 deletions include/esc/brush.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ESC_BRUSH_HPP
#define ESC_BRUSH_HPP
#pragma once

#include <esc/color.hpp>
#include <esc/default_color.hpp>
#include <esc/trait.hpp>
Expand All @@ -12,22 +12,9 @@ struct Brush {
Color background = DefaultColor{};
Color foreground = DefaultColor{};
Traits traits = Traits{Trait::None};
};

/// Returns true if the two Brushes have the same background, foreground, and
/// traits.
[[nodiscard]] inline auto operator==(Brush const& lhs, Brush const& rhs) -> bool
{
return lhs.background == rhs.background &&
lhs.foreground == rhs.foreground && lhs.traits == rhs.traits;
}

/// Returns true if the two Brushes do not have the same background, foreground,
/// and traits.
[[nodiscard]] inline auto operator!=(Brush const& lhs, Brush const& rhs) -> bool
{
return !(lhs == rhs);
}
[[nodiscard]] constexpr bool operator==(Brush const&) const = default;
[[nodiscard]] constexpr bool operator!=(Brush const&) const = default;
};

} // namespace esc
#endif // ESC_BRUSH_HPP
} // namespace esc
20 changes: 20 additions & 0 deletions include/esc/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ namespace esc {
/// Variant for any of the color types that can be used.
using Color = std::variant<ColorIndex, TrueColor, DefaultColor>;

struct ColorFG {
Color value;
};

struct ColorBG {
Color value;
};

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto background(Color c) -> ColorBG { return {c}; }

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto bg(Color c) -> ColorBG { return {c}; }

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto foreground(Color c) -> ColorFG { return {c}; }

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto fg(Color c) -> ColorFG { return {c}; }

} // namespace esc
45 changes: 4 additions & 41 deletions include/esc/color_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ struct ColorIndex {
static ColorIndex const BrightMagenta;
static ColorIndex const BrightCyan;
static ColorIndex const BrightWhite;

[[nodiscard]] constexpr bool operator==(ColorIndex const&) const = default;
[[nodiscard]] constexpr bool operator!=(ColorIndex const&) const = default;
};

// Yes, defining constexpr vs const declaration is legal here.
// Yes, defining constexpr with const declaration is legal here.
constexpr ColorIndex ColorIndex::Black = ColorIndex{0};
constexpr ColorIndex ColorIndex::Red = ColorIndex{1};
constexpr ColorIndex ColorIndex::Green = ColorIndex{2};
Expand All @@ -44,44 +47,4 @@ constexpr ColorIndex ColorIndex::BrightMagenta = ColorIndex{13};
constexpr ColorIndex ColorIndex::BrightCyan = ColorIndex{14};
constexpr ColorIndex ColorIndex::BrightWhite = ColorIndex{15};

/// Return true if \p a and \p b contain the same index value.
constexpr auto operator==(ColorIndex a, ColorIndex b) -> bool
{
return a.value == b.value;
}

/// Return false if \p a and \p b contain the same index value.
constexpr auto operator!=(ColorIndex a, ColorIndex b) -> bool
{
return a.value != b.value;
}

/// Tag type for ColorIndex that is used as a background.
struct ColorIndexBG {
ColorIndex value;
};

/// Tag type for ColorIndex that is used as a foreground.
struct ColorIndexFG {
ColorIndex value;
};

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto background(ColorIndex x) -> ColorIndexBG
{
return {x};
}

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto bg(ColorIndex x) -> ColorIndexBG { return {x}; }

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto foreground(ColorIndex x) -> ColorIndexFG
{
return {x};
}

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto fg(ColorIndex x) -> ColorIndexFG { return {x}; }

} // namespace esc
43 changes: 6 additions & 37 deletions include/esc/default_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,11 @@
namespace esc {

/// Default Color of the terminal.
struct DefaultColor {};

/// Checks for equality, always returns true.
[[nodiscard]] constexpr auto operator==(DefaultColor, DefaultColor) -> bool
{
return true;
}

/// Checks for inequality, always returns false.
[[nodiscard]] constexpr auto operator!=(DefaultColor, DefaultColor) -> bool
{
return false;
}

/// Tag type for terminal default color background.
struct DefaultColorBG {};

/// Tag type for terminal default color foreground.
struct DefaultColorFG {};

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto background(DefaultColor) -> DefaultColorBG
{
return {};
}

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto bg(DefaultColor) -> DefaultColorBG { return {}; }

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto foreground(DefaultColor) -> DefaultColorFG
{
return {};
}

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto fg(DefaultColor) -> DefaultColorFG { return {}; }
struct DefaultColor {
[[nodiscard]] constexpr bool operator==(DefaultColor const&) const =
default;
[[nodiscard]] constexpr bool operator!=(DefaultColor const&) const =
default;
};

} // namespace esc
9 changes: 8 additions & 1 deletion include/esc/detail/mask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ class Mask {

/// Return true if data() members are equal.
template <typename E>
auto constexpr operator==(Mask<E> a, Mask<E> b) -> bool
[[nodiscard]] auto constexpr operator==(Mask<E> a, Mask<E> b) -> bool
{
return a.data() == b.data();
}

/// Return true if data() members are not equal.
template <typename E>
[[nodiscard]] auto constexpr operator!=(Mask<E> a, Mask<E> b) -> bool
{
return a.data() != b.data();
}

} // namespace esc::detail
#endif // ESC_DETAIL_MASK_HPP
26 changes: 14 additions & 12 deletions include/esc/sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ struct Blank_screen {};

// BACKGROUND COLOR ------------------------------------------------------------

/// Return control sequence to set background to Color variant.
[[nodiscard]] auto escape(ColorBG c) -> std::string;

/// Return control sequence to set background to given xterm palette index.
[[nodiscard]] auto escape(ColorIndexBG x) -> std::string;
[[nodiscard]] auto escape_bg(ColorIndex c) -> std::string;

/// Return control sequence to set background to given terminal true color.
[[nodiscard]] auto escape(TrueColorBG x) -> std::string;
[[nodiscard]] auto escape_bg(TrueColor c) -> std::string;

/// Return control sequence to reset the background to the terminal default
[[nodiscard]] auto escape(DefaultColorBG) -> std::string;
[[nodiscard]] auto escape_bg(DefaultColor c) -> std::string;

/// Returns the last color that was created with set_background().
/** May not represent what is on the screen if the last call to set_background()
Expand All @@ -86,14 +89,17 @@ struct Blank_screen {};

// FOREGROUND COLOR ------------------------------------------------------------

/// Return control sequence to set foreground to Color variant.
[[nodiscard]] auto escape(ColorFG c) -> std::string;

/// Return control sequence to set foreground to given xterm palette index.
[[nodiscard]] auto escape(ColorIndexFG x) -> std::string;
[[nodiscard]] auto escape_fg(ColorIndex c) -> std::string;

/// Return control sequence to set foreground to given terminal true color.
[[nodiscard]] auto escape(TrueColorFG x) -> std::string;
[[nodiscard]] auto escape_fg(TrueColor c) -> std::string;

/// Return control sequence to reset the foreground to the terminal default
[[nodiscard]] auto escape(DefaultColorFG) -> std::string;
[[nodiscard]] auto escape_fg(DefaultColor c) -> std::string;

/// Returns the last color that was created with set_foreground().
/** May not represent what is on the screen if the last call to set_foreground()
Expand All @@ -114,12 +120,8 @@ bool constexpr is_escapable = detail::is_any_of<T,
Blank_screen,
Trait,
Traits,
ColorIndexBG,
ColorIndexFG,
TrueColorBG,
TrueColorFG,
DefaultColorBG,
DefaultColorFG,
ColorBG,
ColorFG,
Brush>;

/// Convenience function to concatenate multiple escapable objects at once.
Expand Down
48 changes: 16 additions & 32 deletions include/esc/trait.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ESC_TRAIT_HPP
#define ESC_TRAIT_HPP
#pragma once

#include <cstdint>
#include <string>

Expand Down Expand Up @@ -28,6 +28,17 @@ enum class Trait : std::uint16_t {
/// Mask type for the Trait flag.
using Traits = detail::Mask<Trait>;

/// Used to request a Trait be removed from a Traits mask.
struct RemoveTrait {
Trait trait;
};

/// Create a RemoveTrait object
[[nodiscard]] constexpr auto remove_trait(Trait t) -> RemoveTrait
{
return RemoveTrait{t};
}

/// Mask creating insert operation, non-modifying, returns the new value.
[[nodiscard]] auto constexpr operator|(Trait a, Trait b) -> Traits
{
Expand All @@ -40,37 +51,10 @@ using Traits = detail::Mask<Trait>;
return a.insert(b);
}

/// Insert assignment operation, returns a reference to the modified \p a.
auto constexpr operator|=(Traits& a, Trait b) -> Traits&
{
a = a.insert(b);
return a;
}

/// Merge operation, non-modifying, returns the new value.
[[nodiscard]] auto constexpr operator|(Traits a, Traits b) -> Traits
{
return static_cast<Traits>(static_cast<Trait>(a.data() | b.data()));
}

/// Merge assignment operation, returns reference to the modified \p a.
auto constexpr operator|=(Traits& a, Traits b) -> Traits&
{
a = (a | b);
return a;
}

/// Return true if \p a has exactly the same set of Traits as \p b.
[[nodiscard]] auto constexpr operator==(Traits a, Traits b) -> bool
{
return a.data() == b.data();
}

/// Return true if \p a doesn't have exactly the same set of Traits as \p b.
[[nodiscard]] auto constexpr operator!=(Traits a, Traits b) -> bool
/// Insert operation, non-modifying, returns the new value.
[[nodiscard]] auto constexpr operator|(Traits a, RemoveTrait b) -> Traits
{
return a.data() != b.data();
return a.remove(b.trait);
}

} // namespace esc
#endif // ESC_TRAIT_HPP
44 changes: 2 additions & 42 deletions include/esc/true_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,49 +130,9 @@ class TrueColor {

/// Construct a true color with HSL values.
constexpr TrueColor(HSL x) : TrueColor{hsl_to_rgb(x)} {}
};

/// Returns true if the two `TrueColor`s are the same.

[[nodiscard]] inline auto operator==(TrueColor const& lhs, TrueColor const& rhs)
-> bool
{
return lhs.red == rhs.red && lhs.green == rhs.green && lhs.blue == rhs.blue;
}

/// Returns true if the two TrueColor are not the same.
[[nodiscard]] inline auto operator!=(TrueColor const& lhs, TrueColor const& rhs)
-> bool
{
return !(lhs == rhs);
}

/// Tag type for TrueColor that is used as a background.
struct TrueColorBG {
TrueColor value;
};

/// Tag type for TrueColor that is used as a foreground.
struct TrueColorFG {
TrueColor value;
[[nodiscard]] constexpr bool operator==(TrueColor const&) const = default;
[[nodiscard]] constexpr bool operator!=(TrueColor const&) const = default;
};

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto background(TrueColor x) -> TrueColorBG
{
return {x};
}

/// Return a background tag type to use with escape(...) function.
[[nodiscard]] constexpr auto bg(TrueColor x) -> TrueColorBG { return {x}; }

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto foreground(TrueColor x) -> TrueColorFG
{
return {x};
}

/// Return a foreground tag type to use with escape(...) function.
[[nodiscard]] constexpr auto fg(TrueColor x) -> TrueColorFG { return {x}; }

} // namespace esc
Loading

0 comments on commit 87ce0eb

Please sign in to comment.