summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibVT/Attribute.h
blob: 1bc2c8c51ef4b89b0cdf3854a7a1eaea2973b3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Noncopyable.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibVT/Color.h>
#include <LibVT/XtermColors.h>

namespace VT {

struct Attribute {
    static constexpr Color default_foreground_color = Color::named(Color::ANSIColor::DefaultForeground);
    static constexpr Color default_background_color = Color::named(Color::ANSIColor::DefaultBackground);

    void reset()
    {
        foreground_color = default_foreground_color;
        background_color = default_background_color;
        flags = Flags::NoAttributes;
#ifndef KERNEL
        href = {};
        href_id = {};
#endif
    }

    Color foreground_color { default_foreground_color };
    Color background_color { default_background_color };

    constexpr Color effective_background_color() const { return flags & Negative ? foreground_color : background_color; }
    constexpr Color effective_foreground_color() const { return flags & Negative ? background_color : foreground_color; }

#ifndef KERNEL
    String href;
    String href_id;
#endif

    enum Flags : u8 {
        NoAttributes = 0x00,
        Bold = 0x01,
        Italic = 0x02,
        Underline = 0x04,
        Negative = 0x08,
        Blink = 0x10,
        Touched = 0x20,
    };

    constexpr bool is_untouched() const { return !(flags & Touched); }

    // TODO: it would be really nice if we had a helper for enums that
    // exposed bit ops for class enums...
    u8 flags { Flags::NoAttributes };

    constexpr bool operator==(const Attribute& other) const
    {
        return foreground_color == other.foreground_color && background_color == other.background_color && flags == other.flags;
    }
    constexpr bool operator!=(const Attribute& other) const
    {
        return !(*this == other);
    }
};

}