248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# File 'lib/squared/common/format.rb', line 248
def emphasize(val, title: nil, footer: nil, right: false, cols: nil, sub: nil, pipe: nil,
border: @theme.is_a?(::Hash) && @theme[:border])
n = 0
max = ->(a) { n = [n, a.max_by(&:size).size].max }
set = ->(s) { Array(s).map(&:to_s).tap { |a| max.call(a) } }
title &&= set.call(title)
&&= set.call()
if val.is_a?(::Array)
lines = val.map(&:to_s)
else
lines = val.to_s.lines(chomp: true)
lines[0] = "#{val.class}: #{lines.first}" if (err = val.is_a?(::StandardError))
end
return if lines.empty?
n = (cols.is_a?(::Array) ? cols.map(&:size).max : cols) || max.call(lines)
if $stdout.tty?
require 'io/console'
(n = [n, $stdout.winsize[1] - 4].min) rescue nil
end
b0, b1, b2, b3, b4, b5, b6, b7 = ARG[:BORDER]
out = []
draw = lambda do |a, b|
ret = a + (b1 * (n + 2)) + b
return ret unless border
sub_style ret, border
end
sub = sub.is_a?(::Hash) ? [sub] : Array(sub)
pr = lambda do |line|
s = line.ljust(n)
sub.each { |h| sub_style!(s, **h) }
s = +"#{b0} #{s} #{b0}"
if border
[[/\A(#{Regexp.escape(b0)})(.+)\z/om], [/\A(.+)(#{Regexp.escape(b0)})\z/om, 2]].each do |args|
sub_style!(s, **opt_style(border, *args))
end
end
s
end
out << draw.call(b2, b3)
if title
out.concat(title.map { |t| pr.call(t) })
out << draw.call(b6, b7)
end
lines.each { |line| out << pr.call(line) }
out << draw.call(b5, b4)
if
unless sub.empty? && !right
.map! do |s|
s = s.rjust(n + 4) if right
sub.each { |h| sub_style!(s, **h) }
s
end
end
out.concat()
end
if block_given?
yield out
elsif pipe
return out if pipe == -1
case pipe
when 0
$stdin
when 2
$stderr
else
pipe.respond_to?(:puts) ? pipe : $stdout
end.puts(out)
else
err ? warn(out) : puts(out)
end
end
|