$ show waf/PrettyPrompt
PrettyPrompt is a cross-platform .NET library that replaces `Console.ReadLine()` with a feature-rich terminal input experience. It provides syntax highlighting, autocompletion (with documentation and overload panes), persistent history, multi-line editing with word wrapping, text selection and clipboard operations, and undo/redo. The library is fully extensible via `PromptCallbacks` and `PromptConfiguration`, allowing consumers to define custom completions, highlighting rules, formatting, and key bindings. It targets .NET 10 and uses ANSI escape codes for rendering, making it compatible with any modern terminal. Architecturally, the system is composed of several decoupled layers: a document model (`Document`, `StringBuilderWithCaret`) with grapheme-aware operations; an input system based on `KeyPress` and `KeyPressPattern` for flexible key binding; a rendering pipeline that computes minimal ANSI diffs via `IncrementalRendering` and `Screen`/`Cell` abstractions; and UI panes (`CodePane`, `CompletionPane`, `OverloadPane`) that handle specific interactions. The library employs object pooling (`Pools`) and caching (`StringCache`) to minimize allocations in the per-keystroke hot path. The library is extensively tested with unit tests, integration tests (using recorded keystroke logs), and benchmarks that measure per-keystroke and steady-state rendering costs. It is actively maintained, with regular releases on NuGet and continuous integration across Windows and Ubuntu. Notable external dependencies are `Wcwidth.Sources` (for Unicode display width) and `TextCopy` (for clipboard access); both are wrapped with resilience and fallback behavior.
The LLM identified 5 files worth highlighting for reuse:
Provides two grapheme-aware word-wrapping algorithms: one for editable text (character-wrap, preserving grapheme clusters) and one for display text (word-wrap, breaking at spaces). Both take a string and a maximum width and return an enumerable of wrapped lines. The implementation respects Unicode grapheme clusters (emojis, combining marks) and uses `UnicodeWidth` for display width. It is a self-contained utility that can be extracted into any .NET console or text-rendering project.
Defines a pattern for matching console key presses (with modifiers: Ctrl, Alt, Shift) and an optional character. The `IsMatch` method efficiently compares a `KeyPress` against the pattern. This class decouples key binding definitions from input processing, making it easy to define and customize keyboard shortcuts. It is a small, isolated component reusable in any console application that needs configurable key bindings.
Wraps the `TextCopy` clipboard library with resilience against transient OS failures (e.g., clipboard contention on Linux, missing `xsel`). It logs setup errors and silently swallows runtime failures, ensuring the application remains functional even if clipboard operations fail. This pattern of wrapping a platform-specific dependency with graceful degradation is directly reusable in any .NET console application that needs optional clipboard support.
A model for text with non-overlapping formatting spans (foreground/background color, bold, underline, inverted). The `FormattedString` class holds a plain text string and a sorted list of `FormatSpan` objects. It provides methods to merge overlapping spans and to compute display width. This is a fundamental building block for syntax highlighting and styled output, and can be reused in any .NET application that needs to represent and render formatted text, independent of the console rendering pipeline.
Implements a generic undo/redo system that tracks document state snapshots. It stores a fixed-capacity list of document texts (as strings) and supports undo/redo by indexing into the list. The implementation is simple, efficient (no deep copies of complex objects), and decoupled from the document model. This component can be extracted and applied to any linear state that can be represented as a string, making it a reusable pattern for text-based editors or any application requiring undo/redo.