Class: Rich::Box

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_leftString (readonly)

Returns Bottom-left corner.

Returns:

  • (String)

    Bottom-left corner



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

def bottom_left
  @bottom_left
end

#bottom_rightString (readonly)

Returns Bottom-right corner.

Returns:

  • (String)

    Bottom-right corner



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

def bottom_right
  @bottom_right
end

#bottom_tString (readonly)

Returns Bottom T-junction.

Returns:

  • (String)

    Bottom T-junction



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

def bottom_t
  @bottom_t
end

#crossString (readonly)

Returns Cross/plus junction.

Returns:

  • (String)

    Cross/plus junction



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

def cross
  @cross
end

#horizontalString (readonly)

Returns Horizontal line.

Returns:

  • (String)

    Horizontal line



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

def horizontal
  @horizontal
end

#left_tString (readonly)

Returns Left T-junction.

Returns:

  • (String)

    Left T-junction



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

def left_t
  @left_t
end

#right_tString (readonly)

Returns Right T-junction.

Returns:

  • (String)

    Right T-junction



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

def right_t
  @right_t
end

#thick_crossString (readonly)

Returns Thick cross.

Returns:

  • (String)

    Thick cross



49
50
51
# File 'lib/rich/box.rb', line 49

def thick_cross
  @thick_cross
end

#thick_horizontalString (readonly)

Returns Thick horizontal (for headers).

Returns:

  • (String)

    Thick horizontal (for headers)



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

def thick_horizontal
  @thick_horizontal
end

#thick_left_tString (readonly)

Returns Thick left T-junction.

Returns:

  • (String)

    Thick left T-junction



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

def thick_left_t
  @thick_left_t
end

#thick_right_tString (readonly)

Returns Thick right T-junction.

Returns:

  • (String)

    Thick right T-junction



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

def thick_right_t
  @thick_right_t
end

#top_leftString (readonly)

Returns Top-left corner.

Returns:

  • (String)

    Top-left corner



7
8
9
# File 'lib/rich/box.rb', line 7

def top_left
  @top_left
end

#top_rightString (readonly)

Returns Top-right corner.

Returns:

  • (String)

    Top-right corner



10
11
12
# File 'lib/rich/box.rb', line 10

def top_right
  @top_right
end

#top_tString (readonly)

Returns Top T-junction.

Returns:

  • (String)

    Top T-junction



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

def top_t
  @top_t
end

#verticalString (readonly)

Returns Vertical line.

Returns:

  • (String)

    Vertical line



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

def vertical
  @vertical
end

Class Method Details

.asciiObject

ASCII characters only



162
163
164
# File 'lib/rich/box.rb', line 162

def ascii
  ASCII
end

.doubleObject

Double lines



182
183
184
# File 'lib/rich/box.rb', line 182

def double
  DOUBLE
end

.heavyObject

Heavy/thick lines



177
178
179
# File 'lib/rich/box.rb', line 177

def heavy
  HEAVY
end

.minimalObject

Minimal (no corners)



187
188
189
# File 'lib/rich/box.rb', line 187

def minimal
  MINIMAL
end

.noneObject

No border



197
198
199
# File 'lib/rich/box.rb', line 197

def none
  NONE
end

.roundedObject

Rounded corners



172
173
174
# File 'lib/rich/box.rb', line 172

def rounded
  ROUNDED
end

.simpleObject

Simple horizontal lines only



192
193
194
# File 'lib/rich/box.rb', line 192

def simple
  SIMPLE
end

.squareObject

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

Returns:

  • (Boolean)


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

Parameters:

  • width (Integer)

    Width of content

Returns:

  • (String)


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

Parameters:

  • content (String)

    Content

  • width (Integer)

    Width to pad to

  • align (Symbol) (defaults to: :left)

    Alignment (:left, :center, :right)

Returns:

  • (String)


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)

Parameters:

  • width (Integer)

    Width

Returns:

  • (String)


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

Parameters:

  • width_or_cells (Integer, Array)

    Total width or array of cell contents

  • widths (Array<Integer>, nil) (defaults to: nil)

    Array of column widths

Returns:

  • (String)


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_asciiBox

Substitute ASCII characters for box characters

Returns:



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

Parameters:

  • width (Integer)

    Width of content

Returns:

  • (String)


89
90
91
# File 'lib/rich/box.rb', line 89

def top_edge(width)
  "#{@top_left}#{@horizontal * [0, width - 2].max}#{@top_right}"
end