From 79d477ac3a19b906895ed4aaa0bfbd739ada01dc Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 25 Jun 2017 21:38:22 -0400 Subject: [PATCH] fix #18, ReadLineSecure causes ArgumentOutOfRangeException in setup --- Utils.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Utils.cs b/Utils.cs index a512dd3..7c636e8 100644 --- a/Utils.cs +++ b/Utils.cs @@ -12,9 +12,16 @@ namespace SteamGuard do { key = Console.ReadKey(true); - if ((int)key.Key >= 65 && (int)key.Key <= 90) + if (((int)key.Key) >= 65 && ((int)key.Key <= 90)) { - text.Insert(cursorIndex, key.KeyChar.ToString()); + if (cursorIndex == 0 || cursorIndex == text.Length - 1) + { + text += key.KeyChar.ToString(); + } + else + { + text.Insert(cursorIndex, key.KeyChar.ToString()); + } cursorIndex++; } else if (key.Key == ConsoleKey.Backspace && cursorIndex > 0) @@ -35,10 +42,17 @@ namespace SteamGuard { 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; } + + // 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); return text; }