Class: Formatador

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

Constant Summary collapse

VERSION =
'0.0.16'
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            => "30",
  :red              => "31",
  :green            => "32",
  :yellow           => "33",
  :blue             => "34",
  :magenta          => "35",
  :purple           => "35",
  :cyan             => "36",
  :white            => "37",
  :_black_          => "40",
  :_red_            => "41",
  :_green_          => "42",
  :_yellow_         => "43",
  :_blue_           => "44",
  :_magenta_        => "45",
  :_purple_         => "45",
  :_cyan_           => "46",
  :_white_          => "47",
  :light_black      => "90",
  :light_red        => "91",
  :light_green      => "92",
  :light_yellow     => "93",
  :light_blue       => "94",
  :light_magenta    => "95",
  :light_purple     => "95",
  :light_cyan       => "96",
  :_light_black_    => "100",
  :_light_red_      => "101",
  :_light_green_    => "102",
  :_light_yellow_   => "103",
  :_light_blue_     => "104",
  :_light_magenta_  => "105",
  :_light_purple_   => "105",
  :_light_cyan_     => "106",
}
PARSE_REGEX =
/\[(#{ STYLES.keys.join('|') })\]/ix
INDENT_REGEX =
/\[indent\]/ix

Instance Method Summary collapse

Constructor Details

#initializeFormatador

Returns a new instance of Formatador.



59
60
61
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 59

def initialize
  @indent = 1
end

Instance Method Details

#display(string = '') ⇒ Object



63
64
65
66
67
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 63

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

#display_line(string = '') ⇒ Object



69
70
71
72
73
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 69

def display_line(string = '')
  display(string)
  print("\n")
  nil
end

#display_lines(lines = []) ⇒ Object



75
76
77
78
79
80
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 75

def display_lines(lines = [])
  for line in [*lines]
    display_line(line)
  end
  nil
end

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



3
4
5
6
7
8
9
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
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador/table.rb', line 3

def display_table(hashes, keys = nil, &block)
  headers = keys || []
  widths = {}
  if hashes.empty? && keys
    for key in keys
      widths[key] = key.to_s.length
    end
  else
    for hash in hashes
      for key in hash.keys
        unless keys
          headers << key
        end
        widths[key] = [key.to_s.length, widths[key] || 0, hash[key] && hash[key].to_s.length || 0].max
      end
      headers = headers.uniq
    end
  end

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

  split = "+"
  if headers.empty?
    split << '--+'
  else
    for header in headers
      split << ('-' * (widths[header] + 2)) << '+'
    end
  end

  display_line(split)
  columns = []
  for header in headers
    columns << "[bold]#{header}[/]#{' ' * (widths[header] - header.to_s.length)}"
  end
  display_line("| #{columns.join(' | ')} |")
  display_line(split)

  for hash in hashes
    columns = []
    for header in headers
      datum = hash[header] || ''
      columns << "#{datum}#{' ' * (widths[header] - datum.to_s.length)}"
    end
    display_line("| #{columns.join(' | ')} |")
    display_line(split)
  end
  nil
end

#indent(&block) ⇒ Object



90
91
92
93
94
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 90

def indent(&block)
  @indent += 1
  yield
  @indent -= 1
end

#indentationObject



96
97
98
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 96

def indentation
  '  ' * @indent
end

#parse(string) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 82

def parse(string)
  if STDOUT.tty?
    string.gsub(PARSE_REGEX) { "\e[#{STYLES[$1.to_sym]}m" }.gsub(INDENT_REGEX) { indentation }
  else
    string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation }
  end
end

#redisplay(string = '') ⇒ Object



100
101
102
103
104
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador.rb', line 100

def redisplay(string = '')
  print("\r")
  display("#{string}")
  nil
end

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



3
4
5
6
7
8
9
10
11
12
# File 'lib/bbcloud/vendor/formatador-0.0.16/lib/formatador/progressbar.rb', line 3

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