-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleManager.cs
150 lines (130 loc) · 4.27 KB
/
ConsoleManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Shorthander
{
public static class ConsoleColorExtension
{
public static void Apply(this ConsoleManager.ConsoleColor color)
{
if (color == null)
Console.ResetColor();
else
{
Console.BackgroundColor = color.BackgroundColor;
Console.ForegroundColor = color.ForegroundColor;
}
}
}
public static class ConsoleManager
{
private const string HEX_CHARS = "0123456789ABCDEF";
public class ConsoleColor
{
public System.ConsoleColor ForegroundColor { get; set; }
public System.ConsoleColor BackgroundColor { get; set; }
}
private static ConsoleColor _defaultColor = null;
public static ConsoleColor DefaultColor
{
get => _defaultColor;
set
{
_defaultColor = value;
value.Apply();
Console.Clear();
}
}
static ConsoleManager()
{
Colors = new Dictionary<string, ConsoleColor>();
for (int i = 0; i < HEX_CHARS.Length; i++)
{
for (int j = 0; j < HEX_CHARS.Length; j++)
{
Colors.Add($"&{HEX_CHARS[i]}{HEX_CHARS[j]}", new ConsoleColor
{
BackgroundColor = (System.ConsoleColor)i,
ForegroundColor = (System.ConsoleColor)j
});
}
}
}
public static IDictionary<string, ConsoleColor> Colors { get; }
public static string GetForegroundColor(System.ConsoleColor color)
{
var f = HEX_CHARS[(int)color];
return DefaultColor == null ? "&0" + f : "&" + HEX_CHARS[(int)DefaultColor.BackgroundColor] + f;
}
public static void Error(string message)
{
var color = GetForegroundColor(System.ConsoleColor.DarkRed);
Write(color + message);
Console.ReadKey(true);
}
public static void Info(string message)
{
var color = GetForegroundColor(System.ConsoleColor.DarkGreen);
Write(color + message);
Console.ReadKey(true);
}
public static string Prompt(string message)
{
WriteLine(message);
Write(StaticData.CURSOR);
Colors[GetForegroundColor(System.ConsoleColor.DarkYellow)].Apply();
var result = Console.ReadLine();
DefaultColor.Apply();
return result;
}
public static void WriteLine(IEnumerable<string> messages)
{
Write(messages);
Console.WriteLine();
}
public static void WriteLine(string message)
{
Write(message);
Console.WriteLine();
}
public static void Write(IEnumerable<string> messages)
{
foreach (var message in messages)
{
Write(message);
}
}
public static void Write(string message)
{
var writeEnd = true;
for (int i = 0; i < message.Length - 2; i++)
{
if (message[i] == '&')
{
var valid = false;
var key = new string(message.ToArray(), i, 3);
if (Colors.ContainsKey(key))
{
Colors[key].Apply();
i += 2;
valid = true;
}
else if (key == "&NL")
{
Console.WriteLine();
i += 2;
valid = true;
}
if (valid && i == message.Length - 1) writeEnd = false;
}
else
{
Console.Write(message[i]);
}
}
if (writeEnd) Console.Write(message[message.Length - 2].ToString() + message[message.Length - 1].ToString());
DefaultColor.Apply();
}
}
}