Created
February 4, 2015 11:31
-
-
Save Larusso/ce7df9a788e945e83cae to your computer and use it in GitHub Desktop.
Monkeypatch for text-table to support ANSI colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'colorize' | |
module Text | |
class Table | |
class Cell | |
def initialize(options = {}) #:nodoc: | |
@value = options[:value].to_s | |
@row = options[:row] | |
@align = options[:align ] || :left | |
@colspan = options[:colspan] || 1 | |
@color = options[:color] || :default | |
@background = options[:background] || :default | |
@effect = options[:effect] || :default | |
end | |
def set_color(string) | |
string.colorize color: @color, background: @background, mode: @effect | |
end | |
def to_s #:nodoc: | |
([' ' * table.horizontal_padding]*2).join case align | |
when :left | |
set_color(value.ljust cell_width) | |
when :right | |
set_color(value.rjust cell_width) | |
when :center | |
set_color(value.center cell_width) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment