69 lines
2.5 KiB
C#
69 lines
2.5 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);
|
|
}
|
|
private static (bool,int) read_small_number() {
|
|
var key = Console.ReadKey();
|
|
return (Int32.TryParse(key.KeyChar.ToString(), out int value), value);
|
|
}
|
|
private static (bool,int) read_big_number() {
|
|
var line = Console.ReadLine();
|
|
return (Int32.TryParse(line, out int value), value);
|
|
}
|
|
private static int read_number(int max) {
|
|
do {
|
|
(var success,var value) = max > 9 ? read_big_number() : read_small_number();
|
|
if (success && value >= 0 && value <= max) {
|
|
return value;
|
|
}
|
|
}while(true);
|
|
}
|
|
|
|
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 = read_number(options.Count());
|
|
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;
|
|
}
|
|
|
|
|
|
}
|
|
} |