This repository was archived by the owner on Feb 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIO.cs
More file actions
140 lines (139 loc) · 4.96 KB
/
IO.cs
File metadata and controls
140 lines (139 loc) · 4.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
using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Gerbil
{
namespace IO
{
class Out
{
/// <summary>
/// Writes a line of text to the CLI.
/// </summary>
/// <param name="input">Text to write.</param>
public static void writeln(string input)
{
write(input + "\n");
}
/// <summary>
/// Writes a string of text to the CLI.
/// </summary>
/// <param name="input">Text to write.</param>
public static void write(string input)
{
Console.Write(input);
}
/// <summary>
/// Writes a blank line to the CLI.
/// </summary>
public static void blank()
{
write("\n");
}
/// <summary>
/// Writes a number of blank lines to the CLI.
/// </summary>
/// <param name="iterations">Blank lines to insert.</param>
public static void blank(int iterations)
{
for (int i = 0; i < iterations; i++)
{
blank();
}
}
/// <summary>
/// Prints a formatted menu to the CLI.
/// </summary>
/// <param name="title">Title of menu.</param>
/// <param name="options">List of options to display.</param>
public static void printMenu(string title, params string[] options)
{
writeln(title);
for (int i = 0; i < options.Length; i++)
{
writeln(i + " - " + options[i]);
}
}
}
class In
{
/// <summary>
/// Prompts the user for input using a formatted graphical menu.
/// </summary>
/// <param name="title">Title of menu.</param>
/// <param name="options">List of options to display.</param>
/// <returns>Zero-indexed choice selected by user. (-1 if none)</returns>
public static int menu(string title, params string[] options)
{
Out.printMenu(title, options);
Out.writeln("-1 to cancel.");
int result = 0;
while (true)
{
result = prompt<int>("Option");
if(result >= -1 && result < options.Length)
{
return result;
}
Out.writeln("Invalid input, enter a valid menu choice.");
}
}
/// <summary>
/// Prompts the user for input.
/// </summary>
/// <typeparam name="T">Type of variable to return.</typeparam>
/// <param name="prompt">Prompt to display to user.</param>
/// <returns>Input value by user.</returns>
public static T prompt<T>(string prompt)
{
return prompt<T>(prompt, ':');
}
/// <summary>
/// Prompts the user for input.
/// </summary>
/// <typeparam name="T">Type of variable to return.</typeparam>
/// <param name="prompt">Prompt to display to user.</param>
/// <param name="promptKey">Prompt char to display.</param>
/// <returns>Input value by user.</returns>
public static T prompt<T>(string prompt, char promptKey)
{
while (true)
{
Out.write(prompt + promptKey + " ");
string inval = Console.ReadLine();
try
{
T store = (T)Convert.ChangeType(inval, typeof(T));
return store;
}
catch
{
Out.writeln("Invalid input. Please enter a valid input.");
}
}
}
/// <summary>
/// Prompts the user to perfom a dangerous or uncertain task.
/// </summary>
/// <param name="module">Name of module seeking permission.</param>
/// <param name="action">Action being taken by the module.</param>
/// <returns>Action allowed.</returns>
public static bool securePrompt(string module, string action)
{
Out.writeln(String.Format("GERBIL SECURITY: Module {0} is attempting to {1}.", module, action));
int result = menu("Allow action?", "Yes", "No");
if(result == 0)
{
return true;
}
else
{
return false;
}
}
}
}
}