The TextPath widget renders file system paths with smart truncation and granular styling for each path component.
When to Use
Use TextPath when you need to display file system paths with intelligent formatting. Common scenarios:
- Build output: Show source file locations in compiler messages
- File browsers: Display paths that may exceed available width
- Breadcrumb navigation: Render hierarchical location indicators
For plain text without path semantics, use Text or Markup instead. For directory tree structures, use Tree.
Basic Usage
Pass a file path string to the constructor. TextPath normalizes separators automatically.
var path = new TextPath("C:/Users/Phil/Documents/project/src/main.cs");
AnsiConsole.Write(path);
Styling Components
TextPath lets you style four distinct components independently:
- Root: Drive letter (
C:) or Unix root (/) - Separator: Path delimiters (
/) - Stem: Intermediate directories
- Leaf: Final segment (usually the filename)
Colors
Use convenience methods to set foreground colors for each component.
var path = new TextPath("C:/Users/Phil/Documents/project/src/main.cs")
.RootColor(Color.Red)
.SeparatorColor(Color.Grey)
.StemColor(Color.Blue)
.LeafColor(Color.Green);
AnsiConsole.Write(path);
Full Styles
Apply complete styles including background colors, decorations like bold or underline.
var path = new TextPath("/home/user/projects/app/Program.cs")
.RootStyle(new Style(Color.Yellow, decoration: Decoration.Bold))
.SeparatorStyle(new Style(Color.Grey))
.StemStyle(new Style(Color.Blue))
.LeafStyle(new Style(Color.Green, decoration: Decoration.Underline));
AnsiConsole.Write(path);
Alignment
Control text alignment with LeftJustified(), Centered(), or RightJustified().
var leftPath = new TextPath("src/components/Button.tsx")
.LeftJustified()
.LeafColor(Color.Green);
var centerPath = new TextPath("src/components/Button.tsx")
.Centered()
.LeafColor(Color.Green);
var rightPath = new TextPath("src/components/Button.tsx")
.RightJustified()
.LeafColor(Color.Green);
AnsiConsole.MarkupLine("[grey]Left aligned:[/]");
AnsiConsole.Write(leftPath);
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[grey]Center aligned:[/]");
AnsiConsole.Write(centerPath);
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[grey]Right aligned:[/]");
AnsiConsole.Write(rightPath);
Smart Truncation
When a path exceeds available width, TextPath intelligently truncates by:
- Preserving the root (if present)
- Preserving the leaf (filename)
- Replacing middle segments with ellipsis (
…)
This ensures the most important parts—where the file is rooted and what file it is—remain visible.
// This long path will be truncated to fit, preserving root and leaf
var path = new TextPath("C:/Users/Developer/Documents/Projects/MyCompany/Application/src/components/forms/validation/rules/StringValidator.cs")
.RootColor(Color.Yellow)
.StemColor(Color.Blue)
.LeafColor(Color.Green);
AnsiConsole.Write(path);
Cross-Platform Paths
TextPath handles both Windows and Unix path formats, normalizing separators for consistent display.
var path = new TextPath("/var/log/nginx/access.log")
.RootColor(Color.Yellow)
.StemColor(Color.Blue)
.LeafColor(Color.Green);
AnsiConsole.Write(path);
See Also
- Text Widget - Plain styled text
- Markup Widget - Inline markup syntax
- Tree Widget - Directory tree structures
- Color Reference - Colors for path components
- Text Style Reference - Style decorations
API Reference
Representation of a file system path.
Constructors
public TextPath(string path)
Initializes a new instance of the TextPath class.
Parameters:
path (string)Properties
Justification
: Justify?Gets or sets the alignment.
Methods
public Measurement Measure(RenderOptions options, int maxWidth)
Measures the renderable object.
Parameters:
options (RenderOptions)maxWidth (int)Returns:
The minimum and maximum width of the object.
public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
Renders the object.
Parameters:
options (RenderOptions)maxWidth (int)Returns:
A collection of segments.