58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace bts.test {
|
|
public static class ConsoleEx {
|
|
public static void WriteLine(string line, ConsoleColor foreground=ConsoleColor.Gray, ConsoleColor background=ConsoleColor.Black) {
|
|
Write(line, foreground, background);
|
|
Console.WriteLine();
|
|
}
|
|
public static void Write(string line, ConsoleColor foreground=ConsoleColor.Gray, ConsoleColor background=ConsoleColor.Black) {
|
|
var init_fg = Console.ForegroundColor;
|
|
var init_bg = Console.BackgroundColor;
|
|
try {
|
|
Console.ForegroundColor = foreground;
|
|
Console.BackgroundColor = background;
|
|
Console.Write(line);
|
|
}
|
|
finally {
|
|
Console.ForegroundColor = init_fg;
|
|
Console.BackgroundColor = init_bg;
|
|
}
|
|
}
|
|
|
|
public static T prompt<T>(string prompt, params T[] options) {
|
|
return ConsoleEx.prompt(prompt, (IEnumerable<T>)options);
|
|
}
|
|
public static T prompt<T>(string prompt, IEnumerable<T> options) {
|
|
WriteLine(prompt, ConsoleColor.Yellow);
|
|
for (int x = 0; x < options.Count(); ++x) {
|
|
Write($"{x}. ", ConsoleColor.Yellow);
|
|
WriteLine(options.ElementAt(x).ToString());
|
|
}
|
|
int response = 0;
|
|
bool success = false;
|
|
while(!success) {
|
|
var key = Console.ReadKey();
|
|
success = Int32.TryParse(key.KeyChar.ToString(), out response);
|
|
if (response > options.Count()) success = false;
|
|
}
|
|
var value = options.ElementAt(response);
|
|
Console.WriteLine(" >> " + value.ToString());
|
|
Console.WriteLine();
|
|
return value;
|
|
}
|
|
|
|
public static string input_prompt(string prompt, string default_result) {
|
|
Write(prompt + " (", ConsoleColor.Yellow);
|
|
Write(default_result, ConsoleColor.White);
|
|
Write(") ", ConsoleColor.Yellow);
|
|
var result = Console.ReadLine();
|
|
if (string.IsNullOrWhiteSpace(result)) return default_result;
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
} |