Skip to content

GradientRule

Rule

The 'Rule` class is based off of the rich.rule.Rule class and is used to define a rule in gradient color and variable thickness.

Usage

from rich.console import Console
from rich_gradient.rule import Rule

console = Console()
console.print(
    Rule(
        "Hello, world!",
    )
)

Rule

Alignment

The Rule class supports the same alignment options as the Rule class.

Left Aligned Rule

console.print(
    Rule(
        "← This is a left aligned rule.",
        align="left",
    )
)

Left Aligned Rule Example

Right Aligned Rule

console.print(
    Rule(
        "This is a right aligned rule. →,
        align="right"
    )
)

Rich Aligned Rule Example

Center Aligned Rule

console.print(
    Rule(
        "→ This is a center aligned rule. ←",
        align="center"
    )
)

Center aligned rule example

Note

The default behavior of rich_gradient.rule.Rule is center aligned.

Thickness

The Rule class add the ability to determine the thickness of the rule. Valid values are:

  1. thin ()
  2. double ()
  3. medium () (default)
  4. thick ()

Thin Rule

console.print(
    Rule(
        "This is a thin rule",
        thickness=1
    )
)

Thin Rule Thickness

Double-line Rule

console.print(
    Rule(
        "This is a double-line rule",
        thickness=2
    )
)

Double-lined Rule Thickness

Medium Rule

console.print(
    Rule(
        "This is a medium rule",
        thickness=3
    )
)

Medium Rule Thickness

Thick Rule

console.print(
    Rule(
        "This is a thick rule",
        thickness=4
    )
)

Thick Rule Thickness

Custom Colored Rule

To create a rule with custom colors, simply supply the colors parameter with a list of colors (as CSS color names, 3 or 6 digit hex codes, rich color names, or rgb color codes).

console.print(
  Rule(
    "This rule has custom colors!",
    colors=[
      "#f00", # red
      "#f90", # orange
      "#ff0", # yellow
      "#9f0", # yellow-green
    ]
  )
)


Custom Color

Custom Title Style

If you would like some distinction between the rule and it's title, simply pass a rich style to the title_style parameter:

console.print(
     Rule(
         "Custom Styled Rule Title",
         title_style="bold white",
         align="center"
     )
)

Rule Custom Title Style Example