Code · 44 lines · 1304 bytes
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
#nullable disable

namespace InteractiveReadLine.Formatting
{
    /// <summary>
    /// A struct representing a single character and its foreground and background colors. Null colors represents the
    /// system defaults.
    /// </summary>
    public struct FormattedChar : IEquatable<FormattedChar>
    {
        public FormattedChar(char c, ConsoleColor? foreground, ConsoleColor? background)
        {
            Char = c;
            Foreground = foreground;
            Background = background;
        }

        public char Char { get; }
        public ConsoleColor? Foreground { get; }
        public ConsoleColor? Background { get; }

        public bool Equals(FormattedChar other)
        {
            return Char == other.Char && Foreground == other.Foreground && Background == other.Background;
        }

        public override bool Equals(object obj)
        {
            return obj is FormattedChar other && Equals(other);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = Char.GetHashCode();
                hashCode = (hashCode * 397) ^ Foreground.GetHashCode();
                hashCode = (hashCode * 397) ^ Background.GetHashCode();
                return hashCode;
            }
        }
    }
}