/// <summary> /// Writes the prompt message and takes over the console until the user makes a selection or cancels via the escape key. This method /// never returns a NoOp result. /// </summary> /// <param name="parentContext">context about the parent reader that we are assisting </param> /// <returns>A selection or cancellation result, never a NoOp</returns> public virtual ContextAssistResult DrawMenu(RichCommandLineContext parentContext) { try { DoSearchInternal(parentContext, null, true); return(ContextAssistResult.CreateInsertResult(parentContext, SelectedValue.RichDisplayText)); } catch (OperationCanceledException) { return(ContextAssistResult.Cancel); } }
/// <summary> /// Creates a result that replaces the current token with the given selection. /// </summary> /// <param name="context">Context from the parent reader</param> /// <param name="selection">The selection string to insert</param> /// <returns>a result that replaces the current token with the given selection</returns> public static ContextAssistResult CreateInsertResult(RichCommandLineContext context, ConsoleString selection) { context.RefreshTokenInfo(); var ret = new ContextAssistResult(); bool hasInserted = false; var newBuffer = new List <ConsoleCharacter>(); foreach (var token in context.Tokens) { if (context.IsCursorOnToken(token)) { if (string.IsNullOrWhiteSpace(token.Value)) { newBuffer.AddRange(context.GetBufferSubstringFromToken(token)); ret.ConsoleRefreshLeftOffset = selection.Length; } else { var tokenOffset = context.BufferPosition - token.StartIndex; ret.ConsoleRefreshLeftOffset = selection.Length - tokenOffset; } if (hasInserted == false) { hasInserted = true; // cursor is on the current token newBuffer.AddRange(selection); } } else { // this token not be modified newBuffer.AddRange(context.GetBufferSubstringFromToken(token)); } } if (hasInserted == false) { hasInserted = true; // cursor is on the current token newBuffer.AddRange(selection); ret.ConsoleRefreshLeftOffset = selection.Length; } ret.StatusCode = ContextAssistResultStatusCode.Success; ret.NewBuffer = newBuffer; return(ret); }
public void Handle(RichCommandLineContext context) { if (context.KeyPressed.Modifiers.HasFlag(ConsoleModifiers.Control) == false) { return; } context.Intercept = true; context.RefreshTokenInfo(); if (ContextAssistProvider == null || ContextAssistProvider.CanAssist(context) == false) { return; } int left = context.Console.CursorLeft; int top = context.Console.CursorTop; ContextAssistResult result = ContextAssistResult.NoOp; try { context.Console.WriteLine("\n"); result = ContextAssistProvider.DrawMenu(context); while (result.IsTerminal == false) { var key = context.Console.ReadKey(true); result = ContextAssistProvider.OnKeyboardInput(context, key); } } finally { ContextAssistProvider.ClearMenu(context); context.Console.CursorLeft = left; context.Console.CursorTop = top; } if (result.StatusCode == ContextAssistResultStatusCode.Success) { context.ClearConsole(); context.Console.CursorLeft = left; context.Console.CursorTop = top; context.Buffer.Clear(); context.Buffer.AddRange(result.NewBuffer); context.RefreshConsole(result.ConsoleRefreshLeftOffset, 0); } }
/// <summary> /// Creates a result that replaces the current token with the given selection. /// </summary> /// <param name="context">Context from the parent reader</param> /// <param name="selection">The selection string to insert</param> /// <returns>a result that replaces the current token with the given selection</returns> public static ContextAssistResult CreateInsertResult(RichCommandLineContext context, ConsoleString selection) { context.RefreshTokenInfo(); var ret = new ContextAssistResult(); bool hasInserted = false; var newBuffer = new List<ConsoleCharacter>(); foreach (var token in context.Tokens) { if (context.IsCursorOnToken(token)) { if (string.IsNullOrWhiteSpace(token.Value)) { newBuffer.AddRange(context.GetBufferSubstringFromToken(token)); ret.ConsoleRefreshLeftOffset = selection.Length; } else { var tokenOffset = context.BufferPosition - token.StartIndex; ret.ConsoleRefreshLeftOffset = selection.Length - tokenOffset; } if (hasInserted == false) { hasInserted = true; // cursor is on the current token newBuffer.AddRange(selection); } } else { // this token not be modified newBuffer.AddRange(context.GetBufferSubstringFromToken(token)); } } if (hasInserted == false) { hasInserted = true; // cursor is on the current token newBuffer.AddRange(selection); ret.ConsoleRefreshLeftOffset = selection.Length; } ret.StatusCode = ContextAssistResultStatusCode.Success; ret.NewBuffer = newBuffer; return ret; }