Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Fallout.Build/Utilities/ConsoleUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ public static string PromptForInput(string question, string defaultValue)
}

key = Console.ReadKey(intercept: true);
if (ConsoleKey.A <= key.Key && key.Key <= ConsoleKey.Z
|| ConsoleKey.D0 <= key.Key && key.Key <= ConsoleKey.D9
|| new[] { '.', '/', '\\', '_', '-' }.Any(x => x == key.KeyChar))
if (key.IsValidInputKey())
input.Append(key.KeyChar);
else if (key.Key == ConsoleKey.Backspace && input.Length > 0)
input.Remove(input.Length - 1, length: 1);
else if (key.Key == InterruptKey)
s_interrupted = true;
} while (!(key.Key == ConfirmationKey || key.Key == InterruptKey));
} while (key.Key is not (ConfirmationKey or InterruptKey));

var result = input.Length > 0 ? input.ToString() : defaultValue;
Console.CursorLeft = 0;
Expand Down
17 changes: 17 additions & 0 deletions src/Fallout.Build/Utilities/ConsoleUtilityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Linq;

namespace Fallout.Common.Utilities;

public static class ConsoleUtilityExtensions
{
private static readonly char[] AllowedSpecialCharacters = ['.', '/', '\\', '_', '-'];

public static bool IsValidInputKey(this ConsoleKeyInfo key)
{
return key.Key is >= ConsoleKey.A and <= ConsoleKey.Z
|| key.Key is >= ConsoleKey.D0 and <= ConsoleKey.D9
|| AllowedSpecialCharacters.Any(x => x == key.KeyChar)
|| char.IsLetterOrDigit(key.KeyChar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Fallout.Common.Utilities;
using Xunit;
using System;
using FluentAssertions;

namespace Fallout.Build.Tests.Utilities;

public class ConsoleUtilityExtensionsSpecs
{
[Theory]
[InlineData(ConsoleKey.A, 'a', true)]
[InlineData(ConsoleKey.Z, 'z', true)]
[InlineData(ConsoleKey.D0, '0', true)]
[InlineData(ConsoleKey.D9, '9', true)]
[InlineData(ConsoleKey.Enter, '\r', false)]
[InlineData(ConsoleKey.Backspace, '\b', false)]
[InlineData(ConsoleKey.F8, '\0', false)]
[InlineData(ConsoleKey.Spacebar, ' ', false)]
[InlineData(ConsoleKey.OemPeriod, '.', true)]
[InlineData(ConsoleKey.Divide, '/', true)]
[InlineData(ConsoleKey.OemMinus, '-', true)]
[InlineData(ConsoleKey.NoName, 'ä', true)]
[InlineData(ConsoleKey.NoName, 'ö', true)]
[InlineData(ConsoleKey.NoName, '中', true)]
[InlineData(ConsoleKey.NoName, 'д', true)]
[InlineData(ConsoleKey.NoName, 'α', true)]
public void Valid_characters_are_accepted_as_input(ConsoleKey key, char keyChar, bool expected)
{
var keyInfo = new ConsoleKeyInfo(keyChar, key, false, false, false);
var result = keyInfo.IsValidInputKey();
result.Should().Be(expected);
}
}
Loading