/// <summary> /// Lets you register a custom key handler. You are responsible for ensuring that each key is only handled by one handler. This method will throw if /// you try to add a duplicate key handler. /// </summary> /// <param name="handler">The handler to register</param> public void RegisterHandler(IKeyHandler handler) { foreach (var key in handler.KeysHandled) { KeyHandlers.Add(key, handler); } }
/// <summary> /// Unregisters the given key handler from the reader. You should only do this if you're planning on overriding the default handlers, and you should do so /// with caution. /// </summary> /// <param name="handler">The handler to unregister</param> public void UnregisterHandler(IKeyHandler handler) { foreach (var key in handler.KeysHandled) { KeyHandlers.Remove(key); } }
/// <summary> /// Registers a keypress with the editor. /// </summary> /// <param name="key">The key press info</param> /// <param name="prototype">if specified, the foreground and background color will be taken from this prototype, otherwise the system defaults will be used</param> public void RegisterKeyPress(ConsoleKeyInfo key, ConsoleCharacter?prototype = null) { Context.Reset(); Context.KeyPressed = key; Context.CharacterToWrite = new ConsoleCharacter(Context.KeyPressed.KeyChar, prototype.HasValue ? prototype.Value.ForegroundColor : ConsoleString.DefaultForegroundColor, prototype.HasValue ? prototype.Value.BackgroundColor : ConsoleString.DefaultBackgroundColor); IKeyHandler handler = null; if (KeyHandlers.TryGetValue(Context.KeyPressed.Key, out handler) == false && RichTextCommandLineReader.IsWriteable(Context.KeyPressed)) { WriteCharacterForPressedKey(Context); DoSyntaxHighlighting(Context); } else if (handler != null) { handler.Handle(Context); if (Context.Intercept == false && RichTextCommandLineReader.IsWriteable(Context.KeyPressed)) { WriteCharacterForPressedKey(Context); } DoSyntaxHighlighting(Context); } FireValueChanged(); }
public Shell(IShellState shellState) { shellState = shellState ?? throw new ArgumentNullException(nameof(shellState)); KeyHandlers.RegisterDefaultKeyHandlers(shellState.InputManager); ShellState = shellState; }
public void RegisterKeyPress(ConsoleKeyInfo key) { Context.Reset(); Context.KeyPressed = key; Context.CharacterToWrite = new ConsoleCharacter(Context.KeyPressed.KeyChar); IKeyHandler handler = null; if (KeyHandlers.TryGetValue(Context.KeyPressed.Key, out handler) == false && RichTextCommandLineReader.IsWriteable(Context.KeyPressed)) { WriteCharacterForPressedKey(Context.KeyPressed); DoSyntaxHighlighting(Context); } else if (handler != null) { handler.Handle(Context); if (Context.Intercept == false && RichTextCommandLineReader.IsWriteable(Context.KeyPressed)) { WriteCharacterForPressedKey(Context.KeyPressed); } DoSyntaxHighlighting(Context); } FireValueChanged(); }
/// <summary> /// Reads a line of text from the console. Any interactions you've configured before calling this method will be in effect. /// </summary> /// <param name="initialBuffer">Optionally seed the prompt with an initial value that the end user can modify</param> /// <returns>a line of text from the console</returns> public ConsoleString ReadLine(ConsoleString initialBuffer = null) { RichCommandLineContext context = new RichCommandLineContext(this.HistoryManager); context.Console = this.Console; context.ConsoleStartTop = this.Console.CursorTop; context.ConsoleStartLeft = this.Console.CursorLeft; if (initialBuffer != null) { context.ReplaceConsole(initialBuffer); } while (true) { context.Reset(); context.KeyPressed = this.Console.ReadKey(true); context.CharacterToWrite = new ConsoleCharacter(context.KeyPressed.KeyChar); context.BufferPosition = this.Console.CursorLeft - context.ConsoleStartLeft + (this.Console.CursorTop - context.ConsoleStartTop) * this.Console.BufferWidth; IKeyHandler handler = null; if (KeyHandlers.TryGetValue(context.KeyPressed.Key, out handler) == false && context.CharacterToWrite.Value != '\u0000') { context.WriteCharacterForPressedKey(); DoSyntaxHighlighting(context); } else if (handler != null) { handler.Handle(context); if (context.Intercept == false && context.CharacterToWrite.Value != '\u0000') { this.Console.Write(context.CharacterToWrite); } DoSyntaxHighlighting(context); if (context.IsFinished) { this.Console.WriteLine(); break; } } } return(new ConsoleString(context.Buffer)); }
/// <summary> /// Reads a line of text from the console. Any interactions you've configured before calling this method will be in effect. /// </summary> /// <param name="initialBuffer">Optionally seed the prompt with an initial value that the end user can modify</param> /// <returns>a line of text from the console</returns> public ConsoleString ReadLine(ConsoleString initialBuffer = null) { RichCommandLineContext context; lock (SyncLock) { context = new RichCommandLineContext(this.HistoryManager); context.Console = this.Console; context.ConsoleStartTop = this.Console.CursorTop; context.ConsoleStartLeft = this.Console.CursorLeft; } if (initialBuffer != null) { lock (SyncLock) { context.ReplaceConsole(initialBuffer); } } while (true) { context.Reset(); context.KeyPressed = this.Console.ReadKey(true); lock (SyncLock) { context.CharacterToWrite = new ConsoleCharacter(context.KeyPressed.KeyChar); context.BufferPosition = this.Console.CursorLeft - context.ConsoleStartLeft + (this.Console.CursorTop - context.ConsoleStartTop) * this.Console.BufferWidth; if (context.BufferPosition < 0 || context.BufferPosition > context.Buffer.Count) { var message = string.Format("The cursor is not located within the bounds of the buffer. Cursor: {0},{1} Start position: {2},{3} Buffer position: {4} Buffer length: {5}", this.Console.CursorTop, this.Console.CursorLeft, context.ConsoleStartTop, context.ConsoleStartLeft, context.BufferPosition, context.Buffer.Count); try { throw new IndexOutOfRangeException(message); } catch (Exception ex) { PowerLogger.LogLine(ex.ToString()); throw; } } IKeyHandler handler = null; if (KeyHandlers.TryGetValue(context.KeyPressed.Key, out handler) == false && IsWriteable(context.KeyPressed)) { context.WriteCharacterForPressedKey(); DoSyntaxHighlighting(context); } else if (handler != null) { handler.Handle(context); if (context.Intercept == false && IsWriteable(context.KeyPressed)) { context.WriteCharacterForPressedKey(); } DoSyntaxHighlighting(context); if (context.IsFinished) { this.Console.WriteLine(); break; } } if (AfterReadKey != null) { AfterReadKey(context); } } } return(new ConsoleString(context.Buffer)); }
/// <summary> /// Unregisters the handler for the given key /// </summary> /// <param name="key">the key to unregister</param> /// <returns>true if there was a handler registered and removed, false otherwise</returns> public bool UnregisterHandler(ConsoleKey key) { return(KeyHandlers.Remove(key)); }
public Shell(IShellState shellState) { KeyHandlers.RegisterDefaultKeyHandlers(shellState.InputManager); ShellState = shellState; }