class Formatador
Constants
- INDENT_REGEX
- PARSE_REGEX
- STYLES
- VERSION
Public Class Methods
Public Instance Methods
Source
# File lib/formatador.rb, line 67 def display(string = '') print(parse("[indent]#{string}")) $stdout.flush nil end
Source
# 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
Source
# File lib/formatador.rb, line 73 def display_line(string = '') display(string) new_line nil end
Source
# File lib/formatador.rb, line 79 def display_lines(lines = []) [*lines].each do |line| display_line(line) end nil end
Source
# 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
Source
# File lib/formatador.rb, line 94 def indent @indent += 1 yield ensure @indent -= 1 end
Source
# File lib/formatador.rb, line 86 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
Source
# File lib/formatador.rb, line 105 def redisplay(string = '', width = 120) print("\r#{' ' * width}\r") display(string.to_s) nil end
Source
# File lib/formatador.rb, line 111 def redisplay_line(string = '', width = 120) redisplay(string, width) new_line nil end
Source
# 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
Source
# File lib/formatador.rb, line 122 def set_title(title) print("\033]0;#{title}\007") $stdout.flush nil end
Source
# File lib/formatador.rb, line 128 def strip(string) string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation } end
Private Instance Methods
Source
# File lib/formatador/table.rb, line 89 def calculate_datum(header, hash) if !hash.keys.include?(header) && (splits = header.to_s.split('.')).length > 1 datum = nil splits.each do |split| d = (datum||hash) datum = d[split] || d[split.to_sym] || '' end else datum = hash.fetch(header, '') end datum end
Source
# File lib/formatador.rb, line 152 def color_support @color_support ||= $stdout.tty? || ENV['GITHUB_ACTIONS'] == 'true' end
Source
# File lib/formatador/table.rb, line 82 def length(value) Reline::Unicode.calculate_width(value.to_s.gsub(PARSE_REGEX, '')) rescue NotImplementedError value.to_s.gsub(PARSE_REGEX, '').chars.reduce(0) { |sum, char| sum += char.bytesize > 1 ? 2 : 1 } end
Source
# File lib/formatador/progressbar.rb, line 50 def progressbar(current, total, options) color = options[:color] started_at = options[:started_at] width = options[:width] output = [] if options[:label] output << options[:label] end # width # we are going to write a string that looks like " current/total" # It would be nice if it were left padded with spaces in such a way that # it puts the progress bar in a constant place on the page. This witdh # calculation allows for the "current" string to be up to two characters # longer than the "total" string without problems. eg- current = # 9.99, total = 10 padding = total.to_s.size * 2 + 3 output << "[#{color}]%#{padding}s[/]" % "#{current}/#{total}" percent = current.to_f / total.to_f percent = 0 if percent < 0 percent = 1 if percent > 1 done = '*' * (percent * width).ceil remaining = ' ' * (width - done.length) output << "[_white_]|[/][#{color}][_#{color}_]#{done}[/]#{remaining}[_white_]|[/]" if started_at elapsed = Time.now - started_at minutes = (elapsed / 60).truncate.to_s seconds = (elapsed % 60).truncate.to_s output << "#{minutes}:#{'0' if seconds.size < 2}#{seconds}" end output << '' output.join(' ') end