Module: TTY::Box

Defined in:
lib/tty/box.rb,
lib/tty/box/border.rb,
lib/tty/box/version.rb

Defined Under Namespace

Classes: Border

Constant Summary collapse

NEWLINE =
"\n"
LINE_BREAK =
%r{\r\n|\r|\n}.freeze
BOX_CHARS =
{
  ascii: %w[+ + + + + + + + - | +],
  light: %w[          ],
  thick: %w[          ]
}.freeze
VERSION =
"0.4.1"

Class Method Summary collapse

Class Method Details

.bottom_border(title, width, border, style) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bottom border

Returns:

  • (String)

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/tty/box.rb', line 202

def bottom_border(title, width, border, style)
  bottom_titles_size = title[:bottom_left].to_s.size +
                       title[:bottom_center].to_s.size +
                       title[:bottom_right].to_s.size
  fg, bg = *extract_style(style[:border] || {})

  bottom_left  = border.bottom_left? && border.left? ? send(:"#{border.bottom_left}_char", border.type) : ""
  bottom_right = border.bottom_right? && border.right? ? send(:"#{border.bottom_right}_char", border.type) : ""

  bottom_space_left = width - bottom_titles_size -
                      bottom_left.size - bottom_right.size
  bottom_space_before = bottom_space_left / 2
  bottom_space_after = bottom_space_left / 2 + bottom_space_left % 2

  [
    bg.(fg.(bottom_left)),
    bg.(title[:bottom_left].to_s),
    bg.(fg.(line_char(border.type) * bottom_space_before)),
    bg.(title[:bottom_center].to_s),
    bg.(fg.(line_char(border.type) * bottom_space_after)),
    bg.(title[:bottom_right].to_s),
    bg.(fg.(bottom_right))
  ].join('')
end

.colorObject


72
73
74
# File 'lib/tty/box.rb', line 72

def color
  @color ||= Pastel.new
end

.corner_bottom_left_char(border = :light) ⇒ Object


36
37
38
# File 'lib/tty/box.rb', line 36

def corner_bottom_left_char(border = :light)
  BOX_CHARS[border][3]
end

.corner_bottom_right_char(border = :light) ⇒ Object


24
25
26
# File 'lib/tty/box.rb', line 24

def corner_bottom_right_char(border = :light)
  BOX_CHARS[border][0]
end

.corner_top_left_char(border = :light) ⇒ Object


32
33
34
# File 'lib/tty/box.rb', line 32

def corner_top_left_char(border = :light)
  BOX_CHARS[border][2]
end

.corner_top_right_char(border = :light) ⇒ Object


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

def corner_top_right_char(border = :light)
  BOX_CHARS[border][1]
end

.cross_char(border = :light) ⇒ Object


64
65
66
# File 'lib/tty/box.rb', line 64

def cross_char(border = :light)
  BOX_CHARS[border][10]
end

.cursorObject


68
69
70
# File 'lib/tty/box.rb', line 68

def cursor
  TTY::Cursor
end

.divider_down_char(border = :light) ⇒ Object


48
49
50
# File 'lib/tty/box.rb', line 48

def divider_down_char(border = :light)
  BOX_CHARS[border][6]
end

.divider_left_char(border = :light) ⇒ Object


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

def divider_left_char(border = :light)
  BOX_CHARS[border][4]
end

.divider_right_char(border = :light) ⇒ Object


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

def divider_right_char(border = :light)
  BOX_CHARS[border][7]
end

.divider_up_char(border = :light) ⇒ Object


44
45
46
# File 'lib/tty/box.rb', line 44

def divider_up_char(border = :light)
  BOX_CHARS[border][5]
end

.extract_style(style) ⇒ Array[Proc, Proc]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert style keywords into styling

Returns:

  • (Array[Proc, Proc])

162
163
164
165
166
# File 'lib/tty/box.rb', line 162

def extract_style(style)
  fg = style[:fg] ? color.send(style[:fg]).detach : -> (c) { c }
  bg = style[:bg] ? color.send(:"on_#{style[:bg]}").detach : -> (c) { c }
  [fg, bg]
end

.format(content, width, padding, align) ⇒ Array[String]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format content

Returns:

  • (Array[String])

146
147
148
149
150
151
152
153
154
155
# File 'lib/tty/box.rb', line 146

def format(content, width, padding, align)
  pad = Strings::Padder.parse(padding)
  total_width = width - 2 - (pad.left + pad.right)
  sep = content[LINE_BREAK] || NEWLINE # infer line break

  wrapped = Strings.wrap(content, total_width)
  aligned = Strings.align(wrapped, total_width, direction: align)
  padded  = Strings.pad(aligned, padding)
  padded.split(sep)
end

.frame(top: nil, left: nil, width: 35, height: 3, align: :left, padding: 0, title: {}, border: :light, style: {}) ⇒ Object

Create a frame


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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tty/box.rb', line 79

def frame(top: nil, left: nil, width: 35, height: 3, align: :left, padding: 0,
          title: {}, border: :light, style: {})
  output = []
  content = []
  sep = NEWLINE
  position = top && left

  border = Border.parse(border)
  top_size    = border.top? ? 1: 0
  bottom_size = border.bottom? ? 1: 0
  left_size   = border.left? ? 1 : 0
  right_size  = border.right ? 1 : 0

  if block_given?
    str = yield
    sep = str[LINE_BREAK] || NEWLINE # infer line break
    content = format(str, width, padding, align)
  end

  fg, bg = *extract_style(style)
  border_fg, border_bg = *extract_style(style[:border] || {})

  if border.top?
    output << cursor.move_to(left, top) if position
    output << top_border(title, width, border, style)
    output << sep unless position
  end

  (height - top_size - bottom_size).times do |i|
    output << cursor.move_to(left, top + i + top_size) if position
    if border.left?
      output << border_bg.(border_fg.(pipe_char(border.type)))
    end

    content_size = width - left_size - right_size
    unless content[i].nil?
      output << bg.(fg.(content[i]))
      size = Strings::ANSI.sanitize(content[i]).scan(/[[:print:]]/).join.size
      content_size -= size
    end
    if style[:fg] || style[:bg] || !position # something to color
      output << bg.(fg.(' ' * content_size))
    end

    if border.right?
      if position
        output << cursor.move_to(left + width - right_size, top + i + top_size)
      end
      output << border_bg.(border_fg.(pipe_char(border.type)))
    end
    output << sep unless position
  end

  if border.bottom?
    output << cursor.move_to(left, top + height - bottom_size) if position
    output << bottom_border(title, width, border, style)
    output << sep unless position
  end

  output.join
end

.line_char(border = :light) ⇒ Object


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

def line_char(border = :light)
  BOX_CHARS[border][8]
end

.pipe_char(border = :light) ⇒ Object


60
61
62
# File 'lib/tty/box.rb', line 60

def pipe_char(border = :light)
  BOX_CHARS[border][9]
end

.top_border(title, width, border, style) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Top border

Returns:

  • (String)

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tty/box.rb', line 173

def top_border(title, width, border, style)
  top_titles_size = title[:top_left].to_s.size +
                    title[:top_center].to_s.size +
                    title[:top_right].to_s.size
  fg, bg = *extract_style(style[:border] || {})

  top_left = border.top_left? && border.left? ? send(:"#{border.top_left}_char", border.type) : ""
  top_right = border.top_right? && border.right? ? send(:"#{border.top_right}_char", border.type) : ""

  top_space_left   = width - top_titles_size - top_left.size - top_right.size
  top_space_before = top_space_left / 2
  top_space_after  = top_space_left / 2 + top_space_left % 2

  [
    bg.(fg.(top_left)),
    bg.(title[:top_left].to_s),
    bg.(fg.(line_char(border.type) * top_space_before)),
    bg.(title[:top_center].to_s),
    bg.(fg.(line_char(border.type) * top_space_after)),
    bg.(title[:top_right].to_s),
    bg.(fg.(top_right))
  ].join('')
end