Skip to content

Commit

Permalink
Further enhancements for #3075 and #3051
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Nov 19, 2023
1 parent 2e9791f commit 120f7e3
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions ArchiSteamFarm/NLog/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ internal static class Logging {
private const byte ConsoleResponsivenessDelay = 250; // In milliseconds
private const string GeneralLayout = $@"${{date:format=yyyy-MM-dd HH\:mm\:ss}}|${{processname}}-${{processid}}|${{level:uppercase=true}}|{LayoutMessage}";
private const string LayoutMessage = @"${logger}|${message}${onexception:inner= ${exception:format=toString,Data}}";
private const byte MaxReadFailures = 100; // How many unknown characters we allow before closing STDIN in order to avoid infinite loop

internal static bool LogFileExists => File.Exists(SharedInfo.LogFile);

Expand Down Expand Up @@ -309,29 +308,21 @@ private static string ConsoleReadLineMasked(char mask = '*') {

StringBuilder result = new();

byte readFailures = 0;

while (true) {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

switch (keyInfo.Key) {
case 0 when ++readFailures < MaxReadFailures:
// Likely linux terminal closing STDIN, but we'll check for more
continue;
case 0:
// Linux terminal closing STDIN, we're done here
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(readFailures)));
if (keyInfo.KeyChar == '\u0004') {
// Linux terminal closing STDIN, we're done here
return result.ToString();
}

return result.ToString();
case ConsoleKey.Enter:
// User finishing input, as expected
Console.WriteLine();
if (keyInfo.Key == ConsoleKey.Enter) {
// User finishing input, as expected
Console.WriteLine();

return result.ToString();
return result.ToString();
}

readFailures = 0;

// User continues input
if (!char.IsControl(keyInfo.KeyChar)) {
result.Append(keyInfo.KeyChar);
Expand All @@ -355,8 +346,6 @@ private static string ConsoleReadLineMasked(char mask = '*') {

private static async Task HandleConsoleInteractively() {
try {
byte readFailures = 0;

while (!Program.ShutdownSequenceInitialized) {
if (IsWaitingForUserInput || !Console.KeyAvailable) {
await Task.Delay(ConsoleResponsivenessDelay).ConfigureAwait(false);
Expand All @@ -369,26 +358,15 @@ private static async Task HandleConsoleInteractively() {
try {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

switch (keyInfo.Key) {
case 0 when ++readFailures < MaxReadFailures:
// Likely linux terminal closing STDIN, but we'll check for more
continue;
case 0:
// Linux terminal closing STDIN, we're done here
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(readFailures)));

return;
case ConsoleKey.C:
// User hitting 'c', as expected
break;
default:
// Any other input, ignored
readFailures = 0;

continue;
if (keyInfo.KeyChar == '\u0004') {
// Linux terminal closing STDIN, we're done here
return;
}

readFailures = 0;
if (keyInfo.Key != ConsoleKey.C) {
// Console input other than 'c', ignored
continue;
}

OnUserInputStart();

Expand Down

0 comments on commit 120f7e3

Please sign in to comment.