Module: Squared::Common::Format

Constant Summary

Constants included from Squared::Common

ARG, PATH

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emphasize(val, title: nil, footer: nil, right: false, cols: nil, sub: nil, pipe: nil, border: ) ⇒ Object



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)
  footer &&= set.call(footer)
  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 footer
    unless sub.empty? && !right
      footer.map! do |s|
        s = s.rjust(n + 4) if right
        sub.each { |h| sub_style!(s, **h) }
        s
      end
    end
    out.concat(footer)
  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

.message(*args, hint: nil, empty: false, space: ) ⇒ Object



242
243
244
245
246
# File 'lib/squared/common/format.rb', line 242

def message(*args, hint: nil, empty: false, space: ARG[:SPACE])
  (empty ? args.reject { |val| val.nil? || (val.respond_to?(:empty?) && val.empty?) } : args)
    .join(space)
    .subhint(hint)
end

.raise_error(*args, hint: nil, kind: RuntimeError, start: 0) ⇒ Object

Raises:

  • (kind)


323
324
325
326
327
328
329
330
# File 'lib/squared/common/format.rb', line 323

def raise_error(*args, hint: nil, kind: RuntimeError, start: 0)
  kind = args.shift if args.first.is_a?(::Class) && args.first < ::Exception
  raise kind, message(*args, hint: hint, empty: true), if ARG[:BACKTRACE]
                                                         caller(start.succ)
                                                       else
                                                         caller_locations(start.succ, 1).first&.base_label
                                                       end
end

Instance Method Details

#enable_aixtermObject



46
47
48
49
50
51
# File 'lib/squared/common/format.rb', line 46

def enable_aixterm
  unless (colors = __get__(:colors)).frozen?
    colors.update(AIX_TERM)
  end
  self
end

#enable_drawingObject



53
54
55
56
57
# File 'lib/squared/common/format.rb', line 53

def enable_drawing
  ARG[:GRAPH] = BOX_GRAPH
  ARG[:BORDER] = BOX_BORDER
  self
end