-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisplay.cs
More file actions
116 lines (99 loc) · 3.04 KB
/
Display.cs
File metadata and controls
116 lines (99 loc) · 3.04 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
using System.Text.RegularExpressions;
namespace VirtualShellReader;
public class Display
{
public enum AnsiEscape : int
{
reset = 0,
bold = 1,
disable = 2,
underline = 3,
reverse = 4,
strikethrough = 5,
invisible = 6
}
public enum AnsiForeground : int
{
black = 30,
red = 31,
green = 32,
orange = 33,
blue = 34,
purple = 35,
cyan = 36,
lightgrey = 37,
darkgrey = 90,
lightred = 91,
lightgreen = 92,
yellow = 93,
lightblue = 94,
pink = 95,
lightcyan = 96,
white = 97
}
public enum AnsiBackground : int
{
black = 40,
red = 41,
green = 42,
orange = 43,
blue = 44,
purple = 45,
cyan = 46,
lightgrey = 47
}
public AnsiForeground? Foreground { get; set; }
public AnsiBackground? Background { get; set; }
public Display(AnsiForeground foreground, AnsiBackground background)
{
this.Foreground = foreground;
this.Background = background;
}
private string highlight(string text)
{
string backg = Background != null ? $"{(int)Background};" : "";
string forg = Foreground != null ? $"{(int)Foreground}" : "97";
return $"\u001b[{backg}{forg}m{text}\u001b[0m";
}
private string[] splitLongLines(string line, int maxLength)
{
int lineLenght = RemoveColorCodes(line).Length;
if (lineLenght <= maxLength)
return new[] { line };
var lines = new System.Collections.Generic.List<string>();
for (int i = 0; i < lineLenght; i += maxLength)
{
lines.Add(line.Substring(i, Math.Min(maxLength, lineLenght - i)));
}
return lines.ToArray();
}
private string createBox(string text)
{
int maxLength = Console.WindowWidth - 4;
string[] lines = text.Split('\n');
string output = "";
int boxWidth = Math.Min(maxLength, lines.Max(line => line.Length) + 4);
output += highlight(" ╔" + new string('═', boxWidth - 2) + "╗ ") + "\n";
foreach (string line in lines)
{
var splittedLines = splitLongLines(line, boxWidth - 4);
foreach (var splitLine in splittedLines)
{
string removedExtra = RemoveColorCodes(splitLine);
int extra = splitLine.Length - removedExtra.Length;
output += highlight(" ║ ") + splitLine.PadRight(boxWidth - 4 + extra) + highlight(" ║ ") + "\n";
}
}
output += highlight(" ╚" + new string('═', boxWidth - 2) + "╝ ") + "\n";
return output;
}
private string RemoveColorCodes(string input)
{
string pattern = @"\u001b\[[0-9;]*m";
return Regex.Replace(input, pattern, string.Empty);
}
public void Print(string title, string output)
{
Console.WriteLine("\n" + highlight($" {title} ") + "\n" + createBox(output));
}
}