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
15 changes: 15 additions & 0 deletions src/Inquirer/Console/ConsoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ public int[] Render(string[] content, string[] bottomContent)
return new [] { content.Length, bottomContent.Length };
}

public int[] Render(ConsoleMessage[] content, ConsoleMessage[] bottomContent)
{
content.ToList().ForEach(item =>
{
if (item.ConsoleColor.HasValue)
console.ForegroundColor = item.ConsoleColor.Value;

console.WriteLine(item.Message);
});
Newline();
console.ResetColor();

@afucher afucher Oct 14, 2020

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this console.ResetColor() should be inside the loop.
If we have 3 messages:

  1. Console.Color = Cyan
  2. Console.Color = (Not set)
  3. Console.Color = Red

The second message will print Cyan, and I was expecting it to be the default Console color.

Could you please add some test to cover that? If need any help let me know

bottomContent.ToList().ForEach(item => console.WriteLine(item.Message));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the bottomContent is also of the type ConsoleMessage, we should check the Color the same way that you have done for the content.

console.CursorTop = console.CursorTop - (bottomContent.Length + 1);
return new[] { content.Length, bottomContent.Length };
}

public void Clean(int initialPos, int endPos)
{
Expand Down
30 changes: 30 additions & 0 deletions src/Inquirer/Console/ConsoleMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace InquirerCore.Console
{
public class ConsoleMessage
{
public ConsoleMessage(string message)
{
Message = message;
}

public ConsoleMessage(string message, ConsoleColor consoleColor)
{
Message = message;
ConsoleColor = consoleColor;
}

public string Message { get; private set; }

public ConsoleColor? ConsoleColor { get; private set; }

public void SetMessage(string message)
=> Message = message;

public void SetConsoleColor(ConsoleColor consoleColor)
=> ConsoleColor = consoleColor;
}
}
2 changes: 2 additions & 0 deletions src/Inquirer/Console/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public interface IConsole
int CursorTop { get; set; }
int WindowWidth { get; set; }
bool KeyAvailable { get; }
ConsoleColor ForegroundColor { get; set; }
void ResetColor();
}
}
1 change: 1 addition & 0 deletions src/Inquirer/Prompts/IScreenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface IScreenManager
{
int[,] RenderMultipleMessages(string[] messages);
int[] Render(string[] content, string[] bottomContent);
int[] Render(ConsoleMessage[] content, ConsoleMessage[] bottomContent);
void Clean(int initialPos, int endPos);
string ReadLine();
IInputObservable GetInputObservable();
Expand Down
22 changes: 20 additions & 2 deletions src/Inquirer/Prompts/ListInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using InquirerCore.Console;
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Text;
Expand Down Expand Up @@ -57,7 +58,24 @@ public override string[] GetQuestion()

public override int[] Render()
{
return consoleRender.Render(GetQuestion(), new string[] {});
ConsoleMessage[] messages = ProcessInputMessages(GetQuestion());
return consoleRender.Render(messages, new ConsoleMessage[] { });
}

public ConsoleMessage[] ProcessInputMessages(string[] questions)
{
var messages = new ConsoleMessage[questions.Length];

for (int i = 0; i < questions.Length; i++)
{
messages[i] = new ConsoleMessage(questions[i]);
if ((selectedOption + 1) == i)
messages[i].SetConsoleColor(ConsoleColor.Cyan);
else
messages[i].SetConsoleColor(ConsoleColor.Gray);
}

return messages;
}
}
}
33 changes: 32 additions & 1 deletion test/InquirerUnitTest/InputListUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,39 @@ public void ShouldCallConsoleWriteLine()
var input = new ListInput(name, message, new string[] { "option1", "option2" }, consoleRender);
input.Render();

consoleRender.Received().Render(Arg.Any<string[]>(), Arg.Any<string[]>());
consoleRender.Received().Render(Arg.Any<ConsoleMessage[]>(), Arg.Any<ConsoleMessage[]>());
}

[Fact]
public void FirstQuestionShoulBeADifferentColor()
{
var message = "Which option?";
var name = "option";
var options = new string[] { "option1", "option2" };
var consoleRender = Substitute.For<IScreenManager>();
var input = new ListInput(name, message, options, consoleRender);

var questions = input.GetQuestion();
var inputMessages = input.ProcessInputMessages(questions);

inputMessages[1].Message.Should().Be("> option1");
inputMessages[1].ConsoleColor.Should().Be(ConsoleColor.Cyan);
}

[Fact]
public void SecondQuestionNotShoulBeADifferentColor()
{
var message = "Which option?";
var name = "Name";
var options = new string[] { "option1", "option2" };
var consoleRender = Substitute.For<IScreenManager>();
var input = new ListInput(name, message, options, consoleRender);

var questions = input.GetQuestion();
var inputMessages = input.ProcessInputMessages(questions);

inputMessages[2].Message.Should().Be("option2");
inputMessages[2].ConsoleColor.Should().NotBe(ConsoleColor.Cyan);
}
}
}