From 33e807465f9a46e44bcf65bb044d645726824096 Mon Sep 17 00:00:00 2001 From: lastunicorn Date: Sat, 4 Jan 2020 23:34:05 +0200 Subject: [PATCH] added foreground and background color for the question text in the YesNoQuestion control. --- .../ConsoleTools/InlineControl.cs | 30 ++++++++++++++ .../InputControls/YesNoQuestion.cs | 40 +++++++++++++++---- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/sources/ConsoleTools/ConsoleTools/InlineControl.cs b/sources/ConsoleTools/ConsoleTools/InlineControl.cs index 5882b7ee..3bacb1ba 100644 --- a/sources/ConsoleTools/ConsoleTools/InlineControl.cs +++ b/sources/ConsoleTools/ConsoleTools/InlineControl.cs @@ -41,6 +41,18 @@ public abstract class InlineControl : Control /// public int MarginRight { get; set; } + /// + /// Gets or sets the number of spaces to be written before the content (to the left). + /// Default value: 0 + /// + public int PaddingLeft { get; set; } + + /// + /// Gets or sets the number of spaces to be written after the content (to the right). + /// Default value: 0 + /// + public int PaddingRight { get; set; } + /// /// Gets or sets the foreground color used to write the text. /// Default value: null @@ -61,8 +73,14 @@ protected override void DoDisplay() if (MarginLeft > 0) DisplayLeftMargin(); + if (PaddingLeft > 0) + DisplayPaddingLeft(); + DoDisplayContent(); + if (PaddingRight > 0) + DisplayPaddingRight(); + if (MarginRight > 0) DisplayRightMargin(); } @@ -79,6 +97,18 @@ private void DisplayRightMargin() Console.Write(space); } + private void DisplayPaddingLeft() + { + string paddingLeft = new string(' ', PaddingLeft); + WriteText(paddingLeft); + } + + private void DisplayPaddingRight() + { + string paddingRight = new string(' ', PaddingRight); + WriteText(paddingRight); + } + /// /// When implemented in a derived class, it displays the content of the control. /// This method is not responsible to display the margins. diff --git a/sources/ConsoleTools/ConsoleTools/InputControls/YesNoQuestion.cs b/sources/ConsoleTools/ConsoleTools/InputControls/YesNoQuestion.cs index c5ebccc6..43e6d444 100644 --- a/sources/ConsoleTools/ConsoleTools/InputControls/YesNoQuestion.cs +++ b/sources/ConsoleTools/ConsoleTools/InputControls/YesNoQuestion.cs @@ -29,16 +29,21 @@ namespace DustInTheWind.ConsoleTools.InputControls /// public class YesNoQuestion : BlockControl { - private readonly Label labelControl = new Label - { - ForegroundColor = CustomConsole.EmphasiesColor - }; - /// /// Gets or sets trhe question that is displayed to the user. /// public string QuestionText { get; set; } + /// + /// Gets or sets the foreground color used for displaying the question. + /// + public ConsoleColor? QuestionForegroundColor { get; set; } = CustomConsole.EmphasiesColor; + + /// + /// Gets or sets the background color used for displaying the question. + /// + public ConsoleColor? QuestionBackgroundColor { get; set; } + /// /// Gets or sets the number of spaces to be displayed after the question and the before the user types the answer. /// Default value: 1 @@ -161,14 +166,27 @@ protected override void DoDisplayContent(ControlDisplay display) private void DisplayQuestion() { - labelControl.Text = QuestionText; + InlineTextBlock labelControl = new InlineTextBlock + { + PaddingRight = 1, + ForegroundColor = QuestionForegroundColor ?? ForegroundColor, + BackgroundColor = QuestionBackgroundColor ?? BackgroundColor, + Text = QuestionText + }; labelControl.Display(); } private void DisplaySpaceAfterQuestion() { string space = new string(' ', SpaceAfterQuestion); - Console.Write(space); + + InlineTextBlock inlineTextBlock = new InlineTextBlock + { + Text = space, + ForegroundColor = ForegroundColor, + BackgroundColor = BackgroundColor + }; + inlineTextBlock.Display(); } /// @@ -204,7 +222,13 @@ protected virtual void DisplayPossibleAnswersList() sb.Append("]"); - Console.Write(sb); + InlineTextBlock inlineTextBlock = new InlineTextBlock + { + Text = sb.ToString(), + ForegroundColor = ForegroundColor, + BackgroundColor = BackgroundColor + }; + inlineTextBlock.Display(); } private YesNoAnswer ReadAnswerInternal()