private void HandleBackspace(ObservableCollection <string> document, SubmissionRenderer renderer) { var start = renderer.CurrentChar; if (start == 0) { if (renderer.CurrentLine == 0) { return; } var currentLine = document[renderer.CurrentLine]; var previousLine = document[renderer.CurrentLine - 1]; document.RemoveAt(renderer.CurrentLine); renderer.CurrentLine--; document[renderer.CurrentLine] = previousLine + currentLine; renderer.CurrentChar = previousLine.Length; } else { var lineIndex = renderer.CurrentLine; var line = document[lineIndex]; var before = line.Substring(0, start - 1); var after = line.Substring(start); document[lineIndex] = before + after; renderer.CurrentChar--; } }
private void HandleRightArrow(ObservableCollection <string> document, SubmissionRenderer renderer) { if (renderer.CurrentChar < document[renderer.CurrentLine].Length) { renderer.CurrentChar++; } }
private void HandleLeftArrow(ObservableCollection <string> document, SubmissionRenderer renderer) { if (renderer.CurrentChar > 0) { renderer.CurrentChar--; } }
private void HandleEscape(ObservableCollection <string> document, SubmissionRenderer renderer) { document.Clear(); document.Add(string.Empty); renderer.CurrentLine = 0; renderer.CurrentChar = 0; }
private void HandleTyping(ObservableCollection <string> document, SubmissionRenderer renderer, string text) { var lineIndex = renderer.CurrentLine; var start = renderer.CurrentChar; document[lineIndex] = document[lineIndex].Insert(start, text); renderer.CurrentChar += text.Length; }
private void HandlePageDown(ObservableCollection <string> document, SubmissionRenderer renderer) { submissionHistoryIndex++; if (submissionHistoryIndex > submissionHistory.Count - 1) { submissionHistoryIndex = 0; } UpdateDocumentFromHistory(document, renderer); }
private void HandlePageUp(ObservableCollection <string> document, SubmissionRenderer renderer) { submissionHistoryIndex--; if (submissionHistoryIndex < 0) { submissionHistoryIndex = submissionHistory.Count - 1; } UpdateDocumentFromHistory(document, renderer); }
private void HandleTab(ObservableCollection <string> document, SubmissionRenderer renderer) { const int TabWidth = 4; var start = renderer.CurrentChar; var remainingSpaces = TabWidth - start % TabWidth; var line = document[renderer.CurrentLine]; document[renderer.CurrentLine] = line.Insert(start, new string(' ', remainingSpaces)); renderer.CurrentChar += remainingSpaces; }
private void HandleDownArrow(ObservableCollection <string> document, SubmissionRenderer renderer) { if (renderer.CurrentLine < document.Count - 1) { renderer.CurrentLine++; } else { HandlePageDown(document, renderer); } }
private void HandleUpArrow(ObservableCollection <string> document, SubmissionRenderer renderer) { if (renderer.CurrentLine > 0) { renderer.CurrentLine--; } else { HandlePageUp(document, renderer); } }
private void HandleEnter(ObservableCollection <string> document, SubmissionRenderer renderer) { var submissionText = string.Join(Environment.NewLine, document); if (IsSubmissionComplete(submissionText)) { editDone = true; return; } InsertLine(document, renderer); }
private void InsertLine(ObservableCollection <string> document, SubmissionRenderer renderer) { var remainder = document[renderer.CurrentLine].Substring(renderer.CurrentChar); document[renderer.CurrentLine] = document[renderer.CurrentLine].Substring(0, renderer.CurrentChar); var lineIndex = renderer.CurrentLine + 1; document.Insert(lineIndex, remainder); renderer.CurrentChar = 0; renderer.CurrentLine = lineIndex; }
private string EditSubmission() { editDone = false; var document = new ObservableCollection <string>() { "" }; var renderer = new SubmissionRenderer(this, document); while (!editDone) { var key = Console.ReadKey(true); HandleKey(key, document, renderer); } return(string.Join(Environment.NewLine, document)); }
private void HandleDelete(ObservableCollection <string> document, SubmissionRenderer renderer) { var lineIndex = renderer.CurrentLine; var line = document[lineIndex]; var start = renderer.CurrentChar; if (start >= line.Length) { if (renderer.CurrentLine == document.Count - 1) { return; } var nextLine = document[renderer.CurrentLine + 1]; document[renderer.CurrentLine] += nextLine; document.RemoveAt(renderer.CurrentLine + 1); return; } var before = line.Substring(0, start); var after = line.Substring(start + 1); document[lineIndex] = before + after; }
private void HandleKey(ConsoleKeyInfo key, ObservableCollection <string> document, SubmissionRenderer renderer) { if (key.Modifiers == default(ConsoleModifiers)) { switch (key.Key) { case ConsoleKey.Backspace: HandleBackspace(document, renderer); break; case ConsoleKey.Tab: HandleTab(document, renderer); break; case ConsoleKey.Enter: HandleEnter(document, renderer); break; case ConsoleKey.Escape: HandleEscape(document, renderer); break; case ConsoleKey.PageUp: HandlePageUp(document, renderer); break; case ConsoleKey.PageDown: HandlePageDown(document, renderer); break; case ConsoleKey.End: HandleEnd(document, renderer); break; case ConsoleKey.Home: HandleHome(document, renderer); break; case ConsoleKey.LeftArrow: HandleLeftArrow(document, renderer); break; case ConsoleKey.UpArrow: HandleUpArrow(document, renderer); break; case ConsoleKey.RightArrow: HandleRightArrow(document, renderer); break; case ConsoleKey.DownArrow: HandleDownArrow(document, renderer); break; case ConsoleKey.Delete: HandleDelete(document, renderer); break; } } else if (key.Modifiers == ConsoleModifiers.Control) { switch (key.Key) { case ConsoleKey.Enter: HandleCtrlEnter(document, renderer); break; } } if (key.Key != ConsoleKey.Backspace && key.KeyChar >= ' ') { HandleTyping(document, renderer, key.KeyChar.ToString()); } }
private void UpdateDocumentFromHistory(ObservableCollection <string> document, SubmissionRenderer view) { if (submissionHistory.Count == 0) { return; } document.Clear(); Console.WriteLine(submissionHistoryIndex); var historyItem = submissionHistory[submissionHistoryIndex]; var lines = historyItem.Split(Environment.NewLine); foreach (var line in lines) { document.Add(line); } view.CurrentLine = document.Count - 1; view.CurrentChar = document[view.CurrentLine].Length; }
private void HandleCtrlEnter(ObservableCollection <string> document, SubmissionRenderer renderer) { InsertLine(document, renderer); }
private void HandleHome(ObservableCollection <string> document, SubmissionRenderer renderer) { renderer.CurrentChar = 0; }
private void HandleEnd(ObservableCollection <string> document, SubmissionRenderer renderer) { renderer.CurrentChar = document[renderer.CurrentLine].Length; }