Skip to content
Open
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
23 changes: 23 additions & 0 deletions samples/Samples/Basic/Sample3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using InquirerCore;
using InquirerCore.Prompts;

namespace Samples.Basic
{
public static class Sample3
{
public static void Run()
{
var emailInput = new Input("name", "What is your email?");

var passwordInput = new PasswordInput("password", "What is the password?");

var inquirer = new Inquirer(emailInput, passwordInput);

inquirer.Ask();

System.Console.WriteLine($@"Your email is {emailInput.Answer()}!");
System.Console.WriteLine($@"Secret password: {passwordInput.Answer()}!");
System.Console.ReadKey();
}
}
}
2 changes: 1 addition & 1 deletion samples/Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Program
{
static void Main(string[] args)
{
Basic.Sample1.Run();
Basic.Sample3.Run();
}
}
}
22 changes: 20 additions & 2 deletions src/Inquirer/Console/ConsoleObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IObservable<ConsoleKeyInfo> GetEnterObservable()
public IObservable<string> GetLineObservable()
{
return input.TakeUntil(GetEnterObservable())
.Where(IsNormalKey)
.Where(IsNormalKeyOrAtCharacter)
.Aggregate("", BuildLine);
}

Expand Down Expand Up @@ -79,6 +79,19 @@ public bool IsNormalKey(ConsoleKeyInfo key)
return !ConsoleUtils.isDigit(key.Key);
}

/// <summary>
/// Validate if the key entered is a normal key or the @ character
/// </summary>
/// <param name="key">The current key entered</param>
/// <returns>A boolean representing whether the key entered is a normal key or the @ character</returns>
public bool IsNormalKeyOrAtCharacter(ConsoleKeyInfo key)
{
var hasShift = key.Modifiers.HasFlag(ConsoleModifiers.Shift);
if (!hasShift) return true;

return !ConsoleUtils.isDigit(key.Key) || key.Key == ConsoleKey.D2;
}

private void ImplementKeysBehaviours(ConsoleKeyInfo cki)
{
var key = cki.Key;
Expand All @@ -89,7 +102,12 @@ private void ImplementKeysBehaviours(ConsoleKeyInfo cki)
break;
};
var hasShift = cki.Modifiers.HasFlag(ConsoleModifiers.Shift);
if (hasShift && ConsoleUtils.isDigit(key)) console.Write("\b");


//Update Oct 1, 2020
//If the digit is D2 (@ special character) then it will go back one character and the next will replace it
//Adding a condition to avoid this and include the @ special character
if (hasShift && ConsoleUtils.isDigit(key) && (key != ConsoleKey.D2)) console.Write("\b");
}

public void Intercept(bool intercept)
Expand Down
5 changes: 2 additions & 3 deletions src/Inquirer/Console/ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InquirerCore.Console
{
public static class ConsoleUtils
{
public static ConsoleKey[] DigitsKeys = new ConsoleKey[]{ ConsoleKey.D0,
//Made this private since it is only used in this class
private static ConsoleKey[] DigitsKeys = new ConsoleKey[]{ ConsoleKey.D0,
ConsoleKey.D1,
ConsoleKey.D2,
ConsoleKey.D3,
Expand Down
17 changes: 17 additions & 0 deletions test/InquirerUnitTest/ConsoleObservableUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,22 @@ public void LineObservableShouldRemoveLettersBackspaced()
scheduler.Start();
line.Should().Be(expectedLine);
}

[Fact]
public void LineObservableShouldIncludeAtSign()
{
var line = "";
var expectedLine = "@";
var scheduler = new TestScheduler();
var console = Substitute.For<IConsole>();
var consoleObservable = new ConsoleObservable(console, scheduler);
var keys = ckiFactory.GetMultipleLetters("@\n");

console.ReadKey(Arg.Any<bool>()).Returns(keys.First(), keys.Skip(1).ToArray());

consoleObservable.GetLineObservable().Subscribe(x => line = x);
scheduler.Start();
line.Should().Be(expectedLine);
}
}
}
4 changes: 2 additions & 2 deletions test/InquirerUnitTest/Helpers/ConsoleKeyInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InquirerUnitTest.Helpers
{
Expand All @@ -23,6 +21,8 @@ public ConsoleKeyInfo Get(char key)
return new ConsoleKeyInfo('\0', ConsoleKey.D6, true, false, false);
case 'ü':
return new ConsoleKeyInfo('ü', ConsoleKey.U, false, false, false);
case '@':
return new ConsoleKeyInfo('@', ConsoleKey.D2, true, false, false);
default:
throw new Exception("Not implemented");
}
Expand Down