class Canis::DefaultTableRenderer

sorter }}} renderer {{{

TODO see how jtable does the renderers and columns stuff.

perhaps we can combine the two but have different methods or some flag that way oter methods can be shared

Public Class Methods

new(source) click to toggle source

source is the textpad or extending widget needed so we can call show_colored_chunks if the user specifies column wise colors

# File lib/canis/core/widgets/table.rb, line 203
def initialize source
  @source = source
  @y = '|'
  @x = '+'
  @coffsets = []
  @header_color = :white
  @header_bgcolor = :red
  @header_attrib = NORMAL
  @color = :white
  @bgcolor = :black
  @color_pair = $datacolor
  @attrib = NORMAL
  @_check_coloring = nil
  # adding setting column_model auto on 2014-04-10 - 10:53 why wasn;t this here already
  column_model(source.column_model)
end

Public Instance Methods

check_colors() click to toggle source

check if we need to individually color columns or we can do the entire row in one shot

# File lib/canis/core/widgets/table.rb, line 343
def check_colors
  each_column {|c,i|
    if c.color || c.bgcolor || c.attrib
      @_check_coloring = true
      return
    end
    @_check_coloring = false
  }
end
colorize(pad, lineno, r) click to toggle source
# File lib/canis/core/widgets/table.rb, line 358
def colorize pad, lineno, r
  # the incoming data is already in the order of display based on chash,
  # so we cannot run chash on it again, so how do we get the color info
  _offset = 0
  each_column {|c,i|
    text = r[i]
    color = c.color
    bg = c.bgcolor
    if color || bg
      cp = get_color(@color_pair, color || @color, bg || @bgcolor)
    else
      cp = @color_pair
    end
    att = c.attrib || @attrib
    FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
    FFI::NCurses.mvwaddstr(pad, lineno, _offset, text)
    FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
    _offset += text.length
  }
end
column_model(c) click to toggle source

set column model (Table Renderer)

# File lib/canis/core/widgets/table.rb, line 236
def column_model c
  @chash = c
end
content_attrib(att) click to toggle source
# File lib/canis/core/widgets/table.rb, line 232
def content_attrib att
  @attrib = att
end
content_colors(fg, bg) click to toggle source

set fg and bg color of content rows, default is $datacolor (white on black).

# File lib/canis/core/widgets/table.rb, line 227
def content_colors fg, bg
  @color = fg
  @bgcolor = bg
  @color_pair = get_color($datacolor, fg, bg)
end
convert_value_to_text(r) click to toggle source

Takes the array of row data and formats it using column widths and returns an array which is used for printing

return an array so caller can color columns if need be

# File lib/canis/core/widgets/table.rb, line 244
def convert_value_to_text r
  str = []
  fmt = nil
  field = nil
  # we need to loop through chash and get index from it and get that row from r
  each_column {|c,i|
    e = r[c.index]
    w = c.width
    l = e.to_s.length
    # if value is longer than width, then truncate it
    if l > w
      fmt = "%.#{w}s "
    else
      case c.align
      when :right
        fmt = "%#{w}s "
      else
        fmt = "%-#{w}s "
      end
    end
    field = fmt % e
    # if we really want to print a single column with color, we need to print here itself
    # each cell. If we want the user to use tmux formatting in the column itself ...
    # FIXME - this must not be done for headers.
    #if c.color
    #field = "#[fg=#{c.color}]#{field}#[/end]"
    #end
    str << field
  }
  return str
end
each_column() { |c,i| ... } click to toggle source
# File lib/canis/core/widgets/table.rb, line 352
def each_column
  @chash.each_with_index { |c, i|
    next if c.hidden
    yield c,i if block_given?
  }
end
header_attrib(att) click to toggle source
# File lib/canis/core/widgets/table.rb, line 223
def header_attrib att
  @header_attrib = att
end
header_colors(fg, bg) click to toggle source
# File lib/canis/core/widgets/table.rb, line 219
def header_colors fg, bg
  @header_color = fg
  @header_bgcolor = bg
end
render(pad, lineno, str) click to toggle source

@param pad for calling print methods on @param lineno the line number on the pad to print on @param [String] data to print which will be an array (@list)

# File lib/canis/core/widgets/table.rb, line 285
def render pad, lineno, str
  #lineno += 1 # header_adjustment
  # header_adjustment means columns have been set
  return render_header pad, lineno, 0, str if lineno == 0  && @source.header_adjustment > 0
  #text = str.join " | "
  #text = @fmstr % str
  text = convert_value_to_text str
  if @_check_coloring
    $log.debug "XXX:  INSIDE COLORIIN"
    text = colorize pad, lineno, text
    return
  end
  # check if any specific colors , if so then print colors in a loop with no dependence on colored chunks
  # then we don't need source pointer
  render_data pad, lineno, text

end
render_data(pad, lineno, text) click to toggle source

passes padded data for final printing or data row this allows user to do row related coloring without having to tamper with the headers or other internal workings. This will not be called if column specific colorign is in effect. @param text is an array of strings, in the order of actual printing with hidden cols removed

# File lib/canis/core/widgets/table.rb, line 307
def render_data pad, lineno, text
  text = text.join
  # FIXME why repeatedly getting this colorpair
  cp = @color_pair
  att = @attrib
  # added for selection, but will crash if selection is not extended !!! XXX
    if @source.is_row_selected? lineno
      att = REVERSE
      # FIXME currentl this overflows into next row
    end

  FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
  FFI::NCurses.mvwaddstr(pad, lineno, 0, text)
  FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
end
render_header(pad, lineno, col, columns) click to toggle source
# File lib/canis/core/widgets/table.rb, line 323
def render_header pad, lineno, col, columns
  # I could do it once only but if user sets colors midway we can check once whenvever
  # repainting
  # $log.debug "INSIDE render_header XXXXX #{@source.name}, #{lineno}, #{col}"
  check_colors #if @_check_coloring.nil?
  #text = columns.join " | "
  #text = @fmstr % columns
  text = convert_value_to_text columns
  text = text.join
  bg = @header_bgcolor
  fg = @header_color
  att = @header_attrib
  #cp = $datacolor
  cp = get_color($datacolor, fg, bg)
  FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
  FFI::NCurses.mvwaddstr(pad, lineno, col, text)
  FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
end
to_searchable(arr) click to toggle source

return a string representation of the row so that index can be applied to it.

This must take into account columns widths and offsets. This is used by textpad's
next_match method
# File lib/canis/core/widgets/table.rb, line 278
def to_searchable arr
  convert_value_to_text(arr).join
end