Class: Formatador

Inherits:
Object
  • Object
show all
Defined in:
lib/formatador.rb,
lib/formatador/table.rb,
lib/formatador/progressbar.rb

Defined Under Namespace

Classes: ProgressBar

Constant Summary collapse

VERSION =
'1.2.1'
STYLES =
{
  "\/": '0',
  reset: '0',
  bold: '1',
  underline: '4',
  blink_slow: '5',
  blink_fast: '6',
  negative: '7', # invert color/color
  normal: '22',
  underline_none: '24',
  blink_off: '25',
  positive: '27', # revert color/color
  _black_: '40',
  _red_: '41',
  _green_: '42',
  _yellow_: '43',
  _blue_: '44',
  _magenta_: '45',
  _purple_: '45',
  _cyan_: '46',
  _white_: '47',
  _light_black_: '100',
  _light_red_: '101',
  _light_green_: '102',
  _light_yellow_: '103',
  _light_blue_: '104',
  _light_magenta_: '105',
  _light_purple_: '105',
  _light_cyan_: '106',
  black: '30',
  red: '31',
  green: '32',
  yellow: '33',
  blue: '34',
  magenta: '35',
  purple: '35',
  cyan: '36',
  white: '37',
  light_black: '90',
  light_red: '91',
  light_green: '92',
  light_yellow: '93',
  light_blue: '94',
  light_magenta: '95',
  light_purple: '95',
  light_cyan: '96'
}.freeze
PARSE_REGEX =
/\[(#{STYLES.keys.join('|')})\]/ix.freeze
INDENT_REGEX =
/\[indent\]/ix.freeze

Instance Method Summary collapse

Constructor Details

#initializeFormatador

Returns a new instance of Formatador.



62
63
64
# File 'lib/formatador.rb', line 62

def initialize
  @indent = 1
end

Instance Method Details

#display(string = '') ⇒ Object



66
67
68
69
70
# File 'lib/formatador.rb', line 66

def display(string = '')
  print(parse("[indent]#{string}"))
  $stdout.flush
  nil
end

#display_compact_table(hashes, keys = nil, **options, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/formatador/table.rb', line 10

def display_compact_table(hashes, keys = nil, **options, &block)
  headers = keys || []
  widths = {}

  # Calculate Widths
  if hashes.empty? && keys
    keys.each do |key|
      widths[key] = length(key)
    end
  else
    hashes.each do |hash|
      next unless hash.respond_to?(:keys)

      (headers + hash.keys).each do |key|
        if !keys
          headers << key
        end
        widths[key] = [length(key), widths[key] || 0, length(calculate_datum(key, hash)) || 0].max
      end
      headers = headers.uniq
    end
  end

  # Determine order of headers
  if block_given?
    headers = headers.sort(&block)
  elsif !keys
    headers = headers.sort {|x,y| x.to_s <=> y.to_s}
  end

  # Display separator row
  split = "+"
  if headers.empty?
    split << '--+'
  else
    headers.each do |header|
      widths[header] ||= length(header)
      split << ('-' * (widths[header] + 2)) << '+'
    end
  end
  display_line(split)

  # Display data row
  columns = []
  headers.each do |header|
    columns << "[bold]#{header}[/]#{' ' * (widths[header] - length(header))}"
  end
  display_line("| #{columns.join(' | ')} |")
  display_line(split)

  hashes.each do |hash|
    if hash.respond_to? :keys
      columns = headers.map do |header|
        datum = calculate_datum(header, hash)
        width = widths[header] - length(datum)
        width = width < 0 ? 0 : width

        datum.is_a?(Numeric) && options[:numeric_rjust] ? "#{' ' * width}#{datum}" : "#{datum}#{' ' * width}"
      end
      display_line("| #{columns.join(' | ')} |")
    else
      if hash == :split
        display_line(split)
      end
    end
    nil
  end
  display_line(split)
end

#display_line(string = '') ⇒ Object



72
73
74
75
76
# File 'lib/formatador.rb', line 72

def display_line(string = '')
  display(string)
  new_line
  nil
end

#display_lines(lines = []) ⇒ Object



78
79
80
81
82
83
# File 'lib/formatador.rb', line 78

def display_lines(lines = [])
  [*lines].each do |line|
    display_line(line)
  end
  nil
end

#display_table(hashes, keys = nil, **options, &block) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/formatador/table.rb', line 2

def display_table(hashes, keys = nil, **options, &block)
  new_hashes = hashes.inject([]) do |accum,item|
    accum << :split unless accum.empty?
    accum << item
  end
  display_compact_table(new_hashes, keys, **options, &block)
end

#indentObject



93
94
95
96
97
98
# File 'lib/formatador.rb', line 93

def indent
  @indent += 1
  yield
ensure
  @indent -= 1
end

#indentationObject



100
101
102
# File 'lib/formatador.rb', line 100

def indentation
  '  ' * @indent
end

#new_lineObject



116
117
118
119
# File 'lib/formatador.rb', line 116

def new_line
  print("\n")
  nil
end

#parse(string) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/formatador.rb', line 85

def parse(string)
  if color_support
    string.gsub(PARSE_REGEX) { "\e[#{STYLES[::Regexp.last_match(1).to_sym]}m" }.gsub(INDENT_REGEX) { indentation }
  else
    strip(string)
  end
end

#redisplay(string = '', width = 120) ⇒ Object



104
105
106
107
108
# File 'lib/formatador.rb', line 104

def redisplay(string = '', width = 120)
  print("\r#{' ' * width}\r")
  display(string.to_s)
  nil
end

#redisplay_line(string = '', width = 120) ⇒ Object



110
111
112
113
114
# File 'lib/formatador.rb', line 110

def redisplay_line(string = '', width = 120)
  redisplay(string, width)
  new_line
  nil
end

#redisplay_progressbar(current, total, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/formatador/progressbar.rb', line 34

def redisplay_progressbar(current, total, options = {})
  options = { :color => 'white', :width => 50, :new_line => true }.merge!(options)
  data = progressbar(current, total, options)
  if current < total
    redisplay(data, options[:width])
  else
    redisplay("#{data}", options[:width])
    if options[:new_line]
      new_line
    end
    @progressbar_started_at = nil
  end
end

#strip(string) ⇒ Object



121
122
123
# File 'lib/formatador.rb', line 121

def strip(string)
  string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation }
end