forked from netwrix/pingcastle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleMenu.cs
360 lines (338 loc) · 9.5 KB
/
ConsoleMenu.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace PingCastle
{
public class ConsoleMenuItem
{
public string Choice { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public ConsoleMenuItem(string choice, string shortDescription)
: this(choice, shortDescription, null)
{
}
public ConsoleMenuItem(string choice, string shortDescription, string longDescription)
{
Choice = choice;
ShortDescription = shortDescription;
LongDescription = longDescription;
}
}
public class ConsoleMenu
{
public static string Header { get; set; }
public static string Title { get; set; }
public static string Notice { get; set; }
public static string Information { get; set; }
static void printSelectMenuStyle0(List<ConsoleMenuItem> items, int currentIndex, int top, int left)
{
bool hasDescription = false;
string description = null;
int largerChoice = 0;
int maxDescription = 0;
for (int i = 0; i < items.Count; i++)
{
if (!String.IsNullOrEmpty(items[i].ShortDescription))
hasDescription = true;
int l = items[i].Choice.Length;
if (l > largerChoice)
largerChoice = l;
}
Console.SetCursorPosition(left, top);
for (int i = 0; i < items.Count; i++)
{
if (i == currentIndex - 1)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
description = items[i].LongDescription;
}
if (!String.IsNullOrEmpty(items[i].LongDescription) && maxDescription < items[i].LongDescription.Length)
maxDescription = items[i].LongDescription.Length;
Console.Write(" " + (char)(i < 9 ? i + '1' : i - 9 + 'a') + "-" + items[i].Choice);
if (hasDescription)
{
int diff = largerChoice - items[i].Choice.Length;
if (diff > 0)
Console.Write(new String(' ', diff));
if (!String.IsNullOrEmpty(items[i].ShortDescription))
Console.Write("-" + items[i].ShortDescription);
}
Console.WriteLine();
Console.ResetColor();
}
if (0 == currentIndex)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
}
Console.WriteLine(" 0-Exit");
Console.ResetColor();
if (!String.IsNullOrEmpty(description))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("==============================");
Console.ResetColor();
int currentLineCursor = Console.CursorTop;
Console.WriteLine(new string(' ', maxDescription));
Console.SetCursorPosition(0, currentLineCursor);
Console.WriteLine(description);
}
else
{
Console.WriteLine(new string(' ', Console.WindowWidth - 1));
Console.WriteLine(new string(' ', maxDescription));
}
}
static void printSelectMenuStyle1(List<ConsoleMenuItem> items, int currentIndex, int top, int left)
{
string description = null;
Console.SetCursorPosition(left, top);
string item;
int maxDescription = 0;
for (int i = 0; i < items.Count; i++)
{
if (i == currentIndex -1)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
description = items[i].ShortDescription;
}
if (!String.IsNullOrEmpty(items[i].ShortDescription) && maxDescription < items[i].ShortDescription.Length)
maxDescription = items[i].ShortDescription.Length;
item = " " + (char)(i < 9 ? i + '1' : i - 9 + 'a') + "-" + items[i].Choice;
Console.SetCursorPosition(left + (i < (items.Count + 1) / 2 ? 0 : Console.WindowWidth / 2), top + i + (i < (items.Count + 1) / 2 ? 0 : -(items.Count + 1) / 2));
Console.Write(item + new string(' ',Console.WindowWidth / 2 - item.Length - 1));
Console.ResetColor();
}
if (0 == currentIndex)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
}
Console.SetCursorPosition(left, top + (items.Count+1) /2);
item = " 0-Exit";
Console.WriteLine(item + new string(' ', Console.WindowWidth / 2 - item.Length - 1));
Console.ResetColor();
if (!String.IsNullOrEmpty(description))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("==============================");
Console.ResetColor();
int currentLineCursor = Console.CursorTop;
Console.WriteLine(new string(' ', maxDescription));
Console.SetCursorPosition(0, currentLineCursor);
Console.WriteLine(description);
}
}
protected static void DisplayHeader()
{
Console.Clear();
if (!String.IsNullOrEmpty(Header))
{
Console.WriteLine(Header);
}
if (!String.IsNullOrEmpty(Title))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(Title);
Console.WriteLine(new string('=', Title.Length));
Console.ResetColor();
}
if (!String.IsNullOrEmpty(Information))
{
Console.WriteLine(Information);
}
if (!String.IsNullOrEmpty(Notice))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Notice);
Console.ResetColor();
}
}
private static void ClearTopic()
{
Information = null;
Notice = null;
Title = null;
}
public static string AskForString()
{
DisplayHeader();
ClearTopic();
return Console.ReadLine();
}
public static List<string> AskForListString()
{
DisplayHeader();
ClearTopic();
var list = new List<string>();
var line = Console.ReadLine();
while (!String.IsNullOrEmpty(line))
{
list.Add(line);
line = Console.ReadLine();
}
return list;
}
public static int SelectMenu(List<ConsoleMenuItem> items, int defaultIndex = 1)
{
DisplayHeader();
ClearTopic();
return SelectMenu(items, defaultIndex, 0);
}
public static int SelectMenuCompact(List<ConsoleMenuItem> items, int defaultIndex = 1)
{
DisplayHeader();
ClearTopic();
return SelectMenu(items, defaultIndex, 1);
}
protected static int SelectMenu(List<ConsoleMenuItem> items, int defaultIndex = 1, int style = 0)
{
int top = Console.CursorTop;
int left = Console.CursorLeft;
int index = defaultIndex;
Console.CursorVisible = false;
while (true)
{
switch (style)
{
case 1:
printSelectMenuStyle1(items, index, top, left);
break;
case 0:
default:
printSelectMenuStyle0(items, index, top, left);
break;
}
ConsoleKeyInfo ckey = Console.ReadKey(true);
if (ckey.Key == ConsoleKey.Escape)
{
Console.CursorVisible = true;
Console.ResetColor();
return 0;
}
if (ckey.Key == ConsoleKey.DownArrow)
{
if (index == items.Count)
{
index = 0; // exit key
}
else if (style == 1 && index == (items.Count + 1)/2)
{
index = 0; // exit key
}
else if (index == 0)
{
}
else { index++; }
}
else if (ckey.Key == ConsoleKey.UpArrow)
{
if (index == 1)
{
}
else if (index == 0)
{
if (style == 1)
{
index = (items.Count + 1)/ 2;
}
else
{
index = items.Count;
}
}
else { index--; }
}
else if (ckey.Key == ConsoleKey.LeftArrow && style == 1)
{
if (index >= (items.Count + 1) / 2)
{
index -= (items.Count + 1) / 2;
}
}
else if (ckey.Key == ConsoleKey.RightArrow && style == 1)
{
if (index <= (items.Count ) / 2)
{
index += (items.Count + 1) / 2;
}
}
else if (ckey.Key == ConsoleKey.Enter)
{
Console.CursorVisible = true;
Console.ResetColor();
return index;
}
else
{
int number;
char key = ckey.KeyChar;
if (Int32.TryParse(key.ToString(), out number) && number >= 0 && number <= 9 && (number <= items.Count))
{
Console.CursorVisible = true;
Console.ResetColor();
return number;
}
if (key >= 'a' && key <= 'z' && ((key - 'a' + 10) <= items.Count))
{
Console.CursorVisible = true;
Console.ResetColor();
return (key - 'a' + 10);
}
if (key >= 'A' && key <= 'Z' && ((key - 'A' + 10) <= items.Count))
{
Console.CursorVisible = true;
Console.ResetColor();
return (key - 'A' + 10);
}
}
}
}
// http://msdn.microsoft.com/en-us/library/ms680313
#pragma warning disable 0649
struct _IMAGE_FILE_HEADER
{
public ushort Machine;
public ushort NumberOfSections;
public uint TimeDateStamp;
public uint PointerToSymbolTable;
public uint NumberOfSymbols;
public ushort SizeOfOptionalHeader;
public ushort Characteristics;
};
#pragma warning restore 0649
public static DateTime GetBuildDateTime(Assembly assembly)
{
var path = assembly.Location;
if (File.Exists(path))
{
var buffer = new byte[Marshal.SizeOf(typeof(_IMAGE_FILE_HEADER))];
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
fileStream.Position = 0x3C;
fileStream.Read(buffer, 0, 4);
fileStream.Position = BitConverter.ToUInt32(buffer, 0); // COFF header offset
fileStream.Read(buffer, 0, 4); // "PE\0\0"
fileStream.Read(buffer, 0, buffer.Length);
}
var pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
try
{
var coffHeader = (_IMAGE_FILE_HEADER)Marshal.PtrToStructure(pinnedBuffer.AddrOfPinnedObject(), typeof(_IMAGE_FILE_HEADER));
return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1) + new TimeSpan(coffHeader.TimeDateStamp * TimeSpan.TicksPerSecond));
}
finally
{
pinnedBuffer.Free();
}
}
return new DateTime();
}
}
}