Skip to content

Commit

Permalink
bug fixed: #35, Pipeline control revised and WaitControl modified (Br…
Browse files Browse the repository at this point in the history
…eak-change) to have a Value Context for general use and ResultWaitProcess class created to return value of context and states of tasks
  • Loading branch information
Fernando Cerqueira committed Jul 25, 2023
1 parent 3eea558 commit b903904
Show file tree
Hide file tree
Showing 20 changed files with 873 additions and 137 deletions.
20 changes: 10 additions & 10 deletions Samples/WaitTasksSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ static void Main(string[] args)
.Run();

PromptPlus.DoubleDash($"Control:WaitProcess - normal usage sequencial mode");
var wt1 = PromptPlus.WaitProcess("wait process", "main desc")
var wt1 = PromptPlus.WaitProcess<object>("wait process", "main desc")
.Finish($"end wait all process")
.Interaction(steps1, (ctrl, item) =>
{
ctrl.AddStep(StepMode.Sequential, $"id{item}",null,
(cts) =>
(eventw, cts) =>
{
cts.WaitHandle.WaitOne(TimeSpan.FromSeconds(item));
});
})
.ShowElapsedTime()
.AddStep(StepMode.Sequential, "id5-10", "Desc 5 and 10",
(cts) =>
(eventw, cts) =>
{
cts.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));
},
(cts) =>
(eventw, cts) =>
{
cts.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
})
.Run();

if (!wt1.IsAborted)
{
foreach (var item in wt1.Value)
foreach (var item in wt1.Value.States)
{
PromptPlus.WriteLine($"You task {item.Id} - {item.Description}, {item.Status}, {item.ElapsedTime}, {item.StepMode}");
}
Expand All @@ -63,32 +63,32 @@ static void Main(string[] args)


PromptPlus.DoubleDash($"Control:WaitProcess - normal usage Parallel mode");
wt1 = PromptPlus.WaitProcess("wait process", "main desc")
wt1 = PromptPlus.WaitProcess<object>("wait process", "main desc")
.Finish($"end wait all process")
.TaskTitle("MyProcess")
.Interaction(steps2, (ctrl, item) =>
{
ctrl.AddStep(StepMode.Parallel, $"id{item}",null,
(cts) =>
(eventw, cts) =>
{
cts.WaitHandle.WaitOne(TimeSpan.FromSeconds(item));
});
})
.ShowElapsedTime()
.AddStep(StepMode.Parallel, "id5-10", "Desc 5 and 10",
(cts) =>
(eventw, cts) =>
{
cts.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));
},
(cts) =>
(eventw, cts) =>
{
cts.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
})
.Run();

if (!wt1.IsAborted)
{
foreach (var item in wt1.Value)
foreach (var item in wt1.Value.States)
{
PromptPlus.WriteLine($"You task {item.Id} - {item.Description}, {item.Status}, {item.ElapsedTime}, {item.StepMode}");
}
Expand Down
7 changes: 6 additions & 1 deletion Src/Controls/Pipeline/EventPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ namespace PPlus.Controls
/// <typeparam name="T">Typeof Input</typeparam>
public class EventPipe<T>
{
internal EventPipe(T value,string? from, string? current, string? to, ReadOnlyCollection<string> listpipes)
private EventPipe()
{
throw new PromptPlusException("EventPipe CTOR NotImplemented");
}

internal EventPipe(ref T value,string? from, string? current, string? to, ReadOnlyCollection<string> listpipes)
{
FromPipe = from;
CurrentPipe = current;
Expand Down
13 changes: 7 additions & 6 deletions Src/Controls/Pipeline/PipelineControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class PipelineControl<T> : BaseControl<ResultPipeline<T>>, IControlPipe
private EventPipe<T> _currentevent;
private ReadOnlyCollection<string> _pipes;
private List<PipeRunningStatus> _runpipes;

private T _context;
public PipelineControl(IConsoleControl console, PipelineOptions<T> options) : base(console, options)
{
_options = options;
Expand Down Expand Up @@ -65,14 +65,14 @@ public override ResultPrompt<ResultPipeline<T>> TryResult(CancellationToken canc
!_currentevent.CancelPipeLine ? PipeStatus.Executed : PipeStatus.Canceled,
_runpipes[index].Elapsedtime);
_currentevent = NextPipe(_currentevent, cancellationToken);
return new ResultPrompt<ResultPipeline<T>>(new ResultPipeline<T>(_currentevent.Input,_runpipes.ToArray()), _currentevent.CancelPipeLine, _currentevent.CurrentPipe != null, false, false);
return new ResultPrompt<ResultPipeline<T>>(new ResultPipeline<T>(_currentevent.Input, _runpipes.ToArray()), _currentevent.CancelPipeLine, _currentevent.CurrentPipe != null, false, false);
}

private EventPipe<T> NextPipe(EventPipe<T> curevent, CancellationToken cancellationToken)
{
if (curevent.ToPipe == null || curevent.CurrentPipe == null)
{
return new EventPipe<T>(curevent.Input, curevent.CurrentPipe, null, null, _pipes);
return new EventPipe<T>(ref _context, curevent.CurrentPipe, null, null, _pipes);
}
var from = curevent.CurrentPipe;
var cur = curevent.ToPipe;
Expand All @@ -81,7 +81,7 @@ private EventPipe<T> NextPipe(EventPipe<T> curevent, CancellationToken cancellat
{
to = _pipes[_pipes.IndexOf(cur) + 1];
}
var newevent = new EventPipe<T>(curevent.Input, from, cur, to, _pipes);
var newevent = new EventPipe<T>(ref _context, from, cur, to, _pipes);
while (cur != null && _options.Conditions.TryGetValue(cur, out var condition))
{
var sw = new Stopwatch();
Expand All @@ -105,9 +105,10 @@ private EventPipe<T> NextPipe(EventPipe<T> curevent, CancellationToken cancellat

public override string InitControl(CancellationToken cancellationToken)
{
_context = _options.CurrentValue;
if (_options.Pipes.Count == 0)
{
_currentevent = new EventPipe<T>(_options.CurrentValue, null, null, null, _pipes);
_currentevent = new EventPipe<T>(ref _context, null, null, null, _pipes);
return string.Empty;
}
_runpipes = new List<PipeRunningStatus>();
Expand All @@ -119,7 +120,7 @@ public override string InitControl(CancellationToken cancellationToken)
{
next = _pipes[1];
}
_currentevent = new EventPipe<T>(_options.CurrentValue, null, first, next, _pipes);
_currentevent = new EventPipe<T>(ref _context, null, first, next, _pipes);
while (first != null && _options.Conditions.TryGetValue(first, out var condition))
{
var sw = new Stopwatch();
Expand Down
43 changes: 43 additions & 0 deletions Src/Controls/ResultWaitProcess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ***************************************************************************************
// MIT LICENCE
// The maintenance and evolution is maintained by the PromptPlus project under MIT license
// ***************************************************************************************

using System.Collections.Generic;

namespace PPlus.Controls
{
/// <summary>
/// Represents The Result to WaitProcess Controls
/// </summary>
/// <typeparam name="T">Typeof return</typeparam>
public readonly struct ResultWaitProcess<T>
{
/// <summary>
/// Create a ResultPipeline
/// </summary>
/// <remarks>
/// Do not use this constructor!
/// </remarks>
public ResultWaitProcess()
{
throw new PromptPlusException("ResultWaitProcess CTOR NotImplemented");
}

internal ResultWaitProcess(T conext, StateProcess[] stateprocess)
{
Context = conext;
States = stateprocess;
}

/// <summary>
/// Get conext value
/// </summary>
public T Context { get; }

/// <summary>
/// Get State of process
/// </summary>
public StateProcess[] States { get; }
}
}
76 changes: 76 additions & 0 deletions Src/Controls/TasksProcess/EventWaitProcess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PPlus.Controls
{
/// <summary>
/// Represents the event to task process with with conex value
/// </summary>
/// <typeparam name="T">Typeof Input</typeparam>
public class EventWaitProcess<T>
{
private object _lock = new object();
private T _value = default;
private bool _cancelnext;

private EventWaitProcess()
{
throw new PromptPlusException("EventWaitProcess CTOR NotImplemented");
}

internal EventWaitProcess(ref T value, bool cancelnextalltasks)
{
_value = value;
_cancelnext = cancelnextalltasks;
}


/// <summary>
/// Get/set Context value
/// </summary>
public T Context
{
get
{
lock (_lock)
{
return _value;
}
}
set
{
lock (_lock)
{
_value = value;
}
}
}

/// <summary>
/// Get/Set Cancel all next tasks.
/// </summary>
public bool CancelAllNextTasks
{
get
{
lock (_lock)
{
return _cancelnext;
}
}
set
{
lock (_lock)
{
_cancelnext = value;
}
}
}
}
}

46 changes: 26 additions & 20 deletions Src/Controls/TasksProcess/IControlWait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,58 @@ namespace PPlus.Controls
/// <summary>
/// Represents the interface with all Methods of the WaitTimer/WaitProcess control
/// </summary>
public interface IControlWait : IPromptControls<IEnumerable<StateProcess>>
/// <typeparam name="T">typeof return</typeparam>
public interface IControlWait<T> : IPromptControls<ResultWaitProcess<T>>
{
/// <summary>
/// Execute a action foreach item of colletion passed as a parameter
/// </summary>
/// <typeparam name="T"> typeof item</typeparam>
/// <param name="values">Colletion for interaction</param>
/// <param name="action">Action to execute</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait Interaction<T>(IEnumerable<T> values, Action<IControlWait,T> action);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> Interaction<T1>(IEnumerable<T1> values, Action<IControlWait<T>,T1> action);

/// <summary>
/// Maximum number of concurrent tasks enable. Default vaue is number of processors.
/// </summary>
/// <param name="value">Number of concurrent tasks</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait MaxDegreeProcess(int value);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> MaxDegreeProcess(int value);

/// <summary>
/// Set Contex value for all tasks
/// </summary>
/// <param name="value">Context value</param>
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> Context(T value);

/// <summary>
/// Overwrite Task Title . Default task title comes from the embedded resource.
/// </summary>
/// <param name="value">TaskTitle Task</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait TaskTitle(string value);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> TaskTitle(string value);

/// <summary>
/// Define if show Elapsed Time for each task.
/// </summary>
/// <returns><see cref="IControlWait"/></returns>
IControlWait ShowElapsedTime();
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> ShowElapsedTime();

/// <summary>
/// Custom config the control.
/// </summary>
/// <param name="context">Action to apply changes. <see cref="IPromptConfig"/></param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait Config(Action<IPromptConfig> context);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> Config(Action<IPromptConfig> context);


/// <summary>
/// Finish answer to show when Wait process is completed.
/// </summary>
/// <param name="text">Text Finish answer</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait Finish(string text);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> Finish(string text);

/// <summary>
/// Overwrite <see cref="SpinnersType"/>. Default value is SpinnersType.Ascii
Expand All @@ -68,16 +74,16 @@ public interface IControlWait : IPromptControls<IEnumerable<StateProcess>>
/// <param name="SpinnerStyle">Style of spinner. <see cref="Style"/></param>
/// <param name="speedAnimation">Number of mileseconds foreach interation of spinner. Valid only to SpinnersType.custom, otherwise will be ignored</param>
/// <param name="customspinner">IEnumerable values for custom spinner. Valid only to SpinnersType.custom, otherwise will be ignored</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait Spinner(SpinnersType spinnersType, Style? SpinnerStyle = null, int? speedAnimation = null, IEnumerable<string>? customspinner = null);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> Spinner(SpinnersType spinnersType, Style? SpinnerStyle = null, int? speedAnimation = null, IEnumerable<string>? customspinner = null);

/// <summary>
/// Add list of tasks to execute.
/// </summary>
/// <param name="stepMode">Sequential or parallel execution</param>
/// <param name="process">list of tasks</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait AddStep(StepMode stepMode, params Action<CancellationToken>[] process);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> AddStep(StepMode stepMode, params Action<EventWaitProcess<T>, CancellationToken>[] process);

/// <summary>
/// Add list of tasks to execute with title and description
Expand All @@ -86,7 +92,7 @@ public interface IControlWait : IPromptControls<IEnumerable<StateProcess>>
/// <param name="id">Id of tasks</param>
/// <param name="label">Label of tasks</param>
/// <param name="process">list of tasks</param>
/// <returns><see cref="IControlWait"/></returns>
IControlWait AddStep(StepMode stepMode, string? id, string? label, params Action<CancellationToken>[] process);
/// <returns><see cref="IControlWait{T}"/></returns>
IControlWait<T> AddStep(StepMode stepMode, string? id, string? label, params Action<EventWaitProcess<T>,CancellationToken>[] process);
}
}
5 changes: 4 additions & 1 deletion Src/Controls/TasksProcess/ProgressBarControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class ProgressBarControl<T> : BaseControl<ResultProgessBar<T>>,IControl
private (int CursorLeft, int CursorTop) _spinnerCursor;
private (int CursorLeft, int CursorTop) _progressbarCursor;
private int lastline = -1;
private T _context;

public ProgressBarControl(IConsoleControl console, ProgressBarOptions<T> options) : base(console, options)
{
Expand All @@ -42,10 +43,12 @@ public override string InitControl(CancellationToken cancellationToken)
{
throw new PromptPlusException("Not have UpdateHandler to run");
}

_context = _options.ValueResult;
_ticketStep = double.Parse(_options.Witdth.ToString()) / (int.Parse(_options.Maxvalue.ToString()) - int.Parse(_options.Minvalue.ToString()));
_ctsesc = new CancellationTokenSource();
_lnkcts = CancellationTokenSource.CreateLinkedTokenSource(_ctsesc.Token, cancellationToken);
_handler = new UpdateProgressBar<T>(_options.ValueResult, _options.StartWith, _options.Minvalue, _options.Maxvalue, "");
_handler = new UpdateProgressBar<T>(ref _context, _options.StartWith, _options.Minvalue, _options.Maxvalue, "");
return _handler.Value.ToString();
}

Expand Down
Loading

0 comments on commit b903904

Please sign in to comment.