-
Notifications
You must be signed in to change notification settings - Fork 1
/
TUI.cs
91 lines (74 loc) · 2.35 KB
/
TUI.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
namespace MUI;
public static class TUI
{
private static bool cursorVisible = false;
public static bool CursorVisible { get=>cursorVisible; set=>cursorVisible=value; }
private static ConsoleColor uIColour = ConsoleColor.White;
public static ConsoleColor UIColour { get => uIColour; set => uIColour = value; }
// TODO: investigate improved line wrapping
public static void Message(string text = "", uint level = 0, bool invert = false)
{
ConsoleColor targetColour = level switch
{
1 => ConsoleColor.Green,
2 => ConsoleColor.Yellow,
3 => ConsoleColor.Red,
4 => UIColour,
_ => ConsoleColor.White
};
Console.ForegroundColor = invert ? ConsoleColor.Black : targetColour;
Console.BackgroundColor = invert ? targetColour : ConsoleColor.Black;
Write(text);
Reset();
}
// TODO: tests for this.
public static string Bar(double count, double total, uint width = 0)
{
if(width==0)
{width = (uint)Console.WindowWidth;}
width = width-=2;
string bar = "|".PadLeft((int)((count/total)*width),'=');
return $"[{bar.PadRight((int)width)}]";
}
public static void Write(string text)
{
try{
if(text.Length>=Console.WindowWidth)
{
Console.WriteLine(text.Remove(Console.WindowWidth,text.Length-Console.WindowWidth));
}else{
Console.WriteLine(text.PadRight(Console.WindowWidth));
}
}catch{
AskString("Console Write Error!");
}
}
private static string Ask(string text,string input, uint level, bool inline)
{
string _text = text+((input==null)?"":" ["+input+"]");
Message(_text,level);
if(inline) Console.SetCursorPosition(_text.Length+1,Console.CursorTop-1);
string output = Console.ReadLine();
if(output=="")
return input;
return output;
}
public static string AskString(string text ="", object input =null, uint level =4, bool inline =false)
{
return Ask(text,input?.ToString(),level,inline);
}
public static int AskInt(string text ="", object input =null, uint level =4, bool inline =false)
{
return int.Parse(Ask(text,input?.ToString(),level,inline));
}
public static uint AskUint(string text ="", object input =null, uint level =4, bool inline =false)
{
return uint.Parse(Ask(text,input?.ToString(),level,inline));
}
public static void Reset()
{
Console.ForegroundColor = UIColour;
Console.BackgroundColor = ConsoleColor.Black;
Console.CursorVisible = CursorVisible;
}
}