-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminalScreen.cs
123 lines (100 loc) · 3.57 KB
/
TerminalScreen.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Zeptocom.App
{
public class TerminalScreen
{
readonly static MenuItem[] _items = { new MenuItem("Exit", "ESC"),
new MenuItem("About", "F1"),
new MenuItem("Clear", "F2"),};
private string _title;
public TerminalScreen(string title)
{
_title = title;
Reset();
}
public void Reset()
{
lock (this)
{
Console.ResetColor();
Console.Clear();
DrawTitle(_title);
DrawMenu(_items);
Console.CursorVisible = false;
Console.SetCursorPosition(0, 1);
}
}
private static void DrawTitle(string title)
{
var midScreenPoint = Console.WindowWidth / 2;
Console.SetCursorPosition(midScreenPoint - (title.Length / 2), 0);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(title);
Console.ResetColor();
}
public record MenuItem(string Name, string Hotkey);
private static void DrawMenu(MenuItem[] items)
{
Console.SetCursorPosition(0, Console.WindowHeight - 1);
foreach (var item in items)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write(item.Hotkey);
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write(item.Name);
Console.ResetColor();
Console.Write(" ");
}
}
public void WriteLine(string str = "")
{
this.Write(str + "\r\n");
}
public void Write(string str = "")
{
lock (this)
{
int startInx = 0;
string subStr;
var endInx = str.IndexOf('\r', startInx);
while (endInx != -1)
{
ScrollBuffer();
subStr = str.Substring(startInx, endInx - startInx);
Console.WriteLine(subStr);
startInx = endInx + 2;
if (startInx > str.Length)
break;
endInx = str.IndexOf('\r', startInx);
Thread.Sleep(10);
}
if (startInx < str.Length)
{
ScrollBuffer();
subStr = str.Substring(startInx);
Console.Write(subStr);
}
} }
private static void ScrollBuffer()
{
// Check if lines reached Menu Line or not
if (Console.GetCursorPosition().Top == Console.WindowHeight - 1)
{
#pragma warning disable CA1416 // Validate platform compatibility
// Move Buffer one line above
Console.MoveBufferArea(0, 2, Console.WindowWidth, Console.WindowHeight - 3, 0, 1);
#pragma warning restore CA1416 // Validate platform compatibility
Console.SetCursorPosition(0, Console.WindowHeight - 2);
}
}
}
}