Skip to content

Commit

Permalink
Fixed improvements: #72 and #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Cerqueira committed Oct 6, 2023
1 parent e454e56 commit 3de47f6
Show file tree
Hide file tree
Showing 17 changed files with 442 additions and 17 deletions.
4 changes: 3 additions & 1 deletion Samples/AlternateScreenSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void Main(string[] args)
.Run();


PromptPlus.RunOnBuffer(TargetBuffer.Secondary,
var hassecondscreen = PromptPlus.RunOnBuffer(TargetBuffer.Secondary,
(cts) =>
{
PromptPlus.WriteLine("This text run in secondary screen");
Expand All @@ -35,6 +35,8 @@ static void Main(string[] args)
},
ConsoleColor.White,
ConsoleColor.Red);
if (!hassecondscreen)
//faça algo

PromptPlus.WriteLines(2);
PromptPlus.KeyPress("End Sample!, Press any key", cfg =>
Expand Down
23 changes: 23 additions & 0 deletions Samples/ConsoleFeaturesSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// The maintenance and evolution is maintained by the PromptPlus project under MIT license
// ***************************************************************************************

using System.Globalization;
using PPlus;
using PPlus.Controls;

Expand All @@ -17,6 +18,27 @@ static void Main()
PromptPlus.Reset();
PromptPlus.Clear();

//global
PromptPlus.Config.EnabledAbortKey = false;
PromptPlus.StyleSchema.ApplyStyle(StyleControls.Prompt, new Style(Color.Red));
//by instance
PromptPlus.Input("").Config(cfg =>
{
cfg
.ApplyStyle(StyleControls.Answer, new Style(Color.Yellow))
.EnabledAbortKey(true);
});

PromptPlus.WriteLine("[RED ON WHITE]Hello[/] [YELLOW]Word[/]");

PromptPlus.WriteLine("[RGB(255,0,0) ON WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");

PromptPlus.AppendText("[RGB(255,0,0) ON WHITE]Test[/]")
.And(" COLOR ",Color.Yellow)
.And(" BACK COLOR ",new Style(Color.Red,Color.White))
.And("other text")
.WriteLine();

//standard
PromptPlus.DoubleDash($"PromptPlus IgnoreColorTokens = false - Default value and usage" );
PromptPlus.WriteLine("Valid[[]]formedColor_TokenAny[[RED ON WHITE]]Text[[/]]_[[YELLOW]]Othertext[[/]]");
Expand Down Expand Up @@ -55,6 +77,7 @@ static void Main()
PromptPlus.WriteLine("[#ff0000 ON WHITE]Test [YELLOW] COLOR [/] BACK COLOR [/] other text");
PromptPlus.WriteLine("[RED ON WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");

PromptPlus.WriteLine("Test", new Style(ConsoleColor.Red, ConsoleColor.White, Overflow.None));
PromptPlus.WriteLine("Test", new Style(Color.White, Color.Red, Overflow.None));
PromptPlus.WriteLine("Test", new Style(new Color(255, 255, 255), Color.Red, Overflow.None));
PromptPlus.WriteLine("Test", new Style(Color.FromConsoleColor(ConsoleColor.White), Color.Red, Overflow.None));
Expand Down
1 change: 1 addition & 0 deletions Samples/InputBasicSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
PromptPlus.DoubleDash("Control:Input - with default value when empty");
PromptPlus
.Input("Input sample7","Please press enter with no input")
.Default("initial value")
.DefaultIfEmpty("empty value")
.Run();

Expand Down
26 changes: 15 additions & 11 deletions Samples/InputWithSuggestionSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@

SuggestionOutput SuggestionInputSample(SuggestionInput arg)

Check warning on line 14 in Samples/InputWithSuggestionSamples/Program.cs

View workflow job for this annotation

GitHub Actions / build

The local function 'SuggestionInputSample' is declared but never used

Check warning on line 14 in Samples/InputWithSuggestionSamples/Program.cs

View workflow job for this annotation

GitHub Actions / build

The local function 'SuggestionInputSample' is declared but never used
{
var result = new SuggestionOutput();
if (arg.Text.StartsWith("s", StringComparison.CurrentCultureIgnoreCase))
{
result.Add("suggestion 1");
result.Add("suggestion 2");
result.Add("suggestion 3");
}
else
{
result.Add("other suggestion 1");
result.Add("other suggestion 2");
result.Add("other suggestion 3");
return SuggestionOutput
.Create()
.AddRange(
new string[] { "Opc1", "Opc2", "Opc3" });
}
var result = new SuggestionOutput();
result.Add("other suggestion 1");
result.Add("other suggestion 2");
result.Add("other suggestion 3");
return result;
}

PromptPlus.DoubleDash("Control:Input - with suggestions.");
var in1 = PromptPlus
.Input("Input sample", "input with suggestions.")
.SuggestionHandler(SuggestionInputSample)
.SuggestionHandler((input) =>
{
return SuggestionOutput
.Create()
.AddRange(
new string[] { "Opc1", "Opc2", "Opc3" });
})
.Run();

if (!in1.IsAborted)
Expand Down
9 changes: 9 additions & 0 deletions Src/Controls/Input/IControlInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public interface IControlInput : IPromptControls<string>

/// <summary>
/// Default value when stated.
/// <br>Default cannot be used with IsSecret</br>
/// </summary>
/// <param name="value">Value default</param>
/// <returns><see cref="IControlInput"/></returns>
IControlInput Default(string value);

/// <summary>
/// Overwrite default start value with last result saved on history.
/// <br>OverwriteDefaultFrom cannot be used with IsSecret</br>
/// </summary>
/// <param name="value">name of file to save history</param>
/// <param name="timeout">The timeout for valid items saved. Default value is 365 days</param>
Expand Down Expand Up @@ -62,6 +64,11 @@ public interface IControlInput : IPromptControls<string>

/// <summary>
/// The input is a secret. the input text is masked to '#' (default value)
/// <br>Input secret cannot be used with suggestionhandler</br>
/// <br>Input secret cannot be used with DefaultEmptyValue</br>
/// <br>Input secret cannot be used with DefaultValue</br>
/// <br>Input secret cannot be used with OverwriteDefaultFrom</br>
/// <br>Input secret cannot be used with HistoryEnabled</br>
/// </summary>
/// <param name="value">char secret</param>
/// <returns><see cref="IControlInput"/></returns>
Expand Down Expand Up @@ -105,6 +112,7 @@ public interface IControlInput : IPromptControls<string>

/// <summary>
/// Add Suggestion Handler feature
/// <br>SuggestionHandler cannot be used with IsSecret</br>
/// </summary>
/// <param name="value">function to apply suggestions. <see cref="SuggestionInput"/> and <seealso cref="SuggestionOutput"/></param>
/// <returns><see cref="IControlInput"/></returns>
Expand All @@ -121,6 +129,7 @@ public interface IControlInput : IPromptControls<string>

/// <summary>
/// Enabled saved history inputs.
/// <br>HistoryEnabled cannot be used with IsSecret</br>
/// <br>The history file is saved in <see cref="Environment.SpecialFolder.UserProfile"/> in the 'PromptPlus.History' folder.</br>
/// </summary>
/// <param name="value">name of file to saved history</param>
Expand Down
8 changes: 8 additions & 0 deletions Src/Controls/Input/InputControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public IControlInput FilterType(FilterMode value)

public IControlInput OverwriteDefaultFrom(string value, TimeSpan? timeout)
{
if (_options.IsSecret)
{
throw new PromptPlusException("OverwriteDefaultFrom cannot be used with input secret");
}
_options.OverwriteDefaultFrom = value;
if (timeout != null)
{
Expand Down Expand Up @@ -201,6 +205,10 @@ public IControlInput IsSecret(char? value = '#' )
{
throw new PromptPlusException("Input secret cannot have DefaultEmptyValue");
}
if ((_options.OverwriteDefaultFrom??string.Empty).Length > 0)
{
throw new PromptPlusException("Input secret cannot have OverwriteDefaultFrom");
}
if (_options.DefaultValue != null)
{
throw new PromptPlusException("Input secret cannot have DefaultValue");
Expand Down
17 changes: 15 additions & 2 deletions Src/Controls/SugestionOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public SuggestionOutput()
_items = new();
}

/// <summary>
/// Create a new instance of SuggestionOutput
/// </summary>
/// <returns><see cref="SuggestionOutput"/></returns>
public static SuggestionOutput Create()
{
return new SuggestionOutput();
}

internal SuggestionOutput(IList<string> items)
{
_items.AddRange(items);
Expand All @@ -33,20 +42,24 @@ internal SuggestionOutput(IList<string> items)
/// Add suggestion
/// </summary>
/// <param name="value">text suggestion</param>
public void Add(string value)
/// <returns><see cref="SuggestionOutput"/></returns>
public SuggestionOutput Add(string value)
{
_items.Add(value);
Suggestions = new ReadOnlyCollection<string>(_items);
return this;
}

/// <summary>
/// Add Enumerable suggestions
/// </summary>
/// <param name="items">Enumerable text suggestions</param>
public void AddRange(IEnumerable<string> items)
/// <returns><see cref="SuggestionOutput"/></returns>
public SuggestionOutput AddRange(IEnumerable<string> items)
{
_items.AddRange(items);
Suggestions = new ReadOnlyCollection<string>(_items);
return this;
}

internal ReadOnlyCollection<string> Suggestions { get; private set; }
Expand Down
70 changes: 70 additions & 0 deletions Src/Drivers/AppendTextControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ***************************************************************************************
// MIT LICENCE
// The maintenance and evolution is maintained by the PromptPlus project under MIT license
// ***************************************************************************************

using System;
using System.Collections.Generic;
using PPlus.Drivers;

namespace PPlus
{
internal class AppendTextControl : IAppendText
{
private readonly List<Segment> _segments = new();

public IAppendText And(string text)
{
if (!string.IsNullOrEmpty(text))
{
_segments.Add(new Segment(text, new Style(Style.Default.Foreground, Style.Default.Background, Style.Default.OverflowStrategy)));
}
return this;
}

public IAppendText And(string text, Style style)
{
if (!string.IsNullOrEmpty(text))
{
_segments.Add(new Segment(text, style));
}
return this;
}

public IAppendText And(string text, Color color)
{
if (!string.IsNullOrEmpty(text))
{
_segments.Add(new Segment(text, new Style(color,Style.Default.Background,Style.Default.OverflowStrategy)));
}
return this;
}

public void Write()
{
WriteSegments(false);
}

public void WriteLine()
{
WriteSegments(true);
}

private void WriteSegments(bool newline)
{
if (_segments.Count == 0)
{
return;
}
PromptPlus.Write(_segments[0].Text, _segments[0].Style, true);
for (int i = 1; i < _segments.Count; i++)
{
PromptPlus.Write(_segments[i].Text, _segments[i].Style, false);
}
if (newline)
{
PromptPlus.Write(Environment.NewLine, clearrestofline:false);
}
}
}
}
38 changes: 38 additions & 0 deletions Src/Drivers/DriveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ private static void CurrentDomain_ProcessExit(object? sender, EventArgs e)
ResetColor();
}

/// <summary>
/// Add text to the recording buffer
/// </summary>
/// <param name="text">Text to write</param>
/// <returns><see cref="IAppendText"/></returns>
public static IAppendText AppendText(string text)
{
var ctrl = new AppendTextControl();
ctrl.And(text);
return ctrl;
}

/// <summary>
/// Add text to the recording buffer with <see cref="Style"/>
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="style">The <see cref="Style"/></param>
/// <returns><see cref="IAppendText"/></returns>
public static IAppendText AppendText(string text, Style style)
{
var ctrl = new AppendTextControl();
ctrl.And(text, style);
return ctrl;
}

/// <summary>
/// Add text to the recording buffer with forecolor <see cref="Color"/>
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="color">The forecolor. <see cref="Color"/></param>
/// <returns><see cref="IAppendText"/></returns>
public static IAppendText AppendText(string text, Color color)
{
var ctrl = new AppendTextControl();
ctrl.And(text, color);
return ctrl;
}

/// <summary>
/// Get/set extra console exception info
/// </summary>
Expand Down
46 changes: 46 additions & 0 deletions Src/Drivers/IAppendText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ***************************************************************************************
// MIT LICENCE
// The maintenance and evolution is maintained by the PromptPlus project under MIT license
// ***************************************************************************************

namespace PPlus.Drivers
{
/// <summary>
/// Represents the interface with all Methods of the Conteole Append control
/// </summary>
public interface IAppendText
{
/// <summary>
/// Add text to the recording buffer
/// </summary>
/// <param name="text">Text to write</param>
/// <returns><see cref="IAppendText"/></returns>
IAppendText And(string text);

/// <summary>
/// Add text to the recording buffer with <see cref="Style"/>
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="style">The <see cref="Style"/></param>
/// <returns><see cref="IAppendText"/></returns>
IAppendText And(string text, Style style);

/// <summary>
/// Add text to the recording buffer with forecolor <see cref="Color"/>
/// </summary>
/// <param name="text">Text to write</param>
/// <param name="color">The forecolor. <see cref="Color"/></param>
/// <returns><see cref="IAppendText"/></returns>
IAppendText And(string text, Color color);

/// <summary>
/// Write a text to output console.
/// </summary>
void Write();

/// <summary>
/// Write line terminator a text to output console with line terminator.
/// </summary>
void WriteLine();
}
}
Loading

0 comments on commit 3de47f6

Please sign in to comment.