The Rule widget renders horizontal lines across the console width to visually separate content.
When to Use
Use Rule when you need to visually separate sections of output. Common scenarios:
- Section headers: Mark the beginning of logical sections in reports or logs
- Content dividers: Separate distinct blocks of information for better readability
- Visual hierarchy: Create clear boundaries between different output types
For emphasized section headers with decorative borders, use Panel instead. For large decorative text banners, use FigletText.
Basic Usage
Create a simple horizontal divider that spans the full console width.
public static void BasicRuleExample()
{
AnsiConsole.WriteLine("Section content above");
AnsiConsole.Write(new Rule());
AnsiConsole.WriteLine("Section content below");
}
Adding Titles
Use titles to identify what comes after the rule, turning it into a section header.
public static void RuleTitleExample()
{
AnsiConsole.Write(new Rule("[blue]Configuration[/]"));
AnsiConsole.WriteLine("App settings and environment variables");
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[green]Results[/]"));
AnsiConsole.WriteLine("Test execution completed successfully");
}
Title Alignment
Position titles on the left, center, or right to match your layout needs.
public static void RuleTitleAlignmentExample()
{
AnsiConsole.Write(new Rule("[yellow]Left Aligned[/]")
{
Justification = Justify.Left
});
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[yellow]Center Aligned (default)[/]")
{
Justification = Justify.Center
});
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[yellow]Right Aligned[/]")
{
Justification = Justify.Right
});
}
Border Styles
Choose a line style to match your application's visual tone or to indicate different section types.
public static void RuleBorderStylesExample()
{
AnsiConsole.Write(new Rule("Single Line (default)")
{
Border = BoxBorder.Square
});
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Double Line")
{
Border = BoxBorder.Double
});
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Heavy Line")
{
Border = BoxBorder.Heavy
});
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Rounded")
{
Border = BoxBorder.Rounded
});
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("ASCII")
{
Border = BoxBorder.Ascii
});
}
Note
Styling
Rule Color
Apply colors to emphasize importance or categorize sections by type.
public static void RuleColorExample()
{
AnsiConsole.Write(new Rule("[red]Error Section[/]")
{
Style = Style.Parse("red")
});
AnsiConsole.WriteLine("Error details go here");
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[green]Success Section[/]")
{
Style = Style.Parse("green")
});
AnsiConsole.WriteLine("Success details go here");
}
Subtle Separators
Use dim or muted colors for subtle dividers that don't distract from content.
public static void RuleSubtleSeparatorExample()
{
AnsiConsole.WriteLine("Log entry 1: Application started");
AnsiConsole.Write(new Rule { Style = Style.Parse("grey dim") });
AnsiConsole.WriteLine("Log entry 2: Configuration loaded");
AnsiConsole.Write(new Rule { Style = Style.Parse("grey dim") });
AnsiConsole.WriteLine("Log entry 3: Database connected");
}
Common Patterns
Section Dividers in Reports
Use rules to organize multi-section output like system reports or status dashboards.
public static void RuleSectionDividersExample()
{
AnsiConsole.Write(new Rule("[bold blue]System Information[/]"));
AnsiConsole.WriteLine("OS: Windows 11");
AnsiConsole.WriteLine("Memory: 16 GB");
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[bold blue]Performance Metrics[/]"));
AnsiConsole.WriteLine("CPU Usage: 45%");
AnsiConsole.WriteLine("Disk I/O: 120 MB/s");
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[bold blue]Network Status[/]"));
AnsiConsole.WriteLine("Connected: Yes");
AnsiConsole.WriteLine("Latency: 12ms");
}
Fluent Configuration
Combine extension methods for concise rule creation.
public static void RuleFluentExample()
{
var rule = new Rule()
.RuleTitle("[yellow]Deployment Status[/]")
.RuleStyle(Style.Parse("dim"));
AnsiConsole.Write(rule);
AnsiConsole.WriteLine("Deployment completed at 14:32:15");
}
API Reference
A renderable horizontal rule.
Constructors
Rule()Initializes a new instance of the class.
Rule(string title)Initializes a new instance of the class.
Parameters:
title (string)Properties
Border
: BoxBorderJustification
: Nullable<Justify>Gets or sets the rule's title justification.
Style
: StyleGets or sets the rule style.
Title
: stringGets or sets the rule title markup text.
Extension Methods
IEnumerable<Segment> GetSegments(IAnsiConsole console)Gets the segments for a renderable using the specified console.
Parameters:
console (IAnsiConsole)Returns:
An enumerable containing segments representing the specified .
Rule RuleStyle(Style style)Sets the rule style.
Parameters:
style (Style)Returns:
The same instance so that multiple calls can be chained.
Rule RuleTitle(string title)Sets the rule title.
Parameters:
title (string)Returns:
The same instance so that multiple calls can be chained.