forked from Thraka/SadConsole.Drawing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
162 lines (137 loc) · 7.38 KB
/
StringExtensions.cs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using SadConsole;
using SadConsole.StringParser;
using System.Drawing;
using System.Windows;
namespace System
{
/// <summary>
/// Helpers for strings.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Aligns a string given a total character width and alignment style. Fills in the extra space with the space character.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="alignment">The horizontal alignment.</param>
/// <param name="totalWidth">The total width of the new string.</param>
/// <returns>A new string instance.</returns>
public static string Align(this string value, HorizontalAlignment alignment, int totalWidth)
{
return value.Align(alignment, totalWidth, ' ');
}
/// <summary>
/// Aligns a string given a total character width and alignment style.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="alignment">The horizontal alignment.</param>
/// <param name="totalWidth">The total width of the new string.</param>
/// <param name="fillCharacter">The character to use to fill in the extra spaces after alignment.</param>
/// <returns>A new string instance.</returns>
public static string Align(this string value, HorizontalAlignment alignment, int totalWidth, char fillCharacter)
{
string adjustedText = new string(fillCharacter, totalWidth);
if (!string.IsNullOrEmpty(value))
{
if (alignment == HorizontalAlignment.Left)
{
if (value.Length > totalWidth)
adjustedText = adjustedText = value.Substring(0, totalWidth);
else
adjustedText = value.PadRight(totalWidth, fillCharacter);
}
else if (alignment == HorizontalAlignment.Right)
{
if (value.Length > totalWidth)
adjustedText = adjustedText = value.Substring(value.Length - totalWidth);
else
adjustedText = value.PadLeft(totalWidth, fillCharacter);
}
else
{
if (value.Length > totalWidth)
adjustedText = adjustedText = value.Substring((value.Length - totalWidth) / 2, totalWidth);
else
{
int pad = (totalWidth - value.Length) / 2;
adjustedText = value.PadRight(pad + value.Length, fillCharacter).PadLeft(totalWidth, fillCharacter);
}
}
}
return adjustedText;
}
/// <summary>
/// Creates a <see cref="ColoredString"/> object from an existing string with the specified foreground and background, setting the ignore properties if needed.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="foreground">The foreground color. If null, <see cref="ColoredString.IgnoreForeground"/> will be set.</param>
/// <param name="background">The background color. If null, <see cref="ColoredString.IgnoreBackground"/> will be set.</param>
/// <param name="spriteEffect">The background color. If null, <see cref="ColoredString.IgnoreEffect"/> will be set.</param>
/// <returns>A <see cref="ColoredString"/> object instace.</returns>
public static ColoredString CreateColored(this string value, Color? foreground = null, Color? background = null)
{
var stacks = new ParseCommandStacks();
if (foreground.HasValue)
stacks.AddSafe(new ParseCommandRecolor() { R = foreground.Value.R, G = foreground.Value.G, B = foreground.Value.B, A = foreground.Value.A, CommandType = CommandTypes.Foreground });
if (background.HasValue)
stacks.AddSafe(new ParseCommandRecolor() { R = background.Value.R, G = background.Value.G, B = background.Value.B, A = background.Value.A, CommandType = CommandTypes.Background });
ColoredString newString = ColoredString.Parse(value, initialBehaviors: stacks);
if (!foreground.HasValue)
newString.IgnoreForeground = true;
if (!background.HasValue)
newString.IgnoreBackground = true;
return newString;
}
/// <summary>
/// Creates a <see cref="ColoredString"/> object from an existing string with the specified foreground gradient and cell effect.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="startingForeground">The starting foreground color to blend.</param>
/// <param name="endingForeground">The ending foreground color to blend.</param>
/// <returns>A <see cref="ColoredString"/> object instace.</returns>
public static ColoredString CreateGradient(this string value, Color startingForeground, Color endingForeground)
{
ColoredString newString = new ColoredString(value);
for (int i = 0; i < value.Length; i++)
{
newString[i].Foreground = ColorHelper.Lerp(startingForeground, endingForeground, (float)i / (float)value.Length);
}
newString.IgnoreBackground = true;
return newString;
}
/// <summary>
/// Creates a <see cref="ColoredString"/> object from an existing string with the specified foreground gradient, background gradient, and cell effect.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="startingForeground">The starting foreground color to blend.</param>
/// <param name="endingForeground">The ending foreground color to blend.</param>
/// <param name="startingBackground">The starting background color to blend.</param>
/// <param name="endingBackground">The ending background color to blend.</param>
/// <returns>A <see cref="ColoredString"/> object instace.</returns>
public static ColoredString CreateGradient(this string value, Color startingForeground, Color endingForeground, Color startingBackground, Color endingBackground)
{
ColoredString newString = new ColoredString(value);
for (int i = 0; i < value.Length; i++)
{
newString[i].Foreground = ColorHelper.Lerp(startingForeground, endingForeground, (float)i / (float)value.Length);
newString[i].Background = ColorHelper.Lerp(startingBackground, endingBackground, (float)i / (float)value.Length);
}
return newString;
}
/// <summary>
/// Converts a string to a boolean when it is "0", "1", "true", or "false".
/// </summary>
/// <param name="item">The string to convert</param>
/// <returns>The converted boolean value, otherwise false.</returns>
public static bool ToBool(this string item)
{
int intValue;
bool boolValue;
if (int.TryParse(item, out intValue))
return Convert.ToBoolean(intValue);
if (bool.TryParse(item, out boolValue))
return bool.Parse(item);
return false;
}
}
}