| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-03-04 13:19:54 -07:00
										 |  |  |  * Copyright (c) 2020-2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2020-05-28 20:40:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | #include <AK/DeprecatedString.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  | #include <AK/Types.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-13 17:42:39 +02:00
										 |  |  | #include <AK/Utf8View.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Line { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Style { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |     bool operator==(Style const&) const = default; | 
					
						
							| 
									
										
										
										
											2021-02-20 21:33:13 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |     enum class XtermColor : int { | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |         Default = 9, | 
					
						
							|  |  |  |         Black = 0, | 
					
						
							|  |  |  |         Red, | 
					
						
							|  |  |  |         Green, | 
					
						
							|  |  |  |         Yellow, | 
					
						
							|  |  |  |         Blue, | 
					
						
							|  |  |  |         Magenta, | 
					
						
							|  |  |  |         Cyan, | 
					
						
							|  |  |  |         White, | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |         Unchanged, | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     struct AnchoredTag { | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     struct UnderlineTag { | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     struct BoldTag { | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     struct ItalicTag { | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |     struct Color { | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |         bool operator==(Color const&) const = default; | 
					
						
							| 
									
										
										
										
											2021-02-20 21:33:13 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |         explicit Color(XtermColor color) | 
					
						
							|  |  |  |             : m_xterm_color(color) | 
					
						
							|  |  |  |             , m_is_rgb(false) | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |         Color(u8 r, u8 g, u8 b) | 
					
						
							|  |  |  |             : m_rgb_color({ r, g, b }) | 
					
						
							|  |  |  |             , m_is_rgb(true) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |         bool is_default() const | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return !m_is_rgb && m_xterm_color == XtermColor::Unchanged; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         XtermColor m_xterm_color { XtermColor::Unchanged }; | 
					
						
							|  |  |  |         Vector<int, 3> m_rgb_color; | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |         bool m_is_rgb { false }; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     struct Background : public Color { | 
					
						
							|  |  |  |         explicit Background(XtermColor color) | 
					
						
							|  |  |  |             : Color(color) | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |         Background(u8 r, u8 g, u8 b) | 
					
						
							|  |  |  |             : Color(r, g, b) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         DeprecatedString to_vt_escape() const; | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct Foreground : public Color { | 
					
						
							|  |  |  |         explicit Foreground(XtermColor color) | 
					
						
							|  |  |  |             : Color(color) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Foreground(u8 r, u8 g, u8 b) | 
					
						
							|  |  |  |             : Color(r, g, b) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         DeprecatedString to_vt_escape() const; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     struct Hyperlink { | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |         bool operator==(Hyperlink const&) const = default; | 
					
						
							| 
									
										
										
										
											2021-02-20 21:33:13 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 00:55:02 +01:00
										 |  |  |         explicit Hyperlink(StringView link) | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |             : m_link(link) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_has_link = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 13:19:54 -07:00
										 |  |  |         Hyperlink() = default; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         DeprecatedString to_vt_escape(bool starting) const; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |         bool is_empty() const { return !m_has_link; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         DeprecatedString m_link; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |         bool m_has_link { false }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 16:10:42 +04:30
										 |  |  |     struct Mask { | 
					
						
							|  |  |  |         bool operator==(Mask const& other) const | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return other.mode == mode && other.replacement == replacement; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         enum class Mode { | 
					
						
							|  |  |  |             ReplaceEntireSelection, | 
					
						
							|  |  |  |             ReplaceEachCodePointInSelection, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         explicit Mask(StringView replacement, Mode mode = Mode::ReplaceEntireSelection) | 
					
						
							|  |  |  |             : replacement(replacement) | 
					
						
							|  |  |  |             , replacement_view(this->replacement) | 
					
						
							|  |  |  |             , mode(mode) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         DeprecatedString replacement; | 
					
						
							| 
									
										
										
										
											2022-05-02 16:10:42 +04:30
										 |  |  |         mutable Utf8View replacement_view; | 
					
						
							|  |  |  |         Mode mode; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     static constexpr UnderlineTag Underline {}; | 
					
						
							|  |  |  |     static constexpr BoldTag Bold {}; | 
					
						
							|  |  |  |     static constexpr ItalicTag Italic {}; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     static constexpr AnchoredTag Anchored {}; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 03:19:48 +04:30
										 |  |  |     // Prepare for the horror of templates.
 | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |     template<typename T, typename... Rest> | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |     Style(T const& style_arg, Rest... rest) | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |         : Style(rest...) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         set(style_arg); | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |         m_is_empty = false; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-04 13:19:54 -07:00
										 |  |  |     Style() = default; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     static Style reset_style() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return { Foreground(XtermColor::Default), Background(XtermColor::Default), Hyperlink(""sv) }; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |     Style unified_with(Style const& other, bool prefer_other = true) const | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     { | 
					
						
							|  |  |  |         Style style = *this; | 
					
						
							|  |  |  |         style.unify_with(other, prefer_other); | 
					
						
							|  |  |  |         return style; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |     void unify_with(Style const&, bool prefer_other = false); | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     bool underline() const { return m_underline; } | 
					
						
							|  |  |  |     bool bold() const { return m_bold; } | 
					
						
							|  |  |  |     bool italic() const { return m_italic; } | 
					
						
							| 
									
										
										
										
											2020-05-10 12:27:36 +04:30
										 |  |  |     Background background() const { return m_background; } | 
					
						
							|  |  |  |     Foreground foreground() const { return m_foreground; } | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     Hyperlink hyperlink() const { return m_hyperlink; } | 
					
						
							| 
									
										
										
										
											2022-05-02 16:10:42 +04:30
										 |  |  |     Optional<Mask> mask() const { return m_mask; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void unset_mask() const { m_mask = {}; } | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 14:41:18 +03:30
										 |  |  |     void set(ItalicTag const&) { m_italic = true; } | 
					
						
							|  |  |  |     void set(BoldTag const&) { m_bold = true; } | 
					
						
							|  |  |  |     void set(UnderlineTag const&) { m_underline = true; } | 
					
						
							|  |  |  |     void set(Background const& bg) { m_background = bg; } | 
					
						
							|  |  |  |     void set(Foreground const& fg) { m_foreground = fg; } | 
					
						
							|  |  |  |     void set(Hyperlink const& link) { m_hyperlink = link; } | 
					
						
							|  |  |  |     void set(AnchoredTag const&) { m_is_anchored = true; } | 
					
						
							| 
									
										
										
										
											2022-05-02 16:10:42 +04:30
										 |  |  |     void set(Mask const& mask) { m_mask = mask; } | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_anchored() const { return m_is_anchored; } | 
					
						
							|  |  |  |     bool is_empty() const { return m_is_empty; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |     DeprecatedString to_deprecated_string() const; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     bool m_underline { false }; | 
					
						
							|  |  |  |     bool m_bold { false }; | 
					
						
							|  |  |  |     bool m_italic { false }; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     Background m_background { XtermColor::Unchanged }; | 
					
						
							|  |  |  |     Foreground m_foreground { XtermColor::Unchanged }; | 
					
						
							|  |  |  |     Hyperlink m_hyperlink; | 
					
						
							| 
									
										
										
										
											2022-05-02 16:10:42 +04:30
										 |  |  |     mutable Optional<Mask> m_mask; | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_is_anchored { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool m_is_empty { true }; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | }; | 
					
						
							|  |  |  | } |