Serenity Operating System
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
27#pragma once
28#include <stdlib.h>
29
30namespace Line {
31
32class Style {
33public:
34 enum class Color : int {
35 Default = 9,
36 Black = 0,
37 Red,
38 Green,
39 Yellow,
40 Blue,
41 Magenta,
42 Cyan,
43 White,
44 // TODO: it appears that we do not support these SGR options
45 BrightBlack = 60,
46 BrightRed,
47 BrightGreen,
48 BrightYellow,
49 BrightBlue,
50 BrightMagenta,
51 BrightCyan,
52 BrightWhite,
53 };
54
55 struct UnderlineTag {
56 };
57 struct BoldTag {
58 };
59 struct ItalicTag {
60 };
61 struct Background {
62 explicit Background(Color color)
63 : m_color(color)
64 {
65 }
66 Color m_color;
67 };
68 struct Foreground {
69 explicit Foreground(Color color)
70 : m_color(color)
71 {
72 }
73 Color m_color;
74 };
75
76 static constexpr UnderlineTag Underline {};
77 static constexpr BoldTag Bold {};
78 static constexpr ItalicTag Italic {};
79
80 // prepare for the horror of templates
81 template <typename T, typename... Rest>
82 Style(const T& style_arg, Rest... rest)
83 : Style(rest...)
84 {
85 set(style_arg);
86 }
87 Style() {}
88
89 bool underline() const { return m_underline; }
90 bool bold() const { return m_bold; }
91 bool italic() const { return m_italic; }
92 Color background() const { return m_background; }
93 Color foreground() const { return m_foreground; }
94
95 void set(const ItalicTag&) { m_italic = true; }
96 void set(const BoldTag&) { m_bold = true; }
97 void set(const UnderlineTag&) { m_underline = true; }
98 void set(const Background& bg) { m_background = bg.m_color; }
99 void set(const Foreground& fg) { m_foreground = fg.m_color; }
100
101private:
102 bool m_underline { false };
103 bool m_bold { false };
104 bool m_italic { false };
105 Color m_background { Color::Default };
106 Color m_foreground { Color::Default };
107};
108}