Add interactive exit function
This commit is contained in:
parent
b36efadefd
commit
1794d4ed8a
3 changed files with 64 additions and 6 deletions
7
src/cs-mic-cli/ExitRequestedState.cs
Normal file
7
src/cs-mic-cli/ExitRequestedState.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace cs_mic_cli
|
||||
{
|
||||
internal class ExitRequestedState
|
||||
{
|
||||
public bool Requested { get; set; }
|
||||
}
|
||||
}
|
||||
45
src/cs-mic-cli/Functions/Exit.cs
Normal file
45
src/cs-mic-cli/Functions/Exit.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using CSMic;
|
||||
|
||||
namespace cs_mic_cli.Functions
|
||||
{
|
||||
internal class Exit : ICodedFunction
|
||||
{
|
||||
private readonly ExitRequestedState exitRequestedState;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "exit";
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue ReturnValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return FunctionValue.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
public Exit(ExitRequestedState exitRequestedState)
|
||||
{
|
||||
this.exitRequestedState = exitRequestedState;
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
this.exitRequestedState.Requested = true;
|
||||
|
||||
return FunctionValue.NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -71,16 +71,17 @@ namespace cs_mic_cli
|
|||
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
|
||||
{
|
||||
// console mode
|
||||
bool exitRequested = false;
|
||||
var exitRequestedState = new ExitRequestedState();
|
||||
inputInterpreter.RegisterFunction(new Exit(exitRequestedState));
|
||||
|
||||
Console.CancelKeyPress += (_, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
exitRequested = true;
|
||||
e.Cancel = false;
|
||||
exitRequestedState.Requested = true;
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
while (!exitRequested)
|
||||
while (!exitRequestedState.Requested)
|
||||
{
|
||||
Console.Write("csmic> ");
|
||||
|
||||
|
|
@ -92,7 +93,7 @@ namespace cs_mic_cli
|
|||
break;
|
||||
}
|
||||
|
||||
Calculate(inputInterpreter, options, fullExpression);
|
||||
Calculate(inputInterpreter, options, fullExpression, exitRequestedState);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -104,10 +105,15 @@ namespace cs_mic_cli
|
|||
return 0;
|
||||
}
|
||||
|
||||
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression)
|
||||
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression, ExitRequestedState? exitRequestedState = null)
|
||||
{
|
||||
inputInterpreter.Interpret(expression);
|
||||
|
||||
if (exitRequestedState?.Requested == true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(inputInterpreter.NumericValue);
|
||||
if(options.Verbose)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue