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.

public static void BasicTextPathExample()
{
    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.

public static void TextPathColorsExample()
{
    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.

public static void TextPathStylesExample()
{
    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().

public static void TextPathAlignmentExample()
{
    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.

public static void TextPathTruncationExample()
{
    // 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.

public static void TextPathUnixExample()
{
    var path = new TextPath("/var/log/nginx/access.log")
        .RootColor(Color.Yellow)
        .StemColor(Color.Blue)
        .LeafColor(Color.Green);
  
    AnsiConsole.Write(path);
}

API Reference

Representation of a file system path.

Constructors

TextPath(string path)

Initializes a new instance of the class.

Parameters:

path (string)
The path to render.

Properties

Justification : Nullable<Justify>

Gets or sets the alignment.

LeafStyle : Style

Gets or sets the leaf style.

RootStyle : Style

Gets or sets the root style.

SeparatorStyle : Style

Gets or sets the separator style.

StemStyle : Style

Gets or sets the stem style.

Methods

Measurement Measure(RenderOptions options, int maxWidth)

Parameters:

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

Parameters:

options (RenderOptions)
maxWidth (int)

Extension Methods

IEnumerable<Segment> GetSegments(IAnsiConsole console)

Gets the segments for a renderable using the specified console.

Parameters:

console (IAnsiConsole)
The console.

Returns:

An enumerable containing segments representing the specified .

TextPath LeafColor(Color color)

Sets the leaf color.

Parameters:

color (Color)
The leaf color.

Returns:

The same instance so that multiple calls can be chained.

TextPath LeafStyle(Style style)

Sets the leaf style.

Parameters:

style (Style)
The stem leaf to set.

Returns:

The same instance so that multiple calls can be chained.

TextPath RootColor(Color color)

Sets the root color.

Parameters:

color (Color)
The root color.

Returns:

The same instance so that multiple calls can be chained.

TextPath RootStyle(Style style)

Sets the root style.

Parameters:

style (Style)
The root style to set.

Returns:

The same instance so that multiple calls can be chained.

TextPath SeparatorColor(Color color)

Sets the separator color.

Parameters:

color (Color)
The separator color.

Returns:

The same instance so that multiple calls can be chained.

TextPath SeparatorStyle(Style style)

Sets the separator style.

Parameters:

style (Style)
The separator style to set.

Returns:

The same instance so that multiple calls can be chained.

TextPath StemColor(Color color)

Sets the stem color.

Parameters:

color (Color)
The stem color.

Returns:

The same instance so that multiple calls can be chained.

TextPath StemStyle(Style style)

Sets the stem style.

Parameters:

style (Style)
The stem style to set.

Returns:

The same instance so that multiple calls can be chained.