Module: Rich

Defined in:
lib/rich.rb,
lib/rich/box.rb,
lib/rich/json.rb,
lib/rich/text.rb,
lib/rich/tree.rb,
lib/rich/cells.rb,
lib/rich/color.rb,
lib/rich/panel.rb,
lib/rich/style.rb,
lib/rich/table.rb,
lib/rich/layout.rb,
lib/rich/markup.rb,
lib/rich/syntax.rb,
lib/rich/console.rb,
lib/rich/control.rb,
lib/rich/segment.rb,
lib/rich/version.rb,
lib/rich/markdown.rb,
lib/rich/progress.rb,
lib/rich/_palettes.rb,
lib/rich/color_triplet.rb,
lib/rich/win32_console.rb,
lib/rich/terminal_theme.rb

Defined Under Namespace

Modules: Cells, ColorSystem, ColorType, Control, JSON, Markup, Palettes, Pretty, ProgressStyle, StyleAttribute, TreeGuide, Win32Console Classes: BaseLexer, BashLexer, Box, Color, ColorParseError, ColorTriplet, Column, Columns, Console, ConsoleOptions, JSONLexer, JavaScriptLexer, Live, Markdown, MarkupError, Panel, Progress, ProgressBar, ProgressTask, PythonLexer, RubyLexer, SQLLexer, Segment, Span, Spinner, Status, Style, Syntax, Table, TerminalTheme, Text, TextLexer, Tree, TreeNode, YAMLLexer

Constant Summary collapse

ANSI_COLOR_NAMES =

ANSI color name to number mapping

{
  "black" => 0,
  "red" => 1,
  "green" => 2,
  "yellow" => 3,
  "blue" => 4,
  "magenta" => 5,
  "cyan" => 6,
  "white" => 7,
  "bright_black" => 8,
  "bright_red" => 9,
  "bright_green" => 10,
  "bright_yellow" => 11,
  "bright_blue" => 12,
  "bright_magenta" => 13,
  "bright_cyan" => 14,
  "bright_white" => 15,
  "grey0" => 16,
  "gray0" => 16,
  "navy_blue" => 17,
  "dark_blue" => 18,
  "blue3" => 20,
  "blue1" => 21,
  "dark_green" => 22,
  "deep_sky_blue4" => 25,
  "dodger_blue3" => 26,
  "dodger_blue2" => 27,
  "green4" => 28,
  "spring_green4" => 29,
  "turquoise4" => 30,
  "deep_sky_blue3" => 32,
  "dodger_blue1" => 33,
  "green3" => 40,
  "spring_green3" => 41,
  "dark_cyan" => 36,
  "light_sea_green" => 37,
  "deep_sky_blue2" => 38,
  "deep_sky_blue1" => 39,
  "spring_green2" => 47,
  "cyan3" => 43,
  "dark_turquoise" => 44,
  "turquoise2" => 45,
  "green1" => 46,
  "spring_green1" => 48,
  "medium_spring_green" => 49,
  "cyan2" => 50,
  "cyan1" => 51,
  "dark_red" => 88,
  "deep_pink4" => 125,
  "purple4" => 55,
  "purple3" => 56,
  "blue_violet" => 57,
  "orange4" => 94,
  "grey37" => 59,
  "gray37" => 59,
  "medium_purple4" => 60,
  "slate_blue3" => 62,
  "royal_blue1" => 63,
  "chartreuse4" => 64,
  "dark_sea_green4" => 71,
  "pale_turquoise4" => 66,
  "steel_blue" => 67,
  "steel_blue3" => 68,
  "cornflower_blue" => 69,
  "chartreuse3" => 76,
  "cadet_blue" => 73,
  "sky_blue3" => 74,
  "steel_blue1" => 81,
  "pale_green3" => 114,
  "sea_green3" => 78,
  "aquamarine3" => 79,
  "medium_turquoise" => 80,
  "chartreuse2" => 112,
  "sea_green2" => 83,
  "sea_green1" => 85,
  "aquamarine1" => 122,
  "dark_slate_gray2" => 87,
  "dark_magenta" => 91,
  "dark_violet" => 128,
  "purple" => 129,
  "light_pink4" => 95,
  "plum4" => 96,
  "medium_purple3" => 98,
  "slate_blue1" => 99,
  "yellow4" => 106,
  "wheat4" => 101,
  "grey53" => 102,
  "gray53" => 102,
  "light_slate_grey" => 103,
  "light_slate_gray" => 103,
  "medium_purple" => 104,
  "light_slate_blue" => 105,
  "dark_olive_green3" => 149,
  "dark_sea_green" => 108,
  "light_sky_blue3" => 110,
  "sky_blue2" => 111,
  "dark_sea_green3" => 150,
  "dark_slate_gray3" => 116,
  "sky_blue1" => 117,
  "chartreuse1" => 118,
  "light_green" => 120,
  "pale_green1" => 156,
  "dark_slate_gray1" => 123,
  "red3" => 160,
  "medium_violet_red" => 126,
  "magenta3" => 164,
  "dark_orange3" => 166,
  "indian_red" => 167,
  "hot_pink3" => 168,
  "medium_orchid3" => 133,
  "medium_orchid" => 134,
  "medium_purple2" => 140,
  "dark_goldenrod" => 136,
  "light_salmon3" => 173,
  "rosy_brown" => 138,
  "grey63" => 139,
  "gray63" => 139,
  "medium_purple1" => 141,
  "gold3" => 178,
  "dark_khaki" => 143,
  "navajo_white3" => 144,
  "grey69" => 145,
  "gray69" => 145,
  "light_steel_blue3" => 146,
  "light_steel_blue" => 147,
  "yellow3" => 184,
  "dark_sea_green2" => 157,
  "light_cyan3" => 152,
  "light_sky_blue1" => 153,
  "green_yellow" => 154,
  "dark_olive_green2" => 155,
  "dark_sea_green1" => 193,
  "pale_turquoise1" => 159,
  "deep_pink3" => 162,
  "magenta2" => 200,
  "hot_pink2" => 169,
  "orchid" => 170,
  "medium_orchid1" => 207,
  "orange3" => 172,
  "light_pink3" => 174,
  "pink3" => 175,
  "plum3" => 176,
  "violet" => 177,
  "light_goldenrod3" => 179,
  "tan" => 180,
  "misty_rose3" => 181,
  "thistle3" => 182,
  "plum2" => 183,
  "khaki3" => 185,
  "light_goldenrod2" => 222,
  "light_yellow3" => 187,
  "grey84" => 188,
  "gray84" => 188,
  "light_steel_blue1" => 189,
  "yellow2" => 190,
  "dark_olive_green1" => 192,
  "honeydew2" => 194,
  "light_cyan1" => 195,
  "red1" => 196,
  "deep_pink2" => 197,
  "deep_pink1" => 199,
  "magenta1" => 201,
  "orange_red1" => 202,
  "indian_red1" => 204,
  "hot_pink" => 206,
  "dark_orange" => 208,
  "salmon1" => 209,
  "light_coral" => 210,
  "pale_violet_red1" => 211,
  "orchid2" => 212,
  "orchid1" => 213,
  "orange1" => 214,
  "sandy_brown" => 215,
  "light_salmon1" => 216,
  "light_pink1" => 217,
  "pink1" => 218,
  "plum1" => 219,
  "gold1" => 220,
  "navajo_white1" => 223,
  "misty_rose1" => 224,
  "thistle1" => 225,
  "yellow1" => 226,
  "light_goldenrod1" => 227,
  "khaki1" => 228,
  "wheat1" => 229,
  "cornsilk1" => 230,
  "grey100" => 231,
  "gray100" => 231,
  "grey3" => 232,
  "gray3" => 232,
  "grey7" => 233,
  "gray7" => 233,
  "grey11" => 234,
  "gray11" => 234,
  "grey15" => 235,
  "gray15" => 235,
  "grey19" => 236,
  "gray19" => 236,
  "grey23" => 237,
  "gray23" => 237,
  "grey27" => 238,
  "gray27" => 238,
  "grey30" => 239,
  "gray30" => 239,
  "grey35" => 240,
  "gray35" => 240,
  "grey39" => 241,
  "gray39" => 241,
  "grey42" => 242,
  "gray42" => 242,
  "grey46" => 243,
  "gray46" => 243,
  "grey50" => 244,
  "gray50" => 244,
  "grey54" => 245,
  "gray54" => 245,
  "grey58" => 246,
  "gray58" => 246,
  "grey62" => 247,
  "gray62" => 247,
  "grey66" => 248,
  "gray66" => 248,
  "grey70" => 249,
  "gray70" => 249,
  "grey74" => 250,
  "gray74" => 250,
  "grey78" => 251,
  "gray78" => 251,
  "grey82" => 252,
  "gray82" => 252,
  "grey85" => 253,
  "gray85" => 253,
  "grey89" => 254,
  "gray89" => 254,
  "grey93" => 255,
  "gray93" => 255
}.freeze
COLOR_NUMBER_TO_NAME =

Reverse mapping from number to canonical name

ANSI_COLOR_NAMES.each_with_object({}) do |(name, number), hash|
  hash[number] ||= name
end.freeze
LEXERS =

Lexer registry

{
  "ruby" => RubyLexer.new,
  "python" => PythonLexer.new,
  "javascript" => JavaScriptLexer.new,
  "js" => JavaScriptLexer.new,
  "typescript" => JavaScriptLexer.new,
  "ts" => JavaScriptLexer.new,
  "sql" => SQLLexer.new,
  "json" => JSONLexer.new,
  "yaml" => YAMLLexer.new,
  "yml" => YAMLLexer.new,
  "bash" => BashLexer.new,
  "shell" => BashLexer.new,
  "sh" => BashLexer.new,
  "text" => TextLexer.new,
  "txt" => TextLexer.new
}.freeze
VERSION =
"1.0.2"
DEFAULT_TERMINAL_THEME =

Default terminal theme (based on typical dark terminal)

TerminalTheme.new(
  foreground: ColorTriplet.new(230, 230, 230),
  background: ColorTriplet.new(12, 12, 12),
  ansi_colors: [
    ColorTriplet.new(12, 12, 12),     # 0: Black
    ColorTriplet.new(205, 49, 49),    # 1: Red
    ColorTriplet.new(13, 188, 121),   # 2: Green
    ColorTriplet.new(229, 229, 16),   # 3: Yellow
    ColorTriplet.new(36, 114, 200),   # 4: Blue
    ColorTriplet.new(188, 63, 188),   # 5: Magenta
    ColorTriplet.new(17, 168, 205),   # 6: Cyan
    ColorTriplet.new(229, 229, 229),  # 7: White
    ColorTriplet.new(102, 102, 102),  # 8: Bright Black
    ColorTriplet.new(241, 76, 76),    # 9: Bright Red
    ColorTriplet.new(35, 209, 139),   # 10: Bright Green
    ColorTriplet.new(245, 245, 67),   # 11: Bright Yellow
    ColorTriplet.new(59, 142, 234),   # 12: Bright Blue
    ColorTriplet.new(214, 112, 214),  # 13: Bright Magenta
    ColorTriplet.new(41, 184, 219),   # 14: Bright Cyan
    ColorTriplet.new(255, 255, 255)   # 15: Bright White
  ]
)
MONOKAI_THEME =

Monokai-inspired theme

TerminalTheme.new(
  foreground: ColorTriplet.new(248, 248, 242),
  background: ColorTriplet.new(39, 40, 34),
  ansi_colors: [
    ColorTriplet.new(39, 40, 34),     # 0: Black
    ColorTriplet.new(249, 38, 114),   # 1: Red
    ColorTriplet.new(166, 226, 46),   # 2: Green
    ColorTriplet.new(244, 191, 117),  # 3: Yellow
    ColorTriplet.new(102, 217, 239),  # 4: Blue
    ColorTriplet.new(174, 129, 255),  # 5: Magenta
    ColorTriplet.new(161, 239, 228),  # 6: Cyan
    ColorTriplet.new(248, 248, 242),  # 7: White
    ColorTriplet.new(117, 113, 94),   # 8: Bright Black
    ColorTriplet.new(249, 38, 114),   # 9: Bright Red
    ColorTriplet.new(166, 226, 46),   # 10: Bright Green
    ColorTriplet.new(244, 191, 117),  # 11: Bright Yellow
    ColorTriplet.new(102, 217, 239),  # 12: Bright Blue
    ColorTriplet.new(174, 129, 255),  # 13: Bright Magenta
    ColorTriplet.new(161, 239, 228),  # 14: Bright Cyan
    ColorTriplet.new(248, 248, 242)   # 15: Bright White
  ]
)
SVG_EXPORT_THEME =

Dimmed Monokai for SVG/HTML export

TerminalTheme.new(
  foreground: ColorTriplet.new(248, 248, 242),
  background: ColorTriplet.new(50, 48, 47),
  ansi_colors: [
    ColorTriplet.new(50, 48, 47),     # 0: Black
    ColorTriplet.new(255, 98, 134),   # 1: Red
    ColorTriplet.new(164, 238, 92),   # 2: Green
    ColorTriplet.new(255, 216, 102),  # 3: Yellow
    ColorTriplet.new(98, 209, 255),   # 4: Blue
    ColorTriplet.new(189, 147, 249),  # 5: Magenta
    ColorTriplet.new(128, 255, 234),  # 6: Cyan
    ColorTriplet.new(248, 248, 242),  # 7: White
    ColorTriplet.new(98, 94, 76),     # 8: Bright Black
    ColorTriplet.new(255, 98, 134),   # 9: Bright Red
    ColorTriplet.new(164, 238, 92),   # 10: Bright Green
    ColorTriplet.new(255, 216, 102),  # 11: Bright Yellow
    ColorTriplet.new(98, 209, 255),   # 12: Bright Blue
    ColorTriplet.new(189, 147, 249),  # 13: Bright Magenta
    ColorTriplet.new(128, 255, 234),  # 14: Bright Cyan
    ColorTriplet.new(248, 248, 242)   # 15: Bright White
  ]
)
WINDOWS_TERMINAL_THEME =

Windows Terminal default theme

TerminalTheme.new(
  foreground: ColorTriplet.new(204, 204, 204),
  background: ColorTriplet.new(12, 12, 12),
  ansi_colors: Palettes::WINDOWS_PALETTE.dup
)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.consoleConsole?

Global console instance

Returns:



52
53
54
# File 'lib/rich.rb', line 52

def console
  @console
end

Class Method Details

.get_consoleConsole

Get or create the global console

Returns:



56
57
58
# File 'lib/rich.rb', line 56

def get_console
  @console ||= Console.new
end

.inspect(obj, title: nil, methods: false, docs: true) ⇒ void

This method returns an undefined value.

Inspect an object and print its details

Parameters:

  • obj (Object)

    Object to inspect

  • title (String, nil) (defaults to: nil)

    Title

  • methods (Boolean) (defaults to: false)

    Show methods

  • docs (Boolean) (defaults to: true)

    Show documentation



93
94
95
# File 'lib/rich.rb', line 93

def inspect(obj, title: nil, methods: false, docs: true)
  get_console.inspect(obj, title: title, methods: methods, docs: docs)
end

This method returns an undefined value.

Print to the console with markup support

Parameters:

  • objects (Array)

    Objects to print

  • sep (String) (defaults to: " ")

    Separator between objects

  • end_str (String) (defaults to: "\n")

    End of line string

  • style (String, Style, nil) (defaults to: nil)

    Style to apply

  • highlight (Boolean) (defaults to: true)

    Enable highlighting



74
75
76
# File 'lib/rich.rb', line 74

def print(*objects, sep: " ", end_str: "\n", style: nil, highlight: true)
  get_console.print(*objects, sep: sep, end_str: end_str, style: style, highlight: highlight)
end

This method returns an undefined value.

Print JSON with syntax highlighting

Parameters:

  • json (String, nil) (defaults to: nil)

    JSON string

  • data (Object) (defaults to: nil)

    Object to convert to JSON

  • indent (Integer) (defaults to: 2)

    Indentation level



83
84
85
# File 'lib/rich.rb', line 83

def print_json(json = nil, data: nil, indent: 2)
  get_console.print_json(json, data: data, indent: indent)
end

.reconfigure(**kwargs) ⇒ void

This method returns an undefined value.

Reconfigure the global console

Parameters:

  • kwargs (Hash)

    Console options



63
64
65
# File 'lib/rich.rb', line 63

def reconfigure(**kwargs)
  @console = Console.new(**kwargs)
end

.rule(title = "", style: "rule.line") ⇒ void

This method returns an undefined value.

Create a rule/separator line

Parameters:

  • title (String) (defaults to: "")

    Title text

  • style (String) (defaults to: "rule.line")

    Style for the rule



101
102
103
# File 'lib/rich.rb', line 101

def rule(title = "", style: "rule.line")
  get_console.rule(title, style: style)
end