Class: Canis::DefaultTableRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/canis/core/widgets/table.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ DefaultTableRenderer

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



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# 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

Instance Method Details

#check_colorsObject

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



342
343
344
345
346
347
348
349
350
# File 'lib/canis/core/widgets/table.rb', line 342

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) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/canis/core/widgets/table.rb', line 357

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) ⇒ Object

set column model (Table Renderer)



236
237
238
# File 'lib/canis/core/widgets/table.rb', line 236

def column_model c
  @chash = c
end

#content_attrib(att) ⇒ Object



232
233
234
# File 'lib/canis/core/widgets/table.rb', line 232

def content_attrib att
  @attrib = att
end

#content_colors(fg, bg) ⇒ Object

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



227
228
229
230
231
# 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) ⇒ Object

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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# 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_columnObject



351
352
353
354
355
356
# File 'lib/canis/core/widgets/table.rb', line 351

def each_column
  @chash.each_with_index { |c, i| 
    next if c.hidden
    yield c,i if block_given?
  }
end

#header_attrib(att) ⇒ Object



223
224
225
# File 'lib/canis/core/widgets/table.rb', line 223

def header_attrib att
  @header_attrib = att
end

#header_colors(fg, bg) ⇒ Object



219
220
221
222
# 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) ⇒ Object

Parameters:

  • pad

    for calling print methods on

  • lineno

    the line number on the pad to print on

  • data (String)

    to print which will be an array (@list)



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# 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) ⇒ Object

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.

Parameters:

  • text

    is an array of strings, in the order of actual printing with hidden cols removed



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# 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) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# 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
  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) ⇒ Object

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


278
279
280
# File 'lib/canis/core/widgets/table.rb', line 278

def to_searchable arr
  convert_value_to_text(arr).join
end