Style.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Types.h>
  28. #include <AK/Vector.h>
  29. #include <stdlib.h>
  30. namespace Line {
  31. class Style {
  32. public:
  33. enum class XtermColor : int {
  34. Default = 9,
  35. Black = 0,
  36. Red,
  37. Green,
  38. Yellow,
  39. Blue,
  40. Magenta,
  41. Cyan,
  42. White,
  43. };
  44. struct UnderlineTag {
  45. };
  46. struct BoldTag {
  47. };
  48. struct ItalicTag {
  49. };
  50. struct Color {
  51. explicit Color(XtermColor color)
  52. : m_xterm_color(color)
  53. , m_is_rgb(false)
  54. {
  55. }
  56. Color(u8 r, u8 g, u8 b)
  57. : m_rgb_color({ r, g, b })
  58. , m_is_rgb(true)
  59. {
  60. }
  61. XtermColor m_xterm_color { XtermColor::Default };
  62. Vector<u8, 3> m_rgb_color;
  63. bool m_is_rgb { false };
  64. };
  65. struct Background : public Color {
  66. explicit Background(XtermColor color)
  67. : Color(color)
  68. {
  69. }
  70. Background(u8 r, u8 g, u8 b)
  71. : Color(r, g, b)
  72. {
  73. }
  74. String to_vt_escape() const;
  75. };
  76. struct Foreground : public Color {
  77. explicit Foreground(XtermColor color)
  78. : Color(color)
  79. {
  80. }
  81. Foreground(u8 r, u8 g, u8 b)
  82. : Color(r, g, b)
  83. {
  84. }
  85. String to_vt_escape() const;
  86. };
  87. static constexpr UnderlineTag Underline {};
  88. static constexpr BoldTag Bold {};
  89. static constexpr ItalicTag Italic {};
  90. // prepare for the horror of templates
  91. template<typename T, typename... Rest>
  92. Style(const T& style_arg, Rest... rest)
  93. : Style(rest...)
  94. {
  95. set(style_arg);
  96. }
  97. Style() { }
  98. bool underline() const { return m_underline; }
  99. bool bold() const { return m_bold; }
  100. bool italic() const { return m_italic; }
  101. Background background() const { return m_background; }
  102. Foreground foreground() const { return m_foreground; }
  103. void set(const ItalicTag&) { m_italic = true; }
  104. void set(const BoldTag&) { m_bold = true; }
  105. void set(const UnderlineTag&) { m_underline = true; }
  106. void set(const Background& bg) { m_background = bg; }
  107. void set(const Foreground& fg) { m_foreground = fg; }
  108. private:
  109. bool m_underline { false };
  110. bool m_bold { false };
  111. bool m_italic { false };
  112. Background m_background { XtermColor::Default };
  113. Foreground m_foreground { XtermColor::Default };
  114. };
  115. }