Class: FatTable::TermFormatter

Inherits:
Formatter show all
Defined in:
lib/fat_table/formatters/term_formatter.rb

Overview

Output the table as for a unicode-enabled ANSI terminal. This makes table gridlines drawable with unicode characters, as well as supporting colored text and backgrounds, and blink, and underline attributes. See TermFormatter.valid_colors for an Array of valid colors that you can use. The extent to which all of these are actually supported depends on your terminal. TermFormatter uses the +rainbow+ gem for forming colored strings. Use a

Constant Summary

Constants inherited from Formatter

Formatter::LOCATIONS

Instance Attribute Summary

Attributes inherited from Formatter

#footers, #format_at, #gfooters, #options, #table

Instance Method Summary collapse

Methods inherited from Formatter

#avg_footer, #avg_gfooter, default_format, #foot, #footer, #format, #format_cell, #format_for, #gfoot, #gfooter, #max_footer, #max_gfooter, #min_footer, #min_gfooter, #output, #sum_footer, #sum_gfooter

Constructor Details

#initialize(table = Table.new, **options) ⇒ TermFormatter

Return a new TermFormatter for +table+. You can set a few +options+ with the following hash-like parameters:

unicode:: if set true, use unicode characters to form the frame of the table on output; if set false, use ASCII characters for the frame. By default, this is true.

framecolor:: set to a string of the form '' or '' to set the color of the frame or the color and background color. By default, the framecolor is set to 'none.none', meaning that the normal terminal foreground and background colors will be used for the frame.



27
28
29
30
31
32
33
34
35
# File 'lib/fat_table/formatters/term_formatter.rb', line 27

def initialize(table = Table.new, **options)
  super
  @options[:unicode] = options.fetch(:unicode, true)
  @options[:framecolor] = options.fetch(:framecolor, 'none.none')
  return unless @options[:framecolor] =~ /(?<co>[-_a-zA-Z]*)(\.(?<bg>[-_a-zA-Z]*))/

  @options[:frame_fg] = Regexp.last_match[:co].downcase unless Regexp.last_match[:co].blank?
  @options[:frame_bg] = Regexp.last_match[:bg].downcase unless Regexp.last_match[:bg].blank?
end

Instance Method Details

#decorate_string(str, istruct) ⇒ Object

Add ANSI codes to string to implement the given decorations



42
43
44
45
46
47
48
49
50
# File 'lib/fat_table/formatters/term_formatter.rb', line 42

def decorate_string(str, istruct)
  result = Rainbow(str)
  result = colorize(result, istruct.color, istruct.bgcolor)
  result = result.bold if istruct.bold
  result = result.italic if istruct.italic
  result = result.underline if istruct.underline
  result = result.blink if istruct.blink
  result
end