-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTui.cs
552 lines (489 loc) · 20.3 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
#nullable enable
namespace tui_netcore
{
public class Tui
{
// Box Width
public int Width { get; set; }
// Box Height
public int Height { get; set; }
// Position (left) of the first character of the box inside the terminal
public int PosLeft { get; set; }
// Position (top) of the first character of the box inside the terminal
public int PosTop { get; set; }
public string Title { get; set; }
public string Body { get; set; }
//Character Used to navigate, answer strings and select an option
public char AnswerChar { get; set; }
public char SelectedChar { get; set; }
public char HorizontalChar { get; set; }
public char VerticalChar { get; set; }
public char TopLeftChar { get; set; }
public char TopRightChar { get; set; }
public char BottomLeftChar { get; set; }
public char BottomRightChar { get; set; }
public char IntersectionChar { get; set; }
public char EmptyChar { get; set; }
public int MarginLeft { get; set; }
public int MarginTop { get; set; }
public BorderStyleBrush BorderStyle { get; set; } = BorderStyles.Text;
// * This is used to draw multiple lines below the body (checkbox/radio)
public int LastBodyHeight { get; set; }
public enum ColorSchema
{
Regular,
Info,
Warning,
Danger,
System
}
/// <summary>
/// This is the object used for each option in the checkbox and/or radio options
/// </summary>
public struct CheckBoxOption
{
public bool IsSelected { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public sealed class BorderStyleBrush
{
public char TopLeftChar { get; }
public char HorizontalChar { get; }
public char TopRightChar { get; }
public char VerticalChar { get; }
public char BottomLeftChar { get; }
public char BottomRightChar { get; }
public BorderStyleBrush(char topLeft, char horizontal, char topRight, char vertical, char bottomLeft, char bottomRight)
{
TopLeftChar = topLeft;
HorizontalChar = horizontal;
TopRightChar = topRight;
VerticalChar = vertical;
BottomLeftChar = bottomLeft;
BottomRightChar = bottomRight;
}
public void Deconstruct(out char topLeft, out char horizontal, out char topRight, out char vertical, out char bottomLeft, out char bottomRight)
{
topLeft = TopLeftChar;
horizontal = HorizontalChar;
topRight = TopRightChar;
vertical = VerticalChar;
bottomLeft = BottomLeftChar;
bottomRight = BottomRightChar;
}
}
public static class BorderStyles
{
public static readonly BorderStyleBrush Text = new BorderStyleBrush('+', '-', '+', '|', '+', '+');
public static readonly BorderStyleBrush SingleLine = new BorderStyleBrush('┌', '─', '┐', '│', '└', '┘');
public static readonly BorderStyleBrush DoubleLine = new BorderStyleBrush('╔', '═', '╗', '║', '╚', '╝');
}
public Tui(int width = 100, int height = 80, int posLeft = 0, int posTop = 0)
{
Width = width;
Height = height;
PosTop = posTop;
PosLeft = posLeft;
AnswerChar = '>';
SelectedChar = '*';
IntersectionChar = '+';
EmptyChar = ' ';
Title = String.Empty;
Body = String.Empty;
MarginLeft = 4;
MarginTop = 2;
LastBodyHeight = 0;
}
public Tui() : this(Console.WindowWidth, Console.WindowHeight, 0, 0)
{
}
/// <summary>
/// Defines a Color schema to the box
/// </summary>
/// <param name="color"></param>
private void setColorSchema(ColorSchema color)
{
switch (color)
{
case ColorSchema.Regular:
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
break;
case ColorSchema.Info:
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
break;
case ColorSchema.Warning:
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.ForegroundColor = ConsoleColor.White;
break;
case ColorSchema.Danger:
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.White;
break;
case ColorSchema.System:
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
break;
}
}
/// <summary>
/// Print the entire Screen, Use this to test your terminal
/// </summary>
public void TestSize(ColorSchema schema = ColorSchema.Info)
{
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
Console.SetCursorPosition(j, i);
System.Console.Write("+");
}
Console.SetCursorPosition((Width / 2) - 6, i);
setColorSchema(schema);
System.Console.Write($"Line {i}");
Console.ResetColor();
}
}
private void drawTitle()
{
Console.SetCursorPosition((Width / 2) - (Title.Length / 2), PosTop);
Console.Write(Title);
}
/// <summary>
/// Roughly break body text in pages, considering no line breaks.
/// If there are line breaks, the window will not be complete.
/// words may break abruptely
/// </summary>
/// <returns></returns>
private List<string> separatePages()
{
var pages = new List<string>();
int index = -1;
int totalSpace = (Width - (2 * MarginLeft)) * (Height - (2 * MarginTop) - 2);
foreach (var c in Body)
{
if (index == -1)
{
pages.Add(c.ToString());
index++;
}
else if (pages.Last().ToString().Count() - 1 < totalSpace)
{
pages[index] += c.ToString();
}
else
{
pages.Add(c.ToString());
index++;
}
}
return pages;
}
private void drawBody()
{
int usableSpace = Width - (2 * MarginLeft);
//* TODO: Improve the way the split is done to stay inside the box
//! TODO: "\n" Breaks the layout. Find a way to make this work correctly
int tmpLine = 0;
StringBuilder tmpText = new StringBuilder(usableSpace, usableSpace);
string[] tmpBody = Body.Split(' ');
foreach (string s in tmpBody)
{
try
{
if (s == "\n")
{
throw new ArgumentOutOfRangeException();
}
tmpText.Append(s);
tmpText.Append(" ");
}
catch (ArgumentOutOfRangeException)
{
Console.SetCursorPosition((PosLeft + MarginLeft), (PosTop + MarginTop + tmpLine));
System.Console.Write(tmpText);
tmpLine++;
tmpText.Clear();
}
}
Console.SetCursorPosition((PosLeft + MarginLeft), (PosTop + MarginTop + tmpLine));
LastBodyHeight = PosTop + MarginTop + tmpLine;
System.Console.Write(tmpText);
}
private void DrawCorners()
{
Console.SetCursorPosition(PosLeft, PosTop);
Console.Write(BorderStyle.TopLeftChar);
Console.SetCursorPosition(PosLeft + Width - 1, PosTop);
System.Console.Write(BorderStyle.TopRightChar);
Console.SetCursorPosition(PosLeft, PosTop + Height - 1);
System.Console.Write(BorderStyle.BottomLeftChar);
Console.SetCursorPosition(PosLeft + Width - 1, PosTop + Height - 1);
System.Console.Write(BorderStyle.BottomRightChar);
}
/// <summary>
/// This Frame wraps the entire screen, printing spaces inside.
/// </summary>
/// <param name="schema">Color Scheme to be used</param>
public void Draw(ColorSchema schema = ColorSchema.Regular)
{
setColorSchema(schema);
var headLine = new Char[Width];
var bodyLine = new Char[Width];
for (var i = 0; i < Width; ++i)
{
headLine[i] = i == 0 || i == Width - 1 ? BorderStyle.VerticalChar : BorderStyle.HorizontalChar;
bodyLine[i] = i == 0 || i == Width - 1 ? BorderStyle.VerticalChar : EmptyChar;
}
for (int i = PosTop; i < Height + PosTop; i++)
{
Console.SetCursorPosition(PosLeft, i);
Console.Write(i == PosTop || i == Height + PosTop - 1 ? headLine : bodyLine);
}
DrawCorners();
if (!String.IsNullOrEmpty(Title)) drawTitle();
if (!String.IsNullOrEmpty(Body)) drawBody();
setColorSchema(ColorSchema.Regular);
}
/// <summary>
/// This method Creates an input so the user can answer things
/// </summary>
/// <param name="schema"></param>
/// <returns>string containing the user answer</returns>
public string? DrawInput(ColorSchema schema = ColorSchema.Regular)
{
Draw(schema);
setColorSchema(schema);
Console.SetCursorPosition((PosLeft + MarginLeft), (PosTop + Height - MarginTop));
Console.Write($"{AnswerChar} ");
string? answer = Console.ReadLine();
setColorSchema(ColorSchema.Regular);
return answer;
}
/// <summary>
/// Prints message as book pages, based on window size.
/// Has three button options (next, previous, done) that are used to go forward, backward and exit scree
/// </summary>
/// <param name="schema">Color Schema</param>
/// <param name="txtNext">Text for Next option</param>
/// <param name="txtPrev">Text for Previous option</param>
/// <param name="txtDone">Text for Quit option</param>
/// <param name="defaultAnswer">0=Prev / 1=Next / 2=Done</param>
public void DrawBook(ColorSchema schema = ColorSchema.Regular, string txtNext = "next", string txtPrev = "previous", string txtDone = "Done", int defaultAnswer = 1)
{
setColorSchema(schema);
int answer = defaultAnswer;
int CursorPrev = PosLeft + MarginLeft;
int CursorDone = PosLeft + Width - (2 * MarginLeft);
int CursorNext = (PosLeft + Width) / 2;
int Line = (PosTop + Height - MarginTop);
int index = 0;
string oldTitle = Title;
List<string> pages = separatePages();
int totalPages = pages.Count();
Body = pages[0];
ConsoleKeyInfo keypress;
bool keepGoing = true;
while (keepGoing)
{
Title = oldTitle + $@" [ {index + 1}/{totalPages} ]";
Draw(schema);
Console.SetCursorPosition(CursorPrev + 1, Line);
Console.Write(txtPrev);
Console.SetCursorPosition(CursorNext + 1, Line);
Console.Write(txtNext);
Console.SetCursorPosition(CursorDone + 1, Line);
Console.Write(txtDone);
Console.SetCursorPosition(CursorPrev, Line);
Console.Write(answer == 0 ? AnswerChar : EmptyChar);
Console.SetCursorPosition(CursorNext, Line);
Console.Write(answer == 1 ? AnswerChar : EmptyChar);
Console.SetCursorPosition(CursorDone, Line);
Console.Write(answer == 2 ? AnswerChar : EmptyChar);
keypress = Console.ReadKey(false);
switch (keypress.Key)
{
case ConsoleKey.LeftArrow:
answer = answer - 1 >= 0 ? answer - 1 : 2;
break;
case ConsoleKey.RightArrow:
answer = answer + 1 <= 2 ? answer + 1 : 0;
break;
case ConsoleKey.Enter:
switch (answer)
{
case 0:
index = index - 1 >= 0 ? index - 1 : pages.Count() - 1;
break;
case 1:
index = index + 1 <= pages.Count() - 1 ? index + 1 : 0;
break;
case 2:
keepGoing = false;
break;
}
break;
}
Body = pages[index];
}
setColorSchema(ColorSchema.Regular);
}
/// <summary>
/// Draws a box with two options to answer (Yes/No)
/// Left/Right arrows to move between options
/// Enter to select option
/// </summary>
/// <param name="schema">Color Schema</param>
/// <param name="txtYes">Text to Yes option</param>
/// <param name="txtNo">Text to No option</param>
/// <param name="defaultAnswer">true=yes/false=no</param>
/// <returns>true=yes/false=no</returns>
public bool DrawYesNo(ColorSchema schema = ColorSchema.Regular, string txtYes = "Yes", string txtNo = "No", bool defaultAnswer = false)
{
Draw(schema);
setColorSchema(schema);
bool answer = defaultAnswer;
int CursorYes = PosLeft + MarginLeft;
int CursorNo = PosLeft + Width - (2 * MarginLeft);
int Line = (PosTop + Height - MarginTop);
ConsoleKeyInfo keypress;
do
{
Console.SetCursorPosition(CursorYes + 1, Line);
Console.Write(txtYes);
Console.SetCursorPosition(CursorNo + 1, Line);
Console.Write(txtNo);
Console.SetCursorPosition(CursorYes, Line);
Console.Write(answer ? AnswerChar : EmptyChar);
Console.SetCursorPosition(CursorNo, Line);
Console.Write(!answer ? AnswerChar : EmptyChar);
keypress = Console.ReadKey(false);
if ((keypress.Key == ConsoleKey.LeftArrow) || (keypress.Key == ConsoleKey.RightArrow))
{
answer = !answer;
}
} while (keypress.Key != ConsoleKey.Enter);
setColorSchema(ColorSchema.Regular);
return answer;
}
/// <summary>
/// This method draws the box, and wait the user press any key to continue
/// </summary>
/// <param name="schema">Color schema</param>
/// <param name="txtOk">Text to appear on the bottom of the box</param>
public void DrawOk(ColorSchema schema = ColorSchema.Regular, string txtOk = "Press any key to continue...")
{
Draw(schema);
setColorSchema(schema);
int CursorOk = PosLeft + MarginLeft;
int Line = (PosTop + Height - MarginTop);
Console.SetCursorPosition(CursorOk, Line);
Console.Write($"{AnswerChar}{txtOk}");
Console.ReadKey();
}
public List<CheckBoxOption> DrawCheckBox(List<CheckBoxOption> options, ColorSchema schema = ColorSchema.Regular, bool onlyChecked = true)
{
Draw(schema);
setColorSchema(schema);
int Line = LastBodyHeight + MarginTop;
int tmpCursor = 0;
List<CheckBoxOption> TmpOptions = options;
//Continue Here
foreach (CheckBoxOption o in TmpOptions)
{
char tmpSelected = o.IsSelected ? SelectedChar : EmptyChar;
Console.SetCursorPosition(MarginLeft + PosLeft, Line);
System.Console.Write($" [{tmpSelected}] {o.Name} - {o.Description}");
Line++;
}
Line -= options.Count;
ConsoleKeyInfo keypress;
do
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line + tmpCursor);
Console.Write(AnswerChar);
keypress = Console.ReadKey(true);
if (keypress.Key == ConsoleKey.UpArrow)
{
if (tmpCursor > 0)
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line + tmpCursor);
Console.Write(EmptyChar);
tmpCursor--;
}
}
if (keypress.Key == ConsoleKey.DownArrow)
{
if (tmpCursor < TmpOptions.Count - 1)
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line + tmpCursor);
Console.Write(EmptyChar);
tmpCursor++;
}
}
if (keypress.Key == ConsoleKey.Spacebar)
{
CheckBoxOption c = TmpOptions[tmpCursor];
c.IsSelected = !c.IsSelected;
char ch = c.IsSelected ? SelectedChar : EmptyChar;
Console.SetCursorPosition(MarginLeft + 2 + PosLeft, Line + tmpCursor);
TmpOptions[tmpCursor] = c;
Console.Write(ch);
}
} while (keypress.Key != ConsoleKey.Enter);
if (onlyChecked)
{
TmpOptions = (from t in TmpOptions where t.IsSelected select t).ToList();
}
setColorSchema(ColorSchema.Regular);
return TmpOptions;
}
public string DrawList(List<String> options, ColorSchema schema = ColorSchema.Regular)
{
Draw(schema);
setColorSchema(schema);
int Line = LastBodyHeight + MarginTop;
int tmpCursor = 0;
foreach (string s in options)
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line);
System.Console.Write($" {s}");
Line++;
}
Line -= options.Count;
ConsoleKeyInfo keypress;
do
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line + tmpCursor);
Console.Write(AnswerChar);
keypress = Console.ReadKey(true);
if (keypress.Key == ConsoleKey.UpArrow)
{
if (tmpCursor > 0)
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line + tmpCursor);
Console.Write(EmptyChar);
tmpCursor--;
}
}
if (keypress.Key == ConsoleKey.DownArrow)
{
if (tmpCursor < options.Count - 1)
{
Console.SetCursorPosition(MarginLeft + PosLeft, Line + tmpCursor);
Console.Write(EmptyChar);
tmpCursor++;
}
}
} while (keypress.Key != ConsoleKey.Enter);
setColorSchema(ColorSchema.Regular);
return options[tmpCursor];
}
}
}