Skip to content

Commit

Permalink
🌟 selector char managed locally in the element and not in the core class
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganKryze committed Mar 13, 2024
1 parent c95185a commit 53fc25e
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 50 deletions.
4 changes: 2 additions & 2 deletions example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static void Main()
{
"You just wrote " + responsePrompt?.Value + "!"
},
$"Next {Core.GetSelector.Item1}",
$"Next ",
TextAlignment.Center
);
Window.AddElement(embedResponsePrompt);
Expand Down Expand Up @@ -540,7 +540,7 @@ private static void Main()
{
"You have selected to quit the app. Press [Enter] to continue..."
},
$"Next {Core.GetSelector.Item1}",
$"Next ",
TextAlignment.Left
);
Window.AddElement(exitText);
Expand Down
24 changes: 0 additions & 24 deletions src/ConsoleAppVisuals/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public static class Core
#endregion

#region Fields
private static (char, char) s_selector = ('▶', '◀');

[Visual]
private static int s_previousWindowWidth = Console.WindowWidth;

Expand All @@ -44,11 +42,6 @@ private static (ConsoleColor, ConsoleColor) s_terminalColorPanel = (
#endregion

#region Properties
/// <summary>
/// This property is used to get the selector of the console menus.
/// </summary>
public static (char, char) GetSelector => s_selector;

/// <summary>
/// This property is used to get the colors of the console.
/// </summary>
Expand Down Expand Up @@ -86,23 +79,6 @@ public static bool IsScreenUpdated()
return isUpdated;
}

/// <summary>
/// This method is used to set the selector of the console menus.
/// </summary>
/// <param name="onward">The new selector facing forward.</param>
/// <param name="backward">The new selector facing backward.</param>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public static void SetSelector(char onward, char backward)
{
s_selector = (onward, backward);
}

/// <summary>
/// This method changes the font color of the console.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ public class FloatSelector : InteractiveElement<float>
private float _step;
private bool _roundedCorners;
private Placement _placement;
private (char, char) _selector = (DEFAULT_SELECTOR_LEFT, DEFAULT_SELECTOR_RIGHT);
#endregion

#region Constants
/// <summary>
/// The default left selector of the number selector.
/// </summary>
/// <remarks>
/// Can be updated with the <see cref="UpdateLeftSelector(char)"/> method.
/// </remarks>
public const char DEFAULT_SELECTOR_LEFT = '>';

/// <summary>
/// The default right selector of the number selector.
/// </summary>
/// <remarks>
/// Can be updated with the <see cref="UpdateRightSelector(char)"/> method.
/// </remarks>
public const char DEFAULT_SELECTOR_RIGHT = '<';
#endregion

#region Properties
Expand All @@ -43,7 +62,7 @@ public class FloatSelector : InteractiveElement<float>
public override int Width =>
Math.Max(
_question.Length,
$" {Core.GetSelector.Item1} {BuildNumber((float)Math.Round(_maximumValue, 1))} {Core.GetSelector.Item2} ".Length
$" {LeftSelector} {BuildNumber((float)Math.Round(_maximumValue, 1))} {RightSelector} ".Length
);

/// <summary>
Expand Down Expand Up @@ -75,6 +94,16 @@ public class FloatSelector : InteractiveElement<float>
/// Whether the corners of the selector are rounded.
/// </summary>
public bool RoundedCorners => _roundedCorners;

/// <summary>
/// The left selector of the selector.
/// </summary>
public char LeftSelector => _selector.Item1;

/// <summary>
/// The right selector of the selector.
/// </summary>
public char RightSelector => _selector.Item2;
#endregion

#region Constructor
Expand Down Expand Up @@ -266,6 +295,38 @@ public void SetRoundedCorners(bool roundedCorners = true)
_roundedCorners = roundedCorners;
}

/// <summary>
/// This method is used to update the selector of the selector.
/// </summary>
/// <param name="leftSelector">The new left selector of the selector.</param>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public void UpdateLeftSelector(char leftSelector)
{
_selector.Item1 = leftSelector;
}

/// <summary>
/// This method is used to update the selector of the selector.
/// </summary>
/// <param name="rightSelector">The new right selector of the selector.</param>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public void UpdateRightSelector(char rightSelector)
{
_selector.Item2 = rightSelector;
}

/// <summary>
/// This method is used to draw the selector on the console.
/// </summary>
Expand Down Expand Up @@ -328,7 +389,7 @@ void DisplayChoices(int lineSelector, float currentNumber)
lineSelector - 1
);
Core.WritePositionedString(
$" {Core.GetSelector.Item1} {BuildNumber((float)Math.Round(currentNumber, 1))} {Core.GetSelector.Item2} ",
$" {LeftSelector} {BuildNumber((float)Math.Round(currentNumber, 1))} {RightSelector} ",
TextAlignment.Center,
true,
lineSelector
Expand Down
65 changes: 63 additions & 2 deletions src/ConsoleAppVisuals/elements/interactive_elements/IntSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ public class IntSelector : InteractiveElement<int>
private int _step;
private bool _roundedCorners;
private Placement _placement;
private (char, char) _selector = (DEFAULT_SELECTOR_LEFT, DEFAULT_SELECTOR_RIGHT);
#endregion

#region Constants
/// <summary>
/// The default left selector of the number selector.
/// </summary>
/// <remarks>
/// Can be updated with the <see cref="UpdateLeftSelector(char)"/> method.
/// </remarks>
public const char DEFAULT_SELECTOR_LEFT = '>';

/// <summary>
/// The default right selector of the number selector.
/// </summary>
/// <remarks>
/// Can be updated with the <see cref="UpdateRightSelector(char)"/> method.
/// </remarks>
public const char DEFAULT_SELECTOR_RIGHT = '<';
#endregion

#region Properties
Expand All @@ -43,7 +62,7 @@ public class IntSelector : InteractiveElement<int>
public override int Width =>
Math.Max(
_question.Length,
$" {Core.GetSelector.Item1} {BuildNumber(_maximumValue)} {Core.GetSelector.Item2} ".Length
$" {LeftSelector} {BuildNumber(_maximumValue)} {RightSelector} ".Length
);

/// <summary>
Expand Down Expand Up @@ -75,6 +94,16 @@ public class IntSelector : InteractiveElement<int>
/// Whether the corners of the selector are rounded.
/// </summary>
public bool RoundedCorners => _roundedCorners;

/// <summary>
/// The left selector of the selector.
/// </summary>
public char LeftSelector => _selector.Item1;

/// <summary>
/// The right selector of the selector.
/// </summary>
public char RightSelector => _selector.Item2;
#endregion

#region Constructor
Expand Down Expand Up @@ -265,6 +294,38 @@ public void SetRoundedCorners(bool roundedCorners = true)
_roundedCorners = roundedCorners;
}

/// <summary>
/// This method is used to update the selector of the selector.
/// </summary>
/// <param name="leftSelector">The new left selector of the selector.</param>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public void UpdateLeftSelector(char leftSelector)
{
_selector.Item1 = leftSelector;
}

/// <summary>
/// This method is used to update the selector of the selector.
/// </summary>
/// <param name="rightSelector">The new right selector of the selector.</param>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public void UpdateRightSelector(char rightSelector)
{
_selector.Item2 = rightSelector;
}

/// <summary>
/// This method is used to draw the selector on the console.
/// </summary>
Expand Down Expand Up @@ -323,7 +384,7 @@ void DisplayChoices(int lineSelector, int currentNumber)
lineSelector - 1
);
Core.WritePositionedString(
$" {Core.GetSelector.Item1} {BuildNumber(currentNumber)} {Core.GetSelector.Item2} ",
$" {LeftSelector} {BuildNumber(currentNumber)} {RightSelector} ",
TextAlignment.Center,
true,
lineSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public class ScrollingMenu : InteractiveElement<int>
private string[] _choices;
private int _defaultIndex;
private Placement _placement;
private char _selector = DEFAULT_CURSOR;
#endregion

#region Constants
/// <summary>
/// The default cursor of the menu.
/// </summary>
/// <remarks>
/// Can be updated with the <see cref="UpdateSelector(char)"/> method.
/// </remarks>
public const char DEFAULT_CURSOR = '>';
#endregion

#region Properties
Expand Down Expand Up @@ -55,6 +66,11 @@ public class ScrollingMenu : InteractiveElement<int>
/// </summary>
public int DefaultIndex => _defaultIndex;

/// <summary>
/// The selector char of the menu.
/// </summary>
public char Selector => _selector;

#endregion

#region Constructor
Expand Down Expand Up @@ -151,6 +167,22 @@ public void UpdatePlacement(Placement placement)
_placement = placement;
}

/// <summary>
/// This method is used to update the selector of the menu.
/// </summary>
/// <param name="selector">The new selector of the menu.</param>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public void UpdateSelector(char selector)
{
_selector = selector;
}

/// <summary>
/// This method is used to draw the menu on the console.
/// </summary>
Expand Down Expand Up @@ -213,7 +245,7 @@ private static void EqualizeChoicesLength(string[] choices)
}

[Visual]
private static void DisplayChoices(
private void DisplayChoices(
int defaultIndex,
Placement placement,
string[] choices,
Expand All @@ -224,10 +256,7 @@ private static void DisplayChoices(
string[] array = new string[choices.Length];
for (int i = 0; i < choices.Length; i++)
{
array[i] =
(i == defaultIndex)
? $" {Core.GetSelector.Item1} {choices[i]} "
: $" {choices[i]} ";
array[i] = (i == defaultIndex) ? $" {Selector} {choices[i]} " : $" {choices[i]} ";
Core.WritePositionedString(
array[i],
placement.ToTextAlignment(),
Expand Down
15 changes: 0 additions & 15 deletions testing/Core.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ namespace testing;
[TestClass]
public class UnitTestCore
{
#region SetSelector
[TestMethod]
public void SetSelector_NewChar()
{
// Arrange
Core.SetSelector('>', '<');

// Act
var selector = Core.GetSelector;

// Assert
Assert.AreEqual('>', selector.Item1);
Assert.AreEqual('<', selector.Item2);
}
#endregion

#region SetForegroundColor
[TestMethod]
Expand Down
Loading

0 comments on commit 53fc25e

Please sign in to comment.