Class: Rich::Panel

Inherits:
Object
  • Object
show all
Defined in:
lib/rich/panel.rb

Overview

A bordered panel container for content

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, title: nil, subtitle: nil, box: Box::ROUNDED, border_style: nil, title_style: nil, subtitle_style: nil, expand: true, padding: 1, width: nil, title_align: :center, subtitle_align: :center) ⇒ Panel

Returns a new instance of Panel.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rich/panel.rb', line 48

def initialize(
  content,
  title: nil,
  subtitle: nil,
  box: Box::ROUNDED,
  border_style: nil,
  title_style: nil,
  subtitle_style: nil,
  expand: true,
  padding: 1,
  width: nil,
  title_align: :center,
  subtitle_align: :center
)
  @content = content
  @title = title
  @subtitle = subtitle
  @box = box
  @border_style = border_style.is_a?(String) ? Style.parse(border_style) : border_style
  @title_style = title_style.is_a?(String) ? Style.parse(title_style) : title_style
  @subtitle_style = subtitle_style.is_a?(String) ? Style.parse(subtitle_style) : subtitle_style
  @expand = expand
  @padding = padding
  @width = width
  @title_align = title_align
  @subtitle_align = subtitle_align
end

Instance Attribute Details

#border_styleStyle? (readonly)

Returns Border style.

Returns:

  • (Style, nil)

    Border style



25
26
27
# File 'lib/rich/panel.rb', line 25

def border_style
  @border_style
end

#boxBox (readonly)

Returns Box style.

Returns:

  • (Box)

    Box style



22
23
24
# File 'lib/rich/panel.rb', line 22

def box
  @box
end

#contentObject (readonly)

Returns Content to display.

Returns:

  • (Object)

    Content to display



13
14
15
# File 'lib/rich/panel.rb', line 13

def content
  @content
end

#expandBoolean (readonly)

Returns Expand to fill width.

Returns:

  • (Boolean)

    Expand to fill width



34
35
36
# File 'lib/rich/panel.rb', line 34

def expand
  @expand
end

#paddingInteger (readonly)

Returns Padding inside panel.

Returns:

  • (Integer)

    Padding inside panel



37
38
39
# File 'lib/rich/panel.rb', line 37

def padding
  @padding
end

#subtitleString? (readonly)

Returns Panel subtitle.

Returns:

  • (String, nil)

    Panel subtitle



19
20
21
# File 'lib/rich/panel.rb', line 19

def subtitle
  @subtitle
end

#subtitle_alignSymbol (readonly)

Returns Subtitle alignment.

Returns:

  • (Symbol)

    Subtitle alignment



46
47
48
# File 'lib/rich/panel.rb', line 46

def subtitle_align
  @subtitle_align
end

#subtitle_styleStyle? (readonly)

Returns Subtitle style.

Returns:

  • (Style, nil)

    Subtitle style



31
32
33
# File 'lib/rich/panel.rb', line 31

def subtitle_style
  @subtitle_style
end

#titleString? (readonly)

Returns Panel title.

Returns:

  • (String, nil)

    Panel title



16
17
18
# File 'lib/rich/panel.rb', line 16

def title
  @title
end

#title_alignSymbol (readonly)

Returns Title alignment.

Returns:

  • (Symbol)

    Title alignment



43
44
45
# File 'lib/rich/panel.rb', line 43

def title_align
  @title_align
end

#title_styleStyle? (readonly)

Returns Title style.

Returns:

  • (Style, nil)

    Title style



28
29
30
# File 'lib/rich/panel.rb', line 28

def title_style
  @title_style
end

#widthInteger? (readonly)

Returns Fixed width.

Returns:

  • (Integer, nil)

    Fixed width



40
41
42
# File 'lib/rich/panel.rb', line 40

def width
  @width
end

Class Method Details

.fit(content, title: nil) ⇒ Panel

Create a simple panel

Parameters:

  • content (String)

    Content

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

    Title

Returns:



136
137
138
# File 'lib/rich/panel.rb', line 136

def fit(content, title: nil)
  new(content, title: title, expand: false)
end

Instance Method Details

Print panel to console

Parameters:

  • console (Console)

    Console to print to



125
126
127
128
129
# File 'lib/rich/panel.rb', line 125

def print_to(console)
  rendered = render(max_width: console.width, color_system: console.color_system)
  console.write(rendered)
  console.write("\n")
end

#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String

Render panel to string with ANSI codes

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

  • color_system (Symbol) (defaults to: ColorSystem::TRUECOLOR)

    Color system

Returns:

  • (String)


119
120
121
# File 'lib/rich/panel.rb', line 119

def render(max_width: 80, color_system: ColorSystem::TRUECOLOR)
  Segment.render(to_segments(max_width: max_width), color_system: color_system)
end

#to_segments(max_width: 80) ⇒ Array<Segment>

Render panel to segments

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rich/panel.rb', line 79

def to_segments(max_width: 80)
  segments = []
  inner_width = calculate_inner_width(max_width)
  content_width = [inner_width - @padding * 2, 0].max

  # Render content to lines
  content_lines = render_content(content_width)

  # Top border with title
  segments.concat(render_top_border(inner_width))
  segments << Segment.new("\n")

  # Padding top
  @padding.times do
    segments.concat(render_empty_row(inner_width))
    segments << Segment.new("\n")
  end

  # Content rows
  content_lines.each do |line|
    segments.concat(render_content_row(line, inner_width))
    segments << Segment.new("\n")
  end

  # Padding bottom
  @padding.times do
    segments.concat(render_empty_row(inner_width))
    segments << Segment.new("\n")
  end

  # Bottom border with subtitle
  segments.concat(render_bottom_border(inner_width))

  segments
end