Class: Rich::Box
- Inherits:
-
Object
- Object
- Rich::Box
- Defined in:
- lib/rich/box.rb
Overview
Box drawing character sets for borders and tables
Constant Summary collapse
- ASCII =
ASCII box (works everywhere)
new( top_left: "+", top_right: "+", bottom_left: "+", bottom_right: "+", horizontal: "-", vertical: "|", left_t: "+", right_t: "+", top_t: "+", bottom_t: "+", cross: "+", thick_horizontal: "=", thick_left_t: "+", thick_right_t: "+", thick_cross: "+" )
- SQUARE =
Standard Unicode box
new( top_left: "┌", top_right: "┐", bottom_left: "└", bottom_right: "┘", horizontal: "─", vertical: "│", left_t: "├", right_t: "┤", top_t: "┬", bottom_t: "┴", cross: "┼", thick_horizontal: "━", thick_left_t: "┝", thick_right_t: "┥", thick_cross: "┿" )
- ROUNDED =
Rounded corners
new( top_left: "╭", top_right: "╮", bottom_left: "╰", bottom_right: "╯", horizontal: "─", vertical: "│", left_t: "├", right_t: "┤", top_t: "┬", bottom_t: "┴", cross: "┼", thick_horizontal: "━", thick_left_t: "┝", thick_right_t: "┥", thick_cross: "┿" )
- HEAVY =
Heavy/thick box
new( top_left: "┏", top_right: "┓", bottom_left: "┗", bottom_right: "┛", horizontal: "━", vertical: "┃", left_t: "┣", right_t: "┫", top_t: "┳", bottom_t: "┻", cross: "╋", thick_horizontal: "━", thick_left_t: "┣", thick_right_t: "┫", thick_cross: "╋" )
- DOUBLE =
Double line box
new( top_left: "╔", top_right: "╗", bottom_left: "╚", bottom_right: "╝", horizontal: "═", vertical: "║", left_t: "╠", right_t: "╣", top_t: "╦", bottom_t: "╩", cross: "╬", thick_horizontal: "═", thick_left_t: "╠", thick_right_t: "╣", thick_cross: "╬" )
- MINIMAL =
Minimal (dashes, no corners)
new( top_left: " ", top_right: " ", bottom_left: " ", bottom_right: " ", horizontal: "─", vertical: " ", left_t: " ", right_t: " ", top_t: "─", bottom_t: "─", cross: "─" )
- SIMPLE =
Simple (just horizontal lines)
new( top_left: "", top_right: "", bottom_left: "", bottom_right: "", horizontal: "─", vertical: "", left_t: "", right_t: "", top_t: "", bottom_t: "", cross: "" )
- NONE =
No border at all
new( top_left: "", top_right: "", bottom_left: "", bottom_right: "", horizontal: "", vertical: "", left_t: "", right_t: "", top_t: "", bottom_t: "", cross: "" )
Instance Attribute Summary collapse
-
#bottom_left ⇒ String
readonly
Bottom-left corner.
-
#bottom_right ⇒ String
readonly
Bottom-right corner.
-
#bottom_t ⇒ String
readonly
Bottom T-junction.
-
#cross ⇒ String
readonly
Cross/plus junction.
-
#horizontal ⇒ String
readonly
Horizontal line.
-
#left_t ⇒ String
readonly
Left T-junction.
-
#right_t ⇒ String
readonly
Right T-junction.
-
#thick_cross ⇒ String
readonly
Thick cross.
-
#thick_horizontal ⇒ String
readonly
Thick horizontal (for headers).
-
#thick_left_t ⇒ String
readonly
Thick left T-junction.
-
#thick_right_t ⇒ String
readonly
Thick right T-junction.
-
#top_left ⇒ String
readonly
Top-left corner.
-
#top_right ⇒ String
readonly
Top-right corner.
-
#top_t ⇒ String
readonly
Top T-junction.
-
#vertical ⇒ String
readonly
Vertical line.
Class Method Summary collapse
-
.ascii ⇒ Object
ASCII characters only.
-
.double ⇒ Object
Double lines.
-
.heavy ⇒ Object
Heavy/thick lines.
-
.minimal ⇒ Object
Minimal (no corners).
-
.none ⇒ Object
No border.
-
.rounded ⇒ Object
Rounded corners.
-
.simple ⇒ Object
Simple horizontal lines only.
-
.square ⇒ Object
Standard Unicode box drawing.
Instance Method Summary collapse
-
#ascii? ⇒ Boolean
Check if this is the ASCII box.
-
#bottom_edge(width) ⇒ String
(also: #bottom)
Get the bottom edge.
-
#content_row(content, width, align: :left) ⇒ String
Get a content row.
-
#header_separator(width) ⇒ String
Get header separator (thicker line).
-
#initialize(top_left:, top_right:, bottom_left:, bottom_right:, horizontal:, vertical:, left_t: nil, right_t: nil, top_t: nil, bottom_t: nil, cross: nil, thick_horizontal: nil, thick_left_t: nil, thick_right_t: nil, thick_cross: nil) ⇒ Box
constructor
A new instance of Box.
-
#row(width_or_cells, widths = nil) ⇒ String
Get the row separator.
-
#to_ascii ⇒ Box
Substitute ASCII characters for box characters.
-
#top_edge(width) ⇒ String
(also: #top)
Get the top edge.
Constructor Details
#initialize(top_left:, top_right:, bottom_left:, bottom_right:, horizontal:, vertical:, left_t: nil, right_t: nil, top_t: nil, bottom_t: nil, cross: nil, thick_horizontal: nil, thick_left_t: nil, thick_right_t: nil, thick_cross: nil) ⇒ Box
Returns a new instance of Box.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rich/box.rb', line 51 def initialize( top_left:, top_right:, bottom_left:, bottom_right:, horizontal:, vertical:, left_t: nil, right_t: nil, top_t: nil, bottom_t: nil, cross: nil, thick_horizontal: nil, thick_left_t: nil, thick_right_t: nil, thick_cross: nil ) @top_left = top_left @top_right = top_right @bottom_left = bottom_left @bottom_right = bottom_right @horizontal = horizontal @vertical = vertical @left_t = left_t || vertical @right_t = right_t || vertical @top_t = top_t || horizontal @bottom_t = bottom_t || horizontal @cross = cross || "+" @thick_horizontal = thick_horizontal || horizontal @thick_left_t = thick_left_t || @left_t @thick_right_t = thick_right_t || @right_t @thick_cross = thick_cross || @cross freeze end |
Instance Attribute Details
#bottom_left ⇒ String (readonly)
Returns Bottom-left corner.
13 14 15 |
# File 'lib/rich/box.rb', line 13 def bottom_left @bottom_left end |
#bottom_right ⇒ String (readonly)
Returns Bottom-right corner.
16 17 18 |
# File 'lib/rich/box.rb', line 16 def bottom_right @bottom_right end |
#bottom_t ⇒ String (readonly)
Returns Bottom T-junction.
34 35 36 |
# File 'lib/rich/box.rb', line 34 def bottom_t @bottom_t end |
#cross ⇒ String (readonly)
Returns Cross/plus junction.
37 38 39 |
# File 'lib/rich/box.rb', line 37 def cross @cross end |
#horizontal ⇒ String (readonly)
Returns Horizontal line.
19 20 21 |
# File 'lib/rich/box.rb', line 19 def horizontal @horizontal end |
#left_t ⇒ String (readonly)
Returns Left T-junction.
25 26 27 |
# File 'lib/rich/box.rb', line 25 def left_t @left_t end |
#right_t ⇒ String (readonly)
Returns Right T-junction.
28 29 30 |
# File 'lib/rich/box.rb', line 28 def right_t @right_t end |
#thick_cross ⇒ String (readonly)
Returns Thick cross.
49 50 51 |
# File 'lib/rich/box.rb', line 49 def thick_cross @thick_cross end |
#thick_horizontal ⇒ String (readonly)
Returns Thick horizontal (for headers).
40 41 42 |
# File 'lib/rich/box.rb', line 40 def thick_horizontal @thick_horizontal end |
#thick_left_t ⇒ String (readonly)
Returns Thick left T-junction.
43 44 45 |
# File 'lib/rich/box.rb', line 43 def thick_left_t @thick_left_t end |
#thick_right_t ⇒ String (readonly)
Returns Thick right T-junction.
46 47 48 |
# File 'lib/rich/box.rb', line 46 def thick_right_t @thick_right_t end |
#top_left ⇒ String (readonly)
Returns Top-left corner.
7 8 9 |
# File 'lib/rich/box.rb', line 7 def top_left @top_left end |
#top_right ⇒ String (readonly)
Returns Top-right corner.
10 11 12 |
# File 'lib/rich/box.rb', line 10 def top_right @top_right end |
#top_t ⇒ String (readonly)
Returns Top T-junction.
31 32 33 |
# File 'lib/rich/box.rb', line 31 def top_t @top_t end |
#vertical ⇒ String (readonly)
Returns Vertical line.
22 23 24 |
# File 'lib/rich/box.rb', line 22 def vertical @vertical end |
Class Method Details
.ascii ⇒ Object
ASCII characters only
162 163 164 |
# File 'lib/rich/box.rb', line 162 def ascii ASCII end |
.heavy ⇒ Object
Heavy/thick lines
177 178 179 |
# File 'lib/rich/box.rb', line 177 def heavy HEAVY end |
.minimal ⇒ Object
Minimal (no corners)
187 188 189 |
# File 'lib/rich/box.rb', line 187 def minimal MINIMAL end |
.rounded ⇒ Object
Rounded corners
172 173 174 |
# File 'lib/rich/box.rb', line 172 def rounded ROUNDED end |
.simple ⇒ Object
Simple horizontal lines only
192 193 194 |
# File 'lib/rich/box.rb', line 192 def simple SIMPLE end |
.square ⇒ Object
Standard Unicode box drawing
167 168 169 |
# File 'lib/rich/box.rb', line 167 def square SQUARE end |
Instance Method Details
#ascii? ⇒ Boolean
Check if this is the ASCII box
155 156 157 |
# File 'lib/rich/box.rb', line 155 def ascii? self == ASCII end |
#bottom_edge(width) ⇒ String Also known as: bottom
Get the bottom edge
96 97 98 |
# File 'lib/rich/box.rb', line 96 def bottom_edge(width) "#{@bottom_left}#{@horizontal * [0, width - 2].max}#{@bottom_right}" end |
#content_row(content, width, align: :left) ⇒ String
Get a content row
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rich/box.rb', line 124 def content_row(content, width, align: :left) content_len = Cells.cell_len(content) padding = width - content_len case align when :center left_pad = padding / 2 right_pad = padding - left_pad "#{@vertical}#{' ' * left_pad}#{content}#{' ' * right_pad}#{@vertical}" when :right "#{@vertical}#{' ' * padding}#{content}#{@vertical}" else # :left "#{@vertical}#{content}#{' ' * padding}#{@vertical}" end end |
#header_separator(width) ⇒ String
Get header separator (thicker line)
143 144 145 |
# File 'lib/rich/box.rb', line 143 def header_separator(width) "#{@thick_left_t}#{@thick_horizontal * width}#{@thick_right_t}" end |
#row(width_or_cells, widths = nil) ⇒ String
Get the row separator
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rich/box.rb', line 104 def row(width_or_cells, widths = nil) if widths # Table row separator with multiple columns parts = widths.map { |w| @horizontal * w } "#{@left_t}#{parts.join(@cross)}#{@right_t}" else # Single column separator width = width_or_cells.is_a?(Integer) ? width_or_cells : Cells.cell_len(width_or_cells.to_s) "#{@left_t}#{@horizontal * [0, width - 2].max}#{@right_t}" end end |
#to_ascii ⇒ Box
Substitute ASCII characters for box characters
149 150 151 |
# File 'lib/rich/box.rb', line 149 def to_ascii ASCII end |
#top_edge(width) ⇒ String Also known as: top
Get the top edge
89 90 91 |
# File 'lib/rich/box.rb', line 89 def top_edge(width) "#{@top_left}#{@horizontal * [0, width - 2].max}#{@top_right}" end |