public void CompleteSuggestionIfNecessary() { if (this.currentSuggestions is null || this.currentSuggestions.Length == 0) { return; } ConsoleCommandParser.ParseCommandNameAndArguments( this.inputTextBox.Text, (ushort)this.inputTextBox.CaretIndex, out _, out var parsedArguments, out var parsedArgumentIndexForSuggestion); if (parsedArguments.Length == 0 || parsedArgumentIndexForSuggestion < 0) { return; } // autocomplete the current entry var suggestedText = this.currentSuggestions.Length > 0 ? this.currentSuggestions[this.suggestionCurrentIndex] : string.Empty; this.SetSuggestedText(suggestedText); }
private int FindCurrentSuggestionIndex() { if (this.currentSuggestions is null || this.currentSuggestions.Length == 0) { return(0); } ConsoleCommandParser.ParseCommandNameAndArguments( this.inputTextBox.Text, (ushort)this.inputTextBox.CaretIndex, out _, out var parsedArguments, out var parsedArgumentIndexForSuggestion); if (parsedArguments.Length == 0 || parsedArgumentIndexForSuggestion < 0) { return(0); } var currentArgument = parsedArguments[parsedArgumentIndexForSuggestion]; return(Math.Max(0, Array.FindIndex(this.currentSuggestions, s => string.Equals(s, currentArgument, StringComparison.OrdinalIgnoreCase)))); }
public static void Main() { var factory = new CosmeticsFactory(); var shoppingCart = new ShoppingCart(); var consoleCommandParser = new ConsoleCommandParser(); var engine = new CosmeticsEngine(factory, shoppingCart, consoleCommandParser); engine.Start(); }
static async Task Main(string[] args) { var logger = _container.GetService <ILogger <Program> >(); var consolePrinter = _container.GetService <IConsolePrinter>(); var controller = _container.GetService <IController>(); var mediator = _container.GetService <IMediator>(); var consoleFlagParser = new ConsoleFlagParser(); var consoleCommandParser = new ConsoleCommandParser(consoleFlagParser); try { await mediator.Send(new InitializeStorageCommand()); var argsFlags = consoleFlagParser.Parse(args); var credentials = GetCredentials(argsFlags); var isAuthQuery = new IsAuthenticatedQuery(credentials.Login, credentials.Password); if (!(await mediator.Send(isAuthQuery))) { throw new ArgumentException("Incorrect login or password"); } consolePrinter.PrintAuthenticationSuccessful(); var command = new StorageCommand(); while (command.CommandType != StorageCommands.Exit) { try { command = GetCommand(consoleCommandParser, consolePrinter); await controller.ExecuteConsoleCommand(command); } catch (AggregateException agEx) { foreach (var innerEx in agEx.InnerExceptions) { string logMessage = ConvertingHelper.GetLogMessage(innerEx.Message, innerEx.StackTrace); logger.LogError(logMessage); consolePrinter.PrintErrorMessage(innerEx.Message); } } catch (Exception ex) { string logMessage = ConvertingHelper.GetLogMessage(ex.Message, ex.StackTrace); logger.LogError(logMessage); consolePrinter.PrintErrorMessage(ex.Message); } } } catch (Exception ex) { string logMessage = ConvertingHelper.GetLogMessage(ex.Message, ex.StackTrace); logger.LogError(logMessage); consolePrinter.PrintErrorMessage(ex.Message); Environment.Exit(-1); } }
private static StorageCommand GetCommand(ConsoleCommandParser consoleCommandParser, IConsolePrinter consolePrinter) { consolePrinter.PrintСommandWaitingIcon(); string rowCommand = Console.ReadLine(); if (string.IsNullOrWhiteSpace(rowCommand)) { throw new ArgumentException("You have not entered a command."); } return(consoleCommandParser.Parse(rowCommand.ToLower().Trim())); }
private void ResolveStartupArgs(StartupEventArgs startupEventArgs) { var command = string.Concat(startupEventArgs.Args.Select(e => e + " ")).TrimEnd(); var commandParser = new ConsoleCommandParser(); commandParser.RegisterCommands(DefaultCommands.AllCommands); StartupCommandStrings = command.Split('|'); var parsedCommands = new List <CommandParseResult>(); foreach (var cmd in StartupCommandStrings) { parsedCommands.Add(commandParser.ParseCommand(cmd)); } StartupCommands = parsedCommands; }
private void UpdateSuggestionsMenuAndGhost() { var isNeedServerCommandPrefix = false; var text = new StringBuilder(this.inputTextBox.Text); if (text.Length > 0 && text[0] == ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient) { isNeedServerCommandPrefix = true; } ConsoleCommandParser.ParseCommandNameAndArguments( text.ToString(), (ushort)this.inputTextBox.CaretIndex, out var commandName, out var arguments, out var argumentIndexForSuggestion); text.Clear(); // reconstruct command if (isNeedServerCommandPrefix) { text.Append(ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient); } var suggestionsListControlCharsOffset = 0; text.Append(commandName); for (var index = 0; index < arguments.Length; index++) { // get argument text var argument = index == argumentIndexForSuggestion ? commandName // argument is suggested : arguments[index]; text.Append(' '); var isQuotedArgument = argument.IndexOf(' ') > -1; if (isQuotedArgument) { text.Append('\"'); } text.Append(argument); if (isQuotedArgument) { text.Append('\"'); } if (index == argumentIndexForSuggestion) { suggestionsListControlCharsOffset = text.Length - argument.Length - (isQuotedArgument ? 2 : 0); } } if (isNeedServerCommandPrefix && argumentIndexForSuggestion == -1) { // add extra padding (server command name) suggestionsListControlCharsOffset++; } this.viewModelConsoleControl.SetSuggestionsControlOffset(suggestionsListControlCharsOffset); this.UpdateSuggestionGhostOnly(); }
/// <summary> /// Swaps commandname or argument text with the suggested text. /// </summary> /// <param name="suggestedText"></param> private void SetSuggestedText(string suggestedText) { if (suggestedText.Length == 0) { this.UpdateSuggestionGhostOnly(); return; } var isNeedServerCommandPrefix = false; var text = new StringBuilder(this.inputTextBox.Text); if (text.Length > 0 && text[0] == ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient) { isNeedServerCommandPrefix = true; } ConsoleCommandParser.ParseCommandNameAndArguments( text.ToString(), (ushort)this.inputTextBox.CaretIndex, out var parsedCommandName, out var parsedArguments, out var parsedArgumentIndexForSuggestion); text.Clear(); // reconstruct command if (isNeedServerCommandPrefix) { text.Append(ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient); } int?caretIndex = null; if (parsedArgumentIndexForSuggestion == -1 || this.currentCommandVariant == null || (!string.Equals(parsedCommandName, this.currentCommandVariant.ConsoleCommand.Name, StringComparison.OrdinalIgnoreCase) && !string.Equals(parsedCommandName, this.currentCommandVariant.ConsoleCommand.Alias, StringComparison.OrdinalIgnoreCase))) { // command name is suggested parsedCommandName = suggestedText; parsedArguments = Array.Empty <string>(); } text.Append(parsedCommandName); for (var index = 0; index < parsedArguments.Length; index++) { text.Append(' '); // get argument text var argument = index == parsedArgumentIndexForSuggestion ? suggestedText // argument is suggested : parsedArguments[index]; var isQuotedArgument = argument.IndexOf(' ') > -1; if (isQuotedArgument) { text.Append('\"'); } text.Append(argument); if (isQuotedArgument) { text.Append('\"'); } if (index == parsedArgumentIndexForSuggestion) { caretIndex = text.Length; } } this.inputTextBox.Text = text.ToString(); this.inputTextBox.CaretIndex = caretIndex ?? text.Length; this.UpdateSuggestionsMenuAndGhost(); }
public void UpdateSuggestionGhostOnly() { var text = this.inputTextBox.Text; if (text.Length == 0) { this.textBlockSuggestionGhost.Text = string.Empty; return; } var stringBuilder = new StringBuilder(capacity: 100); string commandName; if (this.currentCommandVariant == null) { // get suggested command name if (text[0] == ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient) { // cannot suggest server commands on client } else { commandName = ConsoleCommandsSystem.SharedGetCommandNamesSuggestions(text) .FirstOrDefault()?.GetNameOrAlias(text); stringBuilder.Append(commandName); } this.textBlockSuggestionGhost.Text = stringBuilder.ToString(); return; } var consoleCommand = this.currentCommandVariant.ConsoleCommand; { var name = text.TrimStart(ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient); var indexOfSpace = name.IndexOf(' '); if (indexOfSpace > 0) { name = name.Substring(0, indexOfSpace); } if (!consoleCommand.Name.StartsWith(name) && (consoleCommand.Alias == null || !consoleCommand.Alias.StartsWith(name))) { // current command variant is different from what is displayed now // remove ghost this.textBlockSuggestionGhost.Text = string.Empty; return; } } ConsoleCommandParser.ParseCommandNameAndArguments( text, textPosition: (ushort)this.inputTextBox.CaretIndex, commandName: out commandName, arguments: out var arguments, argumentIndexForSuggestion: out var argumentIndexForSuggestion); if (text[0] == ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient) { // append server command prefix stringBuilder.Append(ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient); text = text.Substring(1); } var isCommandNameParsedSuccessfully = this.currentCommandVariant != null && (commandName.Equals(consoleCommand.Name, StringComparison.OrdinalIgnoreCase) || commandName.Equals( consoleCommand.Alias, StringComparison.OrdinalIgnoreCase)); if (!isCommandNameParsedSuccessfully) { // need to print ghost for the command name commandName = consoleCommand.GetNameOrAlias(commandName); stringBuilder.Append(commandName) .Append(' '); } else { // no need to print any ghost before for the current auto-complete argument // add padding before this argument var paddingCount = text.TrimEnd(' ').Length + 1; stringBuilder.Append(' ', repeatCount: paddingCount); } // skip all parameters before latest entered and then append the "ghost text" for all the remaining parameters var parametersToSkipCount = Math.Max(arguments.Count(a => !string.IsNullOrEmpty(a)), argumentIndexForSuggestion); var parameters = this.currentCommandVariant.Parameters; { var isFirst = true; foreach (var commandParameter in parameters.Skip(parametersToSkipCount)) { if (isFirst) { isFirst = false; } else { stringBuilder.Append(' '); } stringBuilder.Append(commandParameter.Name); } } this.textBlockSuggestionGhost.Text = stringBuilder.ToString(); }
public void OneTimeSetUp() { _parser = new ConsoleCommandParser(); }