Class: Formatador
- Inherits:
-
Object
- Object
- Formatador
- Defined in:
- lib/formatador.rb,
lib/formatador/table.rb,
lib/formatador/progressbar.rb
Defined Under Namespace
Classes: ProgressBar
Constant Summary collapse
- VERSION =
'1.1.0'
- 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", }
- PARSE_REGEX =
/\[(#{ STYLES.keys.join('|') })\]/ix
- INDENT_REGEX =
/\[indent\]/ix
Instance Method Summary collapse
- #display(string = '') ⇒ Object
- #display_compact_table(hashes, keys = nil, **options, &block) ⇒ Object
- #display_line(string = '') ⇒ Object
- #display_lines(lines = []) ⇒ Object
- #display_table(hashes, keys = nil, **options, &block) ⇒ Object
- #indent(&block) ⇒ Object
- #indentation ⇒ Object
-
#initialize ⇒ Formatador
constructor
A new instance of Formatador.
- #new_line ⇒ Object
- #parse(string) ⇒ Object
- #redisplay(string = '', width = 120) ⇒ Object
- #redisplay_line(string = '', width = 120) ⇒ Object
- #redisplay_progressbar(current, total, options = {}) ⇒ Object
- #strip(string) ⇒ Object
Constructor Details
#initialize ⇒ Formatador
Returns a new instance of Formatador.
59 60 61 |
# File 'lib/formatador.rb', line 59 def initialize @indent = 1 end |
Instance Method Details
#display(string = '') ⇒ Object
63 64 65 66 67 |
# File 'lib/formatador.rb', line 63 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, **, &block) headers = keys || [] widths = {} # Calculate Widths if hashes.empty? && keys keys.each do |key| widths[key] = key.to_s.length 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] - header.to_s.length)}" 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) && [: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
69 70 71 72 73 |
# File 'lib/formatador.rb', line 69 def display_line(string = '') display(string) new_line nil end |
#display_lines(lines = []) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/formatador.rb', line 75 def display_lines(lines = []) for line in [*lines] 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, **, &block) new_hashes = hashes.inject([]) do |accum,item| accum << :split unless accum.empty? accum << item end display_compact_table(new_hashes, keys, **, &block) end |
#indent(&block) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/formatador.rb', line 90 def indent(&block) @indent += 1 yield ensure @indent -= 1 end |
#indentation ⇒ Object
97 98 99 |
# File 'lib/formatador.rb', line 97 def indentation ' ' * @indent end |
#new_line ⇒ Object
113 114 115 116 |
# File 'lib/formatador.rb', line 113 def new_line print("\n") nil end |
#parse(string) ⇒ Object
82 83 84 85 86 87 88 |
# File '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 strip(string) end end |
#redisplay(string = '', width = 120) ⇒ Object
101 102 103 104 105 |
# File 'lib/formatador.rb', line 101 def redisplay(string = '', width = 120) print("\r#{' ' * width}\r") display("#{string}") nil end |
#redisplay_line(string = '', width = 120) ⇒ Object
107 108 109 110 111 |
# File 'lib/formatador.rb', line 107 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 (current, total, = {}) = { :color => 'white', :width => 50, :new_line => true }.merge!() data = (current, total, ) if current < total redisplay(data, [:width]) else redisplay("#{data}", [:width]) if [:new_line] new_line end @progressbar_started_at = nil end end |
#strip(string) ⇒ Object
118 119 120 |
# File 'lib/formatador.rb', line 118 def strip(string) string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation } end |