2017-05-22 23:35:55 +02:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace SteamGuard
|
|
|
|
{
|
|
|
|
public static class Utils
|
|
|
|
{
|
|
|
|
public static string ReadLineSecure()
|
|
|
|
{
|
|
|
|
string text = "";
|
|
|
|
ConsoleKeyInfo key;
|
|
|
|
int cursorIndex = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
key = Console.ReadKey(true);
|
2017-06-26 03:38:22 +02:00
|
|
|
if (((int)key.Key) >= 65 && ((int)key.Key <= 90))
|
2017-05-22 23:35:55 +02:00
|
|
|
{
|
2017-06-26 03:38:22 +02:00
|
|
|
if (cursorIndex == 0 || cursorIndex == text.Length - 1)
|
|
|
|
{
|
|
|
|
text += key.KeyChar.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text.Insert(cursorIndex, key.KeyChar.ToString());
|
|
|
|
}
|
2017-05-22 23:35:55 +02:00
|
|
|
cursorIndex++;
|
|
|
|
}
|
|
|
|
else if (key.Key == ConsoleKey.Backspace && cursorIndex > 0)
|
|
|
|
{
|
|
|
|
text.Remove(cursorIndex - 1, 1);
|
|
|
|
cursorIndex--;
|
|
|
|
}
|
|
|
|
else if (key.Key == ConsoleKey.RightArrow)
|
|
|
|
{
|
|
|
|
cursorIndex++;
|
|
|
|
}
|
|
|
|
else if (key.Key == ConsoleKey.LeftArrow)
|
|
|
|
{
|
|
|
|
cursorIndex--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursorIndex < 0)
|
|
|
|
{
|
|
|
|
cursorIndex = 0;
|
|
|
|
}
|
2017-06-26 03:38:22 +02:00
|
|
|
else if (text.Length == 0 && cursorIndex > text.Length)
|
|
|
|
{
|
|
|
|
cursorIndex = 0;
|
|
|
|
}
|
|
|
|
else if (text.Length != 0 && cursorIndex >= text.Length)
|
2017-05-22 23:35:55 +02:00
|
|
|
{
|
|
|
|
cursorIndex = text.Length - 1;
|
|
|
|
}
|
2017-06-26 03:38:22 +02:00
|
|
|
|
|
|
|
// For debugging:
|
|
|
|
// Console.Title = string.Format("{0}/{1} - {2} ({3}) - {4}", cursorIndex, text.Length, key.Key.ToString(), (int)key.Key, text);
|
2017-05-22 23:35:55 +02:00
|
|
|
} while (key.Key != ConsoleKey.Enter);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|