lib/collimator.rb in collimator-0.0.1 vs lib/collimator.rb in collimator-0.0.2
- old
+ new
@@ -23,10 +23,11 @@
@separators = []
@live_update = false
@table_string = []
@use_capture_string = false
@use_capture_html = false
+ @last_header_color = '#EEEEEE'
def self.live_update=(live_upate)
@live_update = live_upate
end
@@ -49,13 +50,13 @@
def self.set_horizontal(horizontal_character)
@horiz = horizontal_character
end
def self.header(text, opts = {})
- width, padding, justification = parse_options(opts)
+ width, padding, justification, color = parse_options(opts)
- @headers << { :text => text, :padding => padding, :justification => justification }
+ @headers << { :text => text, :padding => padding, :justification => justification , :color => color}
end
def self.footer(text, opts)
width, padding, justification = parse_options(opts)
@@ -123,11 +124,11 @@
@table_string.join("\n")
end
def self.prep_html_table
@table_string = []
- @table_string << "<table>"
+ @table_string << "<table STYLE=\"font-family: helvetica, verdana, tahoma; border-collapse: collapse;\">"
end
def self.complete_html_table
@table_string << "</table>"
end
@@ -315,15 +316,44 @@
def self.put_column_heading_text_string
put_line_of_data(@column_names)
put_horizontal_line_with_dividers
end
- def self.put_column_heading_text_html
- out = "<tr>\n"
+ def self.style_color(rgb)
+ luminance = get_luminance(rgb)
+ color = luminance < 50 ? '#EEEEEE' : '#222222'
+ color_style = "color: #{color};"
+ end
+ def self.style_header_border(rgb)
+ luminance = get_luminance(rgb)
+ color = luminance < 50 ? '#EEEEEE' : '#222222'
+ color_style = "border-bottom: 1px solid #{color};"
+ end
+
+ def self.get_luminance(rgb)
+ rgb_temp = rgb.gsub("#", '')
+ luminance = 0
+ if rgb_temp.length == 6
+ r = rgb_temp[0..1].hex
+ g = rgb_temp[2..3].hex
+ b = rgb_temp[4..5].hex
+ luminance = (0.299*r + 0.587*g + 0.114*b)
+ end
+ luminance
+ end
+
+ def self.put_column_heading_text_html
+ c = @last_header_color
+ text_color = style_color(c)
+ border_color = style_header_border(c)
+ out = "<tr STYLE=\"background-color: #{@last_header_color}; #{text_color} #{border_color}\">\n"
+ column = 0
@column_names.each do |cname|
- out += "<th>#{cname}</th>\n"
+ padding_style = @columns[column][:padding] ? "STYLE=\"padding-left: #{@columns[column][:padding]}em; padding-right: #{@columns[column][:padding]}em;\"" : ""
+ out += "<th #{padding_style}>#{cname}</th>\n"
+ column += 1
end
out += "</tr>\n"
out += "</thead>"
@@ -366,12 +396,15 @@
header_line += @border
end
def self.make_header_line_html(data)
+ @last_header_color = data[:color] || @last_header_color
+
+ text_color = style_color(@last_header_color)
header_line = "<tr>"
- header_line += "<th colspan='#{@column_names.count + 1}'>#{data[:text]}</th>"
+ header_line += "<th STYLE=\"background-color: #{@last_header_color}; #{text_color}\" colspan='#{@column_names.count + 1}'>#{data[:text]}</th>"
header_line += "</tr>"
header_line
end
def self.put_footer
@@ -402,10 +435,12 @@
def self.put_row_of_html_data(row_data)
column = 0
row_string = "<tr>\n"
row_data.each do | val |
- row_string += "<td>#{val}</td>\n"
+ style_info = @columns[column][:padding] ? " STYLE=\"padding-left: #{@columns[column][:padding]}em; padding-right: #{@columns[column][:padding]}em;\"" : ''
+ row_string += "<td#{style_info}>#{val}</td>\n"
+ column += 1
end
row_string += "</tr>"
send_line row_string