/// <summary> /// Computes the visual lines from the console text lines. /// </summary> /// <remarks> /// Text lines are split into several lines if they are larger than _numberOfColumns /// or if they contain '\n'. /// </remarks> private void WrapLines() { _wrappedLines.Clear(); // Temporarily, add text that is currently entered to the text lines. TextLines.Add(Prompt + Text); // Go through TextLines and split lines at if they are to large or contains newlines // characters. StringBuilder builder = new StringBuilder(); foreach (string line in TextLines) { builder.Clear(); int lineLength = line.Length; if (lineLength == 0) { // Empty line. _wrappedLines.Add(string.Empty); continue; } // Go through line character by character. for (int i = 0; i < lineLength; i++) { char c = line[i]; if (c == '\n') // ----- A newline character. { _wrappedLines.Add(builder.ToString()); builder.Clear(); } else if (builder.Length < _numberOfColumns) // ----- A character that fits onto the line. { builder.Append(c); if (i == lineLength - 1) { // Finish line if this is the last character. _wrappedLines.Add(builder.ToString()); builder.Clear(); } } else if (builder.Length >= _numberOfColumns) // ----- Reached the console column limit. { // Wrap line. _wrappedLines.Add(builder.ToString()); builder.Clear(); i--; // Handle the character again in the next loop. } } } // Remove the temporarily added current text. TextLines.RemoveAt(TextLines.Count - 1); }
private void LimitText() { // Limit entries in TextLines and entries in command history. while (TextLines.Count > MaxLines) { TextLines.RemoveAt(0); InvalidateArrange(); } while (History.Count > MaxHistoryEntries) { History.RemoveAt(0); InvalidateArrange(); } }