Style.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/Types.h>
  9. #include <AK/Vector.h>
  10. #include <stdlib.h>
  11. namespace Line {
  12. class Style {
  13. public:
  14. bool operator==(const Style&) 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==(const Color&) 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. String 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. String to_vt_escape() const;
  76. };
  77. struct Hyperlink {
  78. bool operator==(const Hyperlink&) const = default;
  79. explicit Hyperlink(const StringView& link)
  80. : m_link(link)
  81. {
  82. m_has_link = true;
  83. }
  84. Hyperlink() { }
  85. String to_vt_escape(bool starting) const;
  86. bool is_empty() const { return !m_has_link; }
  87. String m_link;
  88. bool m_has_link { false };
  89. };
  90. static constexpr UnderlineTag Underline {};
  91. static constexpr BoldTag Bold {};
  92. static constexpr ItalicTag Italic {};
  93. static constexpr AnchoredTag Anchored {};
  94. // Prepare for the horror of templates.
  95. template<typename T, typename... Rest>
  96. Style(const T& style_arg, Rest... rest)
  97. : Style(rest...)
  98. {
  99. set(style_arg);
  100. m_is_empty = false;
  101. }
  102. Style() { }
  103. static Style reset_style()
  104. {
  105. return { Foreground(XtermColor::Default), Background(XtermColor::Default), Hyperlink("") };
  106. }
  107. Style unified_with(const Style& other, bool prefer_other = true) const
  108. {
  109. Style style = *this;
  110. style.unify_with(other, prefer_other);
  111. return style;
  112. }
  113. void unify_with(const Style&, bool prefer_other = false);
  114. bool underline() const { return m_underline; }
  115. bool bold() const { return m_bold; }
  116. bool italic() const { return m_italic; }
  117. Background background() const { return m_background; }
  118. Foreground foreground() const { return m_foreground; }
  119. Hyperlink hyperlink() const { return m_hyperlink; }
  120. void set(const ItalicTag&) { m_italic = true; }
  121. void set(const BoldTag&) { m_bold = true; }
  122. void set(const UnderlineTag&) { m_underline = true; }
  123. void set(const Background& bg) { m_background = bg; }
  124. void set(const Foreground& fg) { m_foreground = fg; }
  125. void set(const Hyperlink& link) { m_hyperlink = link; }
  126. void set(const AnchoredTag&) { m_is_anchored = true; }
  127. bool is_anchored() const { return m_is_anchored; }
  128. bool is_empty() const { return m_is_empty; }
  129. String to_string() const;
  130. private:
  131. bool m_underline { false };
  132. bool m_bold { false };
  133. bool m_italic { false };
  134. Background m_background { XtermColor::Unchanged };
  135. Foreground m_foreground { XtermColor::Unchanged };
  136. Hyperlink m_hyperlink;
  137. bool m_is_anchored { false };
  138. bool m_is_empty { true };
  139. };
  140. }