-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathConsoleStyle.cs
More file actions
76 lines (72 loc) · 1.69 KB
/
ConsoleStyle.cs
File metadata and controls
76 lines (72 loc) · 1.69 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
namespace BytecodeApi.ConsoleUI;
/// <summary>
/// Represents a style for console text.
/// </summary>
public sealed class ConsoleStyle
{
/// <summary>
/// Gets or sets the text color.
/// </summary>
public ConsoleColor Foreground { get; set; }
/// <summary>
/// Gets or sets the background color.
/// </summary>
public ConsoleColor Background { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleStyle" /> class.
/// </summary>
public ConsoleStyle() : this(ConsoleColor.Gray)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleStyle" /> class.
/// </summary>
/// <param name="foreground">The text color.</param>
public ConsoleStyle(ConsoleColor foreground) : this(foreground, ConsoleColor.Black)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleStyle" /> class.
/// </summary>
/// <param name="foreground">The text color.</param>
/// <param name="background">The background color.</param>
public ConsoleStyle(ConsoleColor foreground, ConsoleColor background)
{
Foreground = foreground;
Background = background;
}
internal void Write(string value)
{
Do(() => Console.Write(value));
}
internal void WriteLine(string value)
{
Write(value);
Console.WriteLine();
}
internal string ReadLine()
{
return Do(() => Console.ReadLine() ?? "");
}
private void Do(Action action)
{
Do<object?>(() =>
{
action();
return null;
});
}
private T Do<T>(Func<T> func)
{
try
{
Console.ForegroundColor = Foreground;
Console.BackgroundColor = Background;
return func();
}
finally
{
Console.ResetColor();
}
}
}