fix #18, ReadLineSecure causes ArgumentOutOfRangeException in setup

This commit is contained in:
Carson McManus 2017-06-25 21:38:22 -04:00
parent 1f4f7901a1
commit 79d477ac3a

View file

@ -12,9 +12,16 @@ namespace SteamGuard
do do
{ {
key = Console.ReadKey(true); key = Console.ReadKey(true);
if ((int)key.Key >= 65 && (int)key.Key <= 90) if (((int)key.Key) >= 65 && ((int)key.Key <= 90))
{
if (cursorIndex == 0 || cursorIndex == text.Length - 1)
{
text += key.KeyChar.ToString();
}
else
{ {
text.Insert(cursorIndex, key.KeyChar.ToString()); text.Insert(cursorIndex, key.KeyChar.ToString());
}
cursorIndex++; cursorIndex++;
} }
else if (key.Key == ConsoleKey.Backspace && cursorIndex > 0) else if (key.Key == ConsoleKey.Backspace && cursorIndex > 0)
@ -35,10 +42,17 @@ namespace SteamGuard
{ {
cursorIndex = 0; cursorIndex = 0;
} }
else if (cursorIndex > text.Length) else if (text.Length == 0 && cursorIndex > text.Length)
{
cursorIndex = 0;
}
else if (text.Length != 0 && cursorIndex >= text.Length)
{ {
cursorIndex = text.Length - 1; cursorIndex = text.Length - 1;
} }
// For debugging:
// Console.Title = string.Format("{0}/{1} - {2} ({3}) - {4}", cursorIndex, text.Length, key.Key.ToString(), (int)key.Key, text);
} while (key.Key != ConsoleKey.Enter); } while (key.Key != ConsoleKey.Enter);
return text; return text;
} }