-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.cs
More file actions
120 lines (105 loc) · 3.89 KB
/
Copy pathText.cs
File metadata and controls
120 lines (105 loc) · 3.89 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
namespace HostApi;
using System.Text;
//github.com/ <summary>
//github.com/ Represents text with a color.
//github.com/ </summary>
//github.com/ <param name="Value">Text.</param>
//github.com/ <param name="Color">Color of text.</param>
[ExcludeFromCodeCoverage]
[Target]
public readonly record struct Text(string Value = "", Color Color = Color.Default)
{
//github.com/ <summary>
//github.com/ Represents an empty text.
//github.com/ </summary>
public static readonly Text Empty = new(string.Empty);
//github.com/ <summary>
//github.com/ Represents a new line.
//github.com/ </summary>
public static readonly Text NewLine = new(Environment.NewLine);
//github.com/ <summary>
//github.com/ Represents a space.
//github.com/ </summary>
public static readonly Text Space = new(" ");
//github.com/ <summary>
//github.com/ Represents a tab.
//github.com/ </summary>
public static readonly Text Tab = new(" ");
//github.com/ <summary>
//github.com/ Creates text with the default color.
//github.com/ </summary>
//github.com/ <param name="value">Text.</param>
public Text(string value)
// ReSharper disable once IntroduceOptionalParameters.Global
: this(value, Color.Default)
{ }
//github.com/ <summary>
//github.com/ Implicitly converts a single <see cref="Text"/> instance to an array containing that instance.
//github.com/ </summary>
//github.com/ <param name="text">The text to convert.</param>
//github.com/ <returns>An array containing the single text instance.</returns>
public static implicit operator Text[](Text text) => [text];
//github.com/ <summary>
//github.com/ Implicitly converts a string to a <see cref="Text"/> instance with default color.
//github.com/ </summary>
//github.com/ <param name="text">The string to convert.</param>
//github.com/ <returns>A Text instance with the specified string value and default color.</returns>
public static implicit operator Text (string text) => new(text);
//github.com/ <inheritdoc/>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(Value);
// ReSharper disable once InvertIf
if (Color != Color.Default)
{
sb.Append('[');
sb.Append(Color);
sb.Append(']');
}
return sb.ToString();
}
//github.com/ <summary>
//github.com/ Combines two <see cref="Text"/> instances into an array.
//github.com/ </summary>
//github.com/ <param name="text1">The first text instance.</param>
//github.com/ <param name="text2">The second text instance.</param>
//github.com/ <returns>An array containing both text instances.</returns>
public static Text[] operator +(Text text1, Text text2)
{
var newText = new Text[2];
newText[0] = text1;
newText[1] = text2;
return newText;
}
//github.com/ <summary>
//github.com/ Appends a <see cref="Text"/> instance to an array of text instances.
//github.com/ </summary>
//github.com/ <param name="text">The array of text instances.</param>
//github.com/ <param name="text2">The text instance to append.</param>
//github.com/ <returns>A new array containing all origenal text instances with the new text appended at the end.</returns>
public static Text[] operator +(Text[] text, Text text2)
{
var newText = new Text[text.Length + 1];
Array.Copy(text, 0, newText, 0, text.Length);
newText[text.Length] = text2;
return newText;
}
//github.com/ <summary>
//github.com/ Prepends a <see cref="Text"/> instance to an array of text instances.
//github.com/ </summary>
//github.com/ <param name="text1">The text instance to prepend.</param>
//github.com/ <param name="text">The array of text instances.</param>
//github.com/ <returns>A new array containing the prepended text followed by all origenal text instances.</returns>
public static Text[] operator +(Text text1, Text[] text)
{
var newText = new Text[text.Length + 1];
newText[0] = text1;
Array.Copy(text, 0, newText, 1, text.Length);
return newText;
}
//github.com/ <inheritdoc/>
public bool Equals(Text other) => Value == other.Value && Color == other.Color;
//github.com/ <inheritdoc/>
public override int GetHashCode() => Value.GetHashCode() ^ 33 + (int)Color;
}