Style.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/Types.h>
  9. #include <AK/Utf8View.h>
  10. #include <AK/Vector.h>
  11. namespace Line {
  12. class Style {
  13. public:
  14. bool operator==(Style const&) const = default;
  15. enum class XtermColor : int {
  16. Default = 9,
  17. Black = 0,
  18. Red,
  19. Green,
  20. Yellow,
  21. Blue,
  22. Magenta,
  23. Cyan,
  24. White,
  25. Unchanged,
  26. };
  27. struct AnchoredTag {
  28. };
  29. struct UnderlineTag {
  30. };
  31. struct BoldTag {
  32. };
  33. struct ItalicTag {
  34. };
  35. struct Color {
  36. bool operator==(Color const&) const = default;
  37. explicit Color(XtermColor color)
  38. : m_xterm_color(color)
  39. , m_is_rgb(false)
  40. {
  41. }
  42. Color(u8 r, u8 g, u8 b)
  43. : m_rgb_color({ r, g, b })
  44. , m_is_rgb(true)
  45. {
  46. }
  47. bool is_default() const
  48. {
  49. return !m_is_rgb && m_xterm_color == XtermColor::Unchanged;
  50. }
  51. XtermColor m_xterm_color { XtermColor::Unchanged };
  52. Vector<int, 3> m_rgb_color;
  53. bool m_is_rgb { false };
  54. };
  55. struct Background : public Color {
  56. explicit Background(XtermColor color)
  57. : Color(color)
  58. {
  59. }
  60. Background(u8 r, u8 g, u8 b)
  61. : Color(r, g, b)
  62. {
  63. }
  64. ByteString to_vt_escape() const;
  65. };
  66. struct Foreground : public Color {
  67. explicit Foreground(XtermColor color)
  68. : Color(color)
  69. {
  70. }
  71. Foreground(u8 r, u8 g, u8 b)
  72. : Color(r, g, b)
  73. {
  74. }
  75. ByteString to_vt_escape() const;
  76. };
  77. struct Hyperlink {
  78. bool operator==(Hyperlink const&) const = default;
  79. explicit Hyperlink(StringView link)
  80. : m_link(link)
  81. {
  82. m_has_link = true;
  83. }
  84. Hyperlink() = default;
  85. ByteString to_vt_escape(bool starting) const;
  86. bool is_empty() const { return !m_has_link; }
  87. ByteString m_link;
  88. bool m_has_link { false };
  89. };
  90. struct Mask {
  91. bool operator==(Mask const& other) const
  92. {
  93. return other.mode == mode && other.replacement == replacement;
  94. }
  95. enum class Mode {
  96. ReplaceEntireSelection,
  97. ReplaceEachCodePointInSelection,
  98. };
  99. explicit Mask(StringView replacement, Mode mode = Mode::ReplaceEntireSelection)
  100. : replacement(replacement)
  101. , replacement_view(this->replacement)
  102. , mode(mode)
  103. {
  104. }
  105. ByteString replacement;
  106. mutable Utf8View replacement_view;
  107. Mode mode;
  108. };
  109. static constexpr UnderlineTag Underline {};
  110. static constexpr BoldTag Bold {};
  111. static constexpr ItalicTag Italic {};
  112. static constexpr AnchoredTag Anchored {};
  113. // Prepare for the horror of templates.
  114. template<typename T, typename... Rest>
  115. Style(T const& style_arg, Rest... rest)
  116. : Style(rest...)
  117. {
  118. set(style_arg);
  119. m_is_empty = false;
  120. }
  121. Style() = default;
  122. static Style reset_style()
  123. {
  124. return { Foreground(XtermColor::Default), Background(XtermColor::Default), Hyperlink(""sv) };
  125. }
  126. Style unified_with(Style const& other, bool prefer_other = true) const
  127. {
  128. Style style = *this;
  129. style.unify_with(other, prefer_other);
  130. return style;
  131. }
  132. void unify_with(Style const&, bool prefer_other = false);
  133. bool underline() const { return m_underline; }
  134. bool bold() const { return m_bold; }
  135. bool italic() const { return m_italic; }
  136. Background background() const { return m_background; }
  137. Foreground foreground() const { return m_foreground; }
  138. Hyperlink hyperlink() const { return m_hyperlink; }
  139. Optional<Mask> mask() const { return m_mask; }
  140. void unset_mask() const { m_mask = {}; }
  141. void set(ItalicTag const&) { m_italic = true; }
  142. void set(BoldTag const&) { m_bold = true; }
  143. void set(UnderlineTag const&) { m_underline = true; }
  144. void set(Background const& bg) { m_background = bg; }
  145. void set(Foreground const& fg) { m_foreground = fg; }
  146. void set(Hyperlink const& link) { m_hyperlink = link; }
  147. void set(AnchoredTag const&) { m_is_anchored = true; }
  148. void set(Mask const& mask) { m_mask = mask; }
  149. bool is_anchored() const { return m_is_anchored; }
  150. bool is_empty() const { return m_is_empty; }
  151. ByteString to_byte_string() const;
  152. private:
  153. bool m_underline { false };
  154. bool m_bold { false };
  155. bool m_italic { false };
  156. Background m_background { XtermColor::Unchanged };
  157. Foreground m_foreground { XtermColor::Unchanged };
  158. Hyperlink m_hyperlink;
  159. mutable Optional<Mask> m_mask;
  160. bool m_is_anchored { false };
  161. bool m_is_empty { true };
  162. };
  163. }