lib/rbcurse/experimental/widgets/tablewidget.rb in rbcurse-experimental-0.0.2 vs lib/rbcurse/experimental/widgets/tablewidget.rb in rbcurse-experimental-0.0.3

- old
+ new

@@ -3,11 +3,11 @@ # File: tablewidget.rb # Description: A tabular widget based on textpad # Author: rkumar http://github.com/rkumar/rbcurse/ # Date: 2013-03-29 - 20:07 # License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt) -# Last update: 2013-04-12 13:04 +# Last update: 2013-04-23 16:33 # ----------------------------------------------------------------------------- # # tablewidget.rb Copyright (C) 2012-2013 rahul kumar require 'logger' require 'rbcurse' @@ -118,13 +118,36 @@ res = 0 @sort_keys.each { |ee| e = ee.abs-1 # since we had offsetted by 1 earlier abse = e.abs if ee < 0 + xx = x[abse] + yy = y[abse] + # the following checks are since nil values cause an error to be raised + if xx.nil? && yy.nil? + res = 0 + elsif xx.nil? + res = 1 + elsif yy.nil? + res = -1 + else res = y[abse] <=> x[abse] + end else + xx = x[e] + yy = y[e] + # the following checks are since nil values cause an error to be raised + # whereas we want a nil to be wither treated as a zero or a blank + if xx.nil? && yy.nil? + res = 0 + elsif xx.nil? + res = -1 + elsif yy.nil? + res = 1 + else res = x[e] <=> y[e] + end end break if res != 0 } res } @@ -338,11 +361,11 @@ # Making a pad of the content but not column header complicates stuff, # do we make a pad of that, or print it like the old thing. class TableWidget < TextPad dsl_accessor :print_footer - attr_reader :columns + #attr_reader :columns attr_accessor :table_row_sorter def initialize form = nil, config={}, &block # hash of column info objects, for some reason a hash and not an array @@ -519,9 +542,16 @@ # column titles as first row. _init_model array end alias :headings= :columns= + # returns array of column names as Strings + def columns + @content[0] + end + + # size each column based on widths of this row of data. + # Only changed width if no width for that column def _init_model array array.each_with_index { |c,i| # if columns added later we could be overwriting the width c = get_column(i) c.width ||= 10