Rule Widget
Create horizontal dividers and section separators with optional titles
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.
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.
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.
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.
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
See the Box Border Reference for all available border styles.
Styling
Rule Color
Apply colors to emphasize importance or categorize sections by type.
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.
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.
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.
var rule = new Rule()
.RuleTitle("[yellow]Deployment Status[/]")
.RuleStyle(Style.Parse("dim"));
AnsiConsole.Write(rule);
AnsiConsole.WriteLine("Deployment completed at 14:32:15");
See Also
- Box Border Reference - Line styles for rules
- Color Reference - Colors for rule styling
- Panel Widget - Bordered sections with headers
- FigletText Widget - Large decorative text banners
- Building a Rich Console App - Learn Spectre.Console basics
API Reference
A renderable horizontal rule.
Constructors
public Rule()
Initializes a new instance of the Rule class.
public Rule(string title)
Initializes a new instance of the Rule class.
Parameters:
title (string)Properties
Border
: BoxBorderGets or sets the box.
Justification
: Justify?Gets or sets the rule's title justification.
Title
: stringGets or sets the rule title markup text.
Extension Methods
Rule RuleTitle(this Rule rule, string title)Sets the rule title.
Returns:
The same instance so that multiple calls can be chained.