protected void HandleEnter() { // Get editor buffer-aware line and column indicies int zeroIndexedCurrentLine = editor.EditorBuffer.CurrentLineIndex; int zeroIndexedCurrentColumn = editor.EditorBuffer.CurrentColumnIndex; // Get string after cursor string remainingSubstring = editor.GetTextBuffer().GetLine(zeroIndexedCurrentLine).Length > 0 ? remainingSubstring = editor.GetTextBuffer().GetLine(zeroIndexedCurrentLine).Substring(zeroIndexedCurrentColumn) : ""; editor.GetTextBuffer().Remove(zeroIndexedCurrentLine, zeroIndexedCurrentColumn, editor.GetTextBuffer().GetLine(zeroIndexedCurrentLine).Length - zeroIndexedCurrentColumn); editor.GetTextBuffer().InsertLine(zeroIndexedCurrentLine, remainingSubstring); // Clear out the text after the cursor of the current line // Console.Write("".PadRight(remainingSubstring.Length)); // Add a new line editor.EditorBuffer.NewLine(); // Remember the column position of the cursor CursorPosition newPosition = new CursorPosition(Console.CursorLeft, Console.CursorTop); // Write the new text Console.Write(remainingSubstring); // Move cursor back to beginning of new line newPosition.Restore(); }
protected void HandleInsert(char character) { // Remember the column position of the cursor CursorPosition position = new CursorPosition(Console.CursorLeft, Console.CursorTop); // Get editor buffer-aware line and column indicies int zeroIndexedCurrentLine = editor.EditorBuffer.CurrentLineIndex; int zeroIndexedCurrentColumn = editor.EditorBuffer.CurrentColumnIndex; // Insert character editor.GetTextBuffer().Insert(zeroIndexedCurrentLine, zeroIndexedCurrentColumn, character); // Restore the column position of the curor before rewriting the line position.Restore(); // Advance cursor position by one editor.EditorBuffer.AdvanceCursor(); }
public void paint() { // Remember console configuration CursorPosition originalCursorPosition = new CursorPosition( Console.CursorLeft, Console.CursorTop ); ColorConfiguration originalColorConfiguration = new ColorConfiguration( Console.BackgroundColor, Console.ForegroundColor ); position.Restore(); colorConfiguration.Restore(); Console.Write(GenerateContent()); // Reset stored configuration originalColorConfiguration.Restore(); originalCursorPosition.Restore(); }
protected void HandleBackpace() { // Remember the column position of the cursor CursorPosition position = new CursorPosition(Console.CursorLeft, Console.CursorTop); // Get editor buffer-aware line and column indicies int zeroIndexedCurrentLine = editor.EditorBuffer.CurrentLineIndex; int zeroIndexedCurrentColumn = editor.EditorBuffer.CurrentColumnIndex; int zeroIndexedPositionToDelete = zeroIndexedCurrentColumn - 1; if (zeroIndexedPositionToDelete < 0) { if (zeroIndexedCurrentLine > 0) { int textLengthFromPreviouLine = editor.TextBuffer.GetLine(zeroIndexedCurrentLine - 1).Length; string textFromCurrentLine = editor.TextBuffer.GetLine(zeroIndexedCurrentLine); editor.TextBuffer.RemoveLine(zeroIndexedCurrentLine); editor.TextBuffer.AppendToLine(zeroIndexedCurrentLine - 1, textFromCurrentLine); // Need to tell editor.EditorBuffer where cursor should be editor.EditorBuffer.MoveCursorTo(textLengthFromPreviouLine, zeroIndexedCurrentLine); } else { return; } } else { // Remove the character from the underlying text buffer editor.GetTextBuffer().Remove(zeroIndexedCurrentLine, zeroIndexedPositionToDelete, 1); // Reset cursor position position.Restore(); // Advance curson position back by one editor.EditorBuffer.MoveCursorLeft(); } }