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"
- SPACE =
" "
- LINE_BREAK =
%r{\r\n|\r|\n}.freeze
- BOX_CHARS =
{ ascii: %w[+ + + + + + + + - | +], light: %w[┘ ┐ ┌ └ ┤ ┴ ┬ ├ ─ │ ┼], thick: %w[╝ ╗ ╔ ╚ ╣ ╩ ╦ ╠ ═ ║ ╬] }.freeze
- VERSION =
"0.7.0"
Class Method Summary collapse
- .color(enabled: nil) ⇒ Object
- .corner_bottom_left_char(border = :light) ⇒ Object
- .corner_bottom_right_char(border = :light) ⇒ Object
- .corner_top_left_char(border = :light) ⇒ Object
- .corner_top_right_char(border = :light) ⇒ Object
- .cross_char(border = :light) ⇒ Object
- .cursor ⇒ Object
- .divider_down_char(border = :light) ⇒ Object
- .divider_left_char(border = :light) ⇒ Object
- .divider_right_char(border = :light) ⇒ Object
- .divider_up_char(border = :light) ⇒ Object
-
.error(message, **opts) ⇒ Object
A frame for error type message.
-
.frame(*content, top: nil, left: nil, width: nil, height: nil, align: :left, padding: 0, title: {}, border: :light, style: {}, enable_color: nil) ⇒ Object
Create a frame.
-
.info(message, **opts) ⇒ Object
A frame for info type message.
- .line_char(border = :light) ⇒ Object
- .pipe_char(border = :light) ⇒ Object
-
.success(message, **opts) ⇒ Object
A frame for for success type message.
-
.warn(message, **opts) ⇒ Object
A frame for warning type message.
Class Method Details
.color(enabled: nil) ⇒ Object
73 74 75 |
# File 'lib/tty/box.rb', line 73 def color(enabled: nil) @color ||= Pastel.new(enabled: enabled) end |
.corner_bottom_left_char(border = :light) ⇒ Object
37 38 39 |
# File 'lib/tty/box.rb', line 37 def corner_bottom_left_char(border = :light) BOX_CHARS[border][3] end |
.corner_bottom_right_char(border = :light) ⇒ Object
25 26 27 |
# File 'lib/tty/box.rb', line 25 def corner_bottom_right_char(border = :light) BOX_CHARS[border][0] end |
.corner_top_left_char(border = :light) ⇒ Object
33 34 35 |
# File 'lib/tty/box.rb', line 33 def corner_top_left_char(border = :light) BOX_CHARS[border][2] end |
.corner_top_right_char(border = :light) ⇒ Object
29 30 31 |
# File 'lib/tty/box.rb', line 29 def corner_top_right_char(border = :light) BOX_CHARS[border][1] end |
.cross_char(border = :light) ⇒ Object
65 66 67 |
# File 'lib/tty/box.rb', line 65 def cross_char(border = :light) BOX_CHARS[border][10] end |
.cursor ⇒ Object
69 70 71 |
# File 'lib/tty/box.rb', line 69 def cursor TTY::Cursor end |
.divider_down_char(border = :light) ⇒ Object
49 50 51 |
# File 'lib/tty/box.rb', line 49 def divider_down_char(border = :light) BOX_CHARS[border][6] end |
.divider_left_char(border = :light) ⇒ Object
41 42 43 |
# File 'lib/tty/box.rb', line 41 def divider_left_char(border = :light) BOX_CHARS[border][4] end |
.divider_right_char(border = :light) ⇒ Object
53 54 55 |
# File 'lib/tty/box.rb', line 53 def divider_right_char(border = :light) BOX_CHARS[border][7] end |
.divider_up_char(border = :light) ⇒ Object
45 46 47 |
# File 'lib/tty/box.rb', line 45 def divider_up_char(border = :light) BOX_CHARS[border][5] end |
.error(message, **opts) ⇒ Object
A frame for error type message
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tty/box.rb', line 152 def error(, **opts) new_opts = { title: { top_left: " ⨯ ERROR " }, border: { type: :thick }, padding: 1, style: { fg: :bright_white, bg: :red, border: { fg: :bright_white, bg: :red } } }.merge(opts) frame(**new_opts) { } end |
.frame(*content, top: nil, left: nil, width: nil, height: nil, align: :left, padding: 0, title: {}, border: :light, style: {}, enable_color: nil) ⇒ Object
Create a frame
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/tty/box.rb', line 191 def frame(*content, top: nil, left: nil, width: nil, height: nil, align: :left, padding: 0, title: {}, border: :light, style: {}, enable_color: nil) @color = nil color(enabled: enable_color) output = [] 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 str = block_given? ? yield : content_to_str(content) sep = str[LINE_BREAK] || NEWLINE # infer line break content_lines = str.split(sep) # infer dimensions dimensions = infer_dimensions(content_lines, padding) width ||= left_size + dimensions[0] + right_size width = [width, top_space_taken(title, border), bottom_space_taken(title, border)].max height ||= top_size + dimensions[1] + bottom_size # apply formatting to content formatted_lines = format(content_lines, width, padding, align, sep) # infer styling 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 filler_size = width - left_size - right_size if formatted_line = formatted_lines[i] output << bg.(fg.(formatted_line)) line_content_size = Strings::ANSI.sanitize(formatted_line) .scan(/[[:print:]]/).join.size filler_size = [filler_size - line_content_size, 0].max end if style[:fg] || style[:bg] || !position # something to color output << bg.(fg.(SPACE * filler_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 |
.info(message, **opts) ⇒ Object
A frame for info type message
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tty/box.rb', line 83 def info(, **opts) new_opts = { title: { top_left: " ℹ INFO " }, border: { type: :thick }, padding: 1, style: { fg: :black, bg: :bright_blue, border: { fg: :black, bg: :bright_blue } } }.merge(opts) frame(**new_opts) { } end |
.line_char(border = :light) ⇒ Object
57 58 59 |
# File 'lib/tty/box.rb', line 57 def line_char(border = :light) BOX_CHARS[border][8] end |
.pipe_char(border = :light) ⇒ Object
61 62 63 |
# File 'lib/tty/box.rb', line 61 def pipe_char(border = :light) BOX_CHARS[border][9] end |
.success(message, **opts) ⇒ Object
A frame for for success type message
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tty/box.rb', line 129 def success(, **opts) new_opts = { title: { top_left: " ✔ OK " }, border: { type: :thick }, padding: 1, style: { fg: :black, bg: :bright_green, border: { fg: :black, bg: :bright_green } } }.merge(opts) frame(**new_opts) { } end |
.warn(message, **opts) ⇒ Object
A frame for warning type message
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/tty/box.rb', line 106 def warn(, **opts) new_opts = { title: { top_left: " ⚠ WARNING " }, border: { type: :thick }, padding: 1, style: { fg: :black, bg: :bright_yellow, border: { fg: :black, bg: :bright_yellow } } }.merge(opts) frame(**new_opts) { } end |