/// <summary> /// Invokes a ShiftOS terminal command. /// </summary> /// <param name="text">The full command text in regular ShiftOS syntax</param> /// <param name="isRemote">Whether the command should be sent through Remote Terminal Session (RTS).</param> public static void InvokeCommand(string text, bool isRemote = false) { if (string.IsNullOrWhiteSpace(text)) { return; } var tw = new MemoryTextWriter(); Console.SetOut(tw); try { var args = GetArgs(ref text); bool commandWasClient = RunClient(text, args, isRemote); if (!commandWasClient) { Console.WriteLine("Error: Command not found."); } CommandProcessed?.Invoke(text, GetSentArgs(args)); } catch (Exception ex) { Console.WriteLine($"Command parse error: {ex.Message}"); PrefixEnabled = true; } string buffer = tw.ToString(); LastCommandBuffer = buffer; Console.SetOut(new TerminalTextWriter()); if (!isRemote) { Console.Write(buffer); } }
private void OnCommandProcessed(Session session, ImageMetaData selected) { CommandProcessed?.Invoke(this, new CommandProcessedEventArgs() { Session = session, Parameter = selected }); }
/// <summary> /// Fires <see cref="CommandProcessed"/> event, passing <paramref name="session"/> and <paramref name="parameter"/> to subscribers /// </summary> /// <param name="session"></param> /// <param name="parameter"></param> protected virtual void OnCommandProcessed(Session session, object parameter) { CommandProcessed?.Invoke(this, new CommandProcessedEventArgs() { Session = session, Parameter = parameter }); }
private async Task <ICommand?> TryExecuteCommandAsync(ICommand?lastCommand, CancellationToken cancellationToken) { ICommand?commandWithError = null; ICommand?newLastCommand = null; var errorOccured = false; var command = _commandPool.Pop(); try { if (command != null) { await SetHasPendingCommandsAsync(true, cancellationToken).ConfigureAwait(false); try { command.Execute(); } catch (InvalidOperationException) { commandWithError = command; throw; } await HandleRepeatableAsync(command, cancellationToken).ConfigureAwait(false); newLastCommand = command; } else { await SetHasPendingCommandsAsync(false, cancellationToken).ConfigureAwait(false); if (lastCommand != null) { CommandProcessed?.Invoke(this, EventArgs.Empty); newLastCommand = null; } } } catch (InvalidOperationException exception) { errorOccured = true; DoErrorOccured(commandWithError, exception); newLastCommand = null; } finally { if (errorOccured) { await SetHasPendingCommandsAsync(false, cancellationToken).ConfigureAwait(false); CommandProcessed?.Invoke(this, EventArgs.Empty); newLastCommand = null; } } return(newLastCommand); }
private async Task HandleRepeatableAsync(ICommand?command, CancellationToken cancellationToken) { if (command is IRepeatableCommand repeatableCommand) { // It is necesary because CanRepeate and CanExecute can perform early that globe updates its state. await WaitGlobeIterationPerformedAsync().ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); if (_commandLoopContext.HasNextIteration && repeatableCommand.CanRepeat() && repeatableCommand.CanExecute().IsSuccess) { cancellationToken.ThrowIfCancellationRequested(); repeatableCommand.IncreaceIteration(); _commandPool.Push(repeatableCommand); CommandAutoExecuted?.Invoke(this, EventArgs.Empty); } else { await SetHasPendingCommandsAsync(false, cancellationToken).ConfigureAwait(false); CommandProcessed?.Invoke(this, EventArgs.Empty); } } else { await SetHasPendingCommandsAsync(false, cancellationToken).ConfigureAwait(false); CommandProcessed?.Invoke(this, EventArgs.Empty); } }
/// <summary> /// Invokes a ShiftOS terminal command. /// </summary> /// <param name="command">The command name.</param> /// <param name="arguments">The command arguments.</param> /// <param name="isRemote">Whether the command should be sent through Remote Terminal Session (RTS).</param> public static void InvokeCommand(string command, Dictionary <string, string> arguments, bool isRemote = false) { try { bool commandWasClient = RunClient(command, arguments, isRemote); if (!commandWasClient) { Console.WriteLine("{ERR_COMMANDNOTFOUND}"); } CommandProcessed?.Invoke(command, JsonConvert.SerializeObject(arguments)); } catch (Exception ex) { Console.WriteLine("{ERR_SYNTAXERROR}"); PrefixEnabled = true; } }
protected virtual void OnCommandProcessed(object sender, CommandProcessedEventArgs eventArgs) { CommandProcessed?.Invoke(sender, eventArgs); }