-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
166 lines (134 loc) · 5.96 KB
/
Copy pathLogger.cs
File metadata and controls
166 lines (134 loc) · 5.96 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using nyoka_Client;
using System.Collections.Generic;
using System.Linq;
public static class Logger
{
public static void logWarning(string message)
{
internalWriteLine($"WARNING: {message}", Constants.warningColor);
}
public static void logError(string message)
{
internalWriteLine($"ERROR: {message}", Constants.errorColor);
}
public static bool askYesOrNo(string question, bool acceptEnterAsYes = true)
{
bool? response = null;
while (!response.HasValue)
{
System.Console.ResetColor();
System.Console.ForegroundColor = Constants.questionColor;
System.Console.Write(question + " [y/n] ");
// internalWriteLine(question + " [y/n] ", questionColor);
string input = System.Console.ReadLine();
if (input.Trim().ToLower() == "y" || (acceptEnterAsYes && input.Trim() == ""))
{
response = true;
}
else if (input.Trim().ToLower() == "n")
{
response = false;
}
else
{
internalWriteLine($"Invalid response \"{input.Trim()}\": type in \"y\" or \"n\"", Constants.errorColor);
}
}
return response.Value;
}
public static void writeBottomLineOverwriteExisting(string str)
{
System.Console.ResetColor();
System.Console.Write("\r" + str.PadRight(System.Console.WindowWidth));
}
public static void logLine(string str)
{
internalWriteLine(str);
}
private static void internalWriteLine(string str, System.ConsoleColor foregroundColor)
{
List<System.ConsoleColor> charColors = Enumerable.Repeat(foregroundColor, str.Length).ToList();
internalWriteLine(str, charColors);
}
private static void internalWriteLine(string str) => internalWriteLine(str, System.Console.ForegroundColor);
private static void internalWriteLine(string str, List<System.ConsoleColor> charColors)
{
// number of returns to print
int returnCount = str.Count(ch => ch == '\n');
// create extra returns at bottom of screen and moves cursor to starting place
System.Console.ResetColor();
System.ConsoleColor currentColor = System.Console.ForegroundColor;
for (int i = 0; i < str.Length; i++)
{
if (currentColor != charColors[i])
{
currentColor = charColors[i];
System.Console.ForegroundColor = charColors[i];
}
System.Console.Write(str[i]);
}
System.Console.ResetColor();
System.Console.Write("\n");
}
public static void logTable(PrintTable table, bool visibleLines = true)
{
char verticalSeparator = visibleLines ? '|' : ' ';
char horizontalSeparator = visibleLines ? '=' : ' ';
char lineIntersectionChar = visibleLines ? '+' : ' ';
string headerLine = "";
List<System.ConsoleColor> headerLineCharColors = new List<System.ConsoleColor>();
for (int col = 0; col < table.columnNames.Count; col++)
{
headerLine += verticalSeparator;
headerLineCharColors.Add(Constants.tableFrameColor);
// don't pad rightmost header if lines are not supposed to be visible
if (visibleLines || col != table.columnNames.Count - 1)
{
string nameStr = table.columnNames[col].PadRight(table.columnWidths[col]);
headerLine += nameStr;
headerLineCharColors.AddRange(Enumerable.Repeat(Constants.tableHeaderColor, nameStr.Length));
}
else
{
string paddedNameStr = table.columnNames[col];
headerLine += paddedNameStr;
headerLineCharColors.AddRange(Enumerable.Repeat(Constants.tableHeaderColor, paddedNameStr.Length));
}
}
headerLine += verticalSeparator;
headerLineCharColors.Add(Constants.tableFrameColor);
internalWriteLine(headerLine, headerLineCharColors);
string horizontalLine = "";
// log line
if (visibleLines)
{
for (int col = 0; col < table.columnNames.Count; col++)
{
horizontalLine += lineIntersectionChar;
horizontalLine += new string(horizontalSeparator, table.columnWidths[col]);
if (col == table.columnNames.Count - 1) horizontalLine += lineIntersectionChar;
}
internalWriteLine(horizontalLine, Constants.tableFrameColor);
}
else
{
internalWriteLine("");
}
foreach (List<string> row in table.rows)
{
string lineString = "";
List<System.ConsoleColor> lineCharColors = new List<System.ConsoleColor>();
for (int col = 0; col < table.columnWidths.Count; col++)
{
lineString += verticalSeparator;
lineCharColors.Add(Constants.tableFrameColor);
string tableValue = row[col].PadRight(table.columnWidths[col]);
lineString += tableValue;
lineCharColors.AddRange(Enumerable.Repeat(System.Console.ForegroundColor, tableValue.Length));
}
lineString += verticalSeparator;
lineCharColors.Add(Constants.tableFrameColor);
internalWriteLine(lineString, lineCharColors);
}
}
}