This commit is contained in:
JustArchi
2020-11-11 21:06:17 +01:00
parent 47b42d6fc0
commit 102ed7282a

View File

@@ -88,8 +88,6 @@ namespace ArchiSteamFarm.NLog {
OnUserInputStart(); OnUserInputStart();
try { try {
Console.Beep();
switch (userInputType) { switch (userInputType) {
case ASF.EUserInputType.Login: case ASF.EUserInputType.Login:
Console.Write(Bot.FormatBotResponse(Strings.UserInputSteamLogin, botName)); Console.Write(Bot.FormatBotResponse(Strings.UserInputSteamLogin, botName));
@@ -227,40 +225,72 @@ namespace ArchiSteamFarm.NLog {
ASF.ArchiLogger.LogGenericInfo(Strings.InteractiveConsoleEnabled); ASF.ArchiLogger.LogGenericInfo(Strings.InteractiveConsoleEnabled);
} }
private static string? ConsoleReadLine() { private static async Task BeepUntilCanceled(CancellationToken cancellationToken, byte secondsDelay = 30) {
Console.Beep(); if (secondsDelay == 0) {
throw new ArgumentOutOfRangeException(nameof(secondsDelay));
}
return Console.ReadLine(); while (true) {
try {
await Task.Delay(secondsDelay * 1000, cancellationToken).ConfigureAwait(false);
} catch (TaskCanceledException) {
return;
}
Console.Beep();
}
}
private static string? ConsoleReadLine() {
using CancellationTokenSource cts = new CancellationTokenSource();
try {
CancellationToken token = cts.Token;
Utilities.InBackground(() => BeepUntilCanceled(token));
return Console.ReadLine();
} finally {
cts.Cancel();
}
} }
private static string ConsoleReadLineMasked(char mask = '*') { private static string ConsoleReadLineMasked(char mask = '*') {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
Console.Beep(); using CancellationTokenSource cts = new CancellationTokenSource();
ConsoleKeyInfo keyInfo; try {
CancellationToken token = cts.Token;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter) { Utilities.InBackground(() => BeepUntilCanceled(token));
if (!char.IsControl(keyInfo.KeyChar)) {
result.Append(keyInfo.KeyChar);
Console.Write(mask);
} else if ((keyInfo.Key == ConsoleKey.Backspace) && (result.Length > 0)) {
result.Length--;
if (Console.CursorLeft == 0) { ConsoleKeyInfo keyInfo;
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop - 1);
Console.Write(' '); while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter) {
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop - 1); if (!char.IsControl(keyInfo.KeyChar)) {
} else { result.Append(keyInfo.KeyChar);
// There are two \b characters here Console.Write(mask);
Console.Write(@" "); } else if ((keyInfo.Key == ConsoleKey.Backspace) && (result.Length > 0)) {
result.Length--;
if (Console.CursorLeft == 0) {
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop - 1);
Console.Write(' ');
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop - 1);
} else {
// There are two \b characters here
Console.Write(@" ");
}
} }
} }
Console.WriteLine();
return result.ToString();
} finally {
cts.Cancel();
} }
Console.WriteLine();
return result.ToString();
} }
private static async Task HandleConsoleInteractively() { private static async Task HandleConsoleInteractively() {