Class: Strop::Optlist

Inherits:
Object
  • Object
show all
Defined in:
lib/imsg-grep/utils/strop_utils.rb

Instance Method Summary collapse

Instance Method Details

#report_usage(chars = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/imsg-grep/utils/strop_utils.rb', line 62

def report_usage(chars = nil)
  x     = true # flipper for stagger
  r     = ->s{ Rainbow(s) }
  chars = chars&.chars || (?0..?z).to_a.grep(/[^\W_]/)
  chars = chars.sort_by{|c| [c =~ /\d/ || -1, c.downcase, c =~ /[[:upper:]]/ || 1] } # A, a, B, ... 9
  chars = chars.map{|c| [c] << r[c].then{ self[c]&.label ? it.black.bright : it.green.bright }}
  longs = chars.map{|c, cc| l = self[c]&.label; [r["-"].black.bright, cc, r[(" --#{l}" if l)].blue].join }
  longs = longs.map{ it.sub((x=!x) ? /^/ : / --/, '  \0') } # stagger
  w     = longs.map(&:size).max + 4 # col width
  pc    = [16, chars.length].min # results per column
  ll    = longs.length
  longs.fill("", ll...((ll + pc - 1) / pc * pc)) # fill to multiple for transpose
  # puts self
  puts chars.map(&:last).join(" ")
  puts
  puts longs.map{ "%-#{w}s" % it }.each_slice(pc).to_a.transpose.map(&:join).join("\n")
end