Skip to content

Commit

Permalink
🌟 EmbedText is now passive | EmbedText has no more button | PasswordM…
Browse files Browse the repository at this point in the history
…anager and Presentation need update
  • Loading branch information
MorganKryze committed Mar 26, 2024
1 parent 467bcd1 commit 6f6b98d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 267 deletions.
4 changes: 2 additions & 2 deletions examples/PasswordManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static void Authenticate()
}
else
{
EmbedText error = new(["Incorrect password. Please try again."], "Retry");
EmbedText error = new(["Incorrect password. Please try again."]);
Window.AddElement(error);

Window.ActivateElement(error);
Expand Down Expand Up @@ -253,7 +253,7 @@ static bool AddPassword(List<string>? row = null)
|| responseNotes?.Status == Status.Escaped
)
{
EmbedText error = new(["Password not saved."], "Home");
EmbedText error = new(["Password not saved."]);
Window.AddElement(error);
Window.ActivateElement(error);

Expand Down
20 changes: 11 additions & 9 deletions examples/Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ private static void Main()
"C# is a general-purpose, multi-paradigm programming language encompassing strong typing,",
"lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based),",
"and component-oriented programming disciplines.",
""
},
"Press [Enter] to continue..."
"Press [Enter] to continue..."
}
);
Window.AddElement(text);

Expand All @@ -102,8 +101,11 @@ private static void Main()

// We create an EmbedText to display the response
EmbedText embedResponsePrompt = new EmbedText(
new List<string>() { "You just wrote " + responsePrompt!.Value + "!" },
$"Next â–¶",
new List<string>()
{
"You just wrote " + responsePrompt!.Value + "!",
"Next â–¶"
},
TextAlignment.Center
);
Window.AddElement(embedResponsePrompt);
Expand All @@ -128,9 +130,9 @@ private static void Main()
EmbedText embedResponsePasswordPrompt = new EmbedText(
new List<string>()
{
"You just wrote " + responsePasswordPrompt!.Value + "!"
"You just wrote " + responsePasswordPrompt!.Value + "!",
$"Next â–¶"
},
$"Next â–¶",
TextAlignment.Center
);
Window.AddElement(embedResponsePasswordPrompt);
Expand Down Expand Up @@ -484,9 +486,9 @@ private static void Main()
EmbedText exitText = new EmbedText(
new List<string>()
{
"You have selected to quit the app. Press [Enter] to continue..."
"You have selected to quit the app. Press [Enter] to continue...",
$"Next â–¶",
},
$"Next â–¶",
TextAlignment.Left
);
Window.AddElement(exitText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ namespace ConsoleAppVisuals.InteractiveElements;
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public class EmbedText : InteractiveElement<string>
public class EmbedText : PassiveElement
{
#region Fields
private List<string> _lines;
private string? _button;
private TextAlignment _align;
private Placement _placement;
private readonly Borders _borders;
Expand Down Expand Up @@ -52,11 +51,6 @@ public class EmbedText : InteractiveElement<string>
/// </summary>
public List<string> Lines => _lines;

/// <summary>
/// The text of the button.
/// </summary>
public string? ButtonText => _button;

/// <summary>
/// The borders of the Embed text.
/// </summary>
Expand All @@ -79,7 +73,6 @@ public class EmbedText : InteractiveElement<string>
/// A <see cref="EmbedText"/> is an interactive element that displays text in a box with an optional button.
/// </summary>
/// <param name="text">The text to display.</param>
/// <param name="button">The text of the button. Null to not display a button.</param>
/// <param name="align">The alignment of the Embed text.</param>
/// <param name="placement">The placement of the Embed text element.</param>
/// <param name="bordersType">The type of border to display.</param>
Expand All @@ -92,54 +85,22 @@ public class EmbedText : InteractiveElement<string>
/// </remarks>
public EmbedText(
List<string> text,
string? button = null,
TextAlignment align = TextAlignment.Left,
Placement placement = Placement.TopCenter,
BordersType bordersType = BordersType.SingleStraight
)
{
_lines = text;
_button = button;
_align = align;
_placement = placement;
_borders = new Borders(bordersType);
if (CheckIntegrity())
BuildText();
if (IsLinesNotEmpty())
Build();
}
#endregion

#region Methods
private bool CheckIntegrity()
{
if (_lines.Count == 0)
{
return false;
}
if (_button is not null)
{
if (_lines.Max((string s) => s.Length) < _button.Length)
{
return false;
}
}
return true;
}

/// <summary>
/// This method updates the text of the button.
/// </summary>
/// <param name="newButton">The new text of the button.</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 UpdateButtonText(string? newButton)
{
_button = newButton;
}
private bool IsLinesNotEmpty() => _lines.Count > 0;

/// <summary>
/// This method updates the text of the Embed text.
Expand Down Expand Up @@ -285,7 +246,7 @@ public void RemoveLine(int index)
[Visual]
protected override void RenderElementActions()
{
BuildText();
Build();
Core.WriteMultiplePositionedLines(
false,
TextAlignment,
Expand All @@ -294,10 +255,9 @@ protected override void RenderElementActions()
Line,
_textToDisplay!.ToArray()
);
Window.Freeze();
}

private void BuildText()
private void Build()
{
var maxLength = _lines.Max((string s) => s.Length);
_textToDisplay = new List<string>();
Expand Down Expand Up @@ -325,22 +285,6 @@ private void BuildText()
0,
Borders.TopLeft + new string(Borders.Horizontal, maxLength + 2) + Borders.TopRight
);
if (_button is not null)
{
_textToDisplay.Add(
$"{Borders.Vertical} " + new string(' ', maxLength) + $" {Borders.Vertical}"
);
_textToDisplay.Add(
$"{Borders.Vertical} "
+ "".PadRight(maxLength - _button.Length - 2)
+ Core.NEGATIVE_ANCHOR
+ " "
+ _button
+ " "
+ Core.NEGATIVE_ANCHOR
+ $" {Borders.Vertical}"
);
}
_textToDisplay.Add(
Borders.BottomLeft + new string(Borders.Horizontal, maxLength + 2) + Borders.BottomRight
);
Expand Down
Loading

0 comments on commit 6f6b98d

Please sign in to comment.