2020-01-12 19:15:57 +00:00
using System.IO ;
using System.Threading ;
using System.Threading.Tasks ;
2020-10-03 10:48:25 +00:00
using Elsa.ActivityResults ;
2020-01-12 19:15:57 +00:00
using Elsa.Attributes ;
using Elsa.Services ;
using Elsa.Services.Models ;
// ReSharper disable once CheckNamespace
namespace Elsa.Activities.Console
{
/// <summary>
/// Reads input from the console.
/// </summary>
[ ActivityDefinition (
Category = "Console" ,
Description = "Read text from standard in." ,
Icon = "fas fa-terminal" ,
2020-10-03 10:48:25 +00:00
RuntimeDescription =
"a => !!a.state.variableName ? `Read text from standard in and store into <strong>${ a.state.variableName }</strong>.` : 'Read text from standard in.'" ,
2020-01-12 19:15:57 +00:00
Outcomes = new [ ] { OutcomeNames . Done }
) ]
public class ReadLine : Activity
{
2020-10-09 17:52:54 +00:00
private readonly TextReader _input ;
2020-01-12 19:15:57 +00:00
public ReadLine ( )
{
}
public ReadLine ( TextReader input )
{
2020-10-10 15:13:52 +00:00
_input = input ;
2020-01-12 19:15:57 +00:00
}
2020-10-13 20:21:47 +00:00
protected override async ValueTask < IActivityExecutionResult > OnExecuteAsync (
2020-10-03 10:48:25 +00:00
ActivityExecutionContext context ,
CancellationToken cancellationToken )
2020-01-12 19:15:57 +00:00
{
2020-10-09 17:52:54 +00:00
if ( _input = = null )
2020-01-12 19:15:57 +00:00
return Suspend ( ) ;
2020-10-09 17:52:54 +00:00
var receivedInput = await _input . ReadLineAsync ( ) ;
2020-01-12 19:15:57 +00:00
return Execute ( receivedInput ) ;
}
protected override IActivityExecutionResult OnResume ( ActivityExecutionContext context )
{
2020-10-03 10:48:25 +00:00
var receivedInput = ( string ) context . Input ;
2020-01-12 19:15:57 +00:00
return Execute ( receivedInput ) ;
}
2020-10-18 15:05:48 +00:00
private IActivityExecutionResult Execute ( string receivedInput ) = > Done ( receivedInput ) ;
2020-01-12 19:15:57 +00:00
}
2018-10-14 15:16:33 +00:00
}