TextPath Widget

Display file paths with intelligent truncation and component styling

The TextPath widget renders file system paths with smart truncation and granular styling for each path component.

Screenshot

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.

csharp
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.

csharp
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.

csharp
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().

csharp
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:

  1. Preserving the root (if present)
  2. Preserving the leaf (filename)
  3. Replacing middle segments with ellipsis ()

This ensures the most important parts—where the file is rooted and what file it is—remain visible.

csharp
// 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.

csharp
var path = new TextPath("/var/log/nginx/access.log")
    .RootColor(Color.Yellow)
    .StemColor(Color.Blue)
    .LeafColor(Color.Green);
  
AnsiConsole.Write(path);

See Also

API Reference

Representation of a file system path.

Constructors

public TextPath(string path)

Initializes a new instance of the TextPath class.

Parameters:

path (string)
The path to render.

Properties

Justification : Justify?

Gets or sets the alignment.

Methods

public Measurement Measure(RenderOptions options, int maxWidth)

Measures the renderable object.

Parameters:

options (RenderOptions)
The render options.
maxWidth (int)
The maximum allowed width.

Returns:

The minimum and maximum width of the object.

public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)

Renders the object.

Parameters:

options (RenderOptions)
The render options.
maxWidth (int)
The maximum allowed width.

Returns:

A collection of segments.