class OutputPrinter

Constants

NILCHAR

Public Class Methods

new(interactive=true) click to toggle source

Class initializer Interactive mode will not be implemented for a while so the variable is more of a place holder for now. Interactive

# File libs/printer.rb, line 58
def initialize(interactive=true)
  @swidth=80                  # screen width
  @interactive=interactive    # mode for output
  @lines=0                    # how many lines have been displayed thus far?

  # Check the environment variables to see if screen height has been set
  EnvVars.instance["lines"]=24 if EnvVars.instance["lines"].nil?
end

Public Instance Methods

array_width(item) click to toggle source
# File libs/printer.rb, line 76
def array_width(item)
  w=0
  item.each do |value|
    w+=value.length + 2  # 2 is for ", "
  end
  w-=2  # remove last comma and space
  return w
end
format_for_print(item) click to toggle source
# File libs/printer.rb, line 153
def format_for_print(item)
  if item.nil? || item==[]
    return NILCHAR
  else
    case item.class.to_s
      when "Hash"
        return format_hash_for_print(item)
      else
        return item
    end
  end
end
format_hash_for_print(item) click to toggle source
# File libs/printer.rb, line 144
def format_hash_for_print(item)
  s = ""
  item.each do |value, index|
    s << ", " if !s.empty?
    s << index << " => " << value
  end
  return s
end
getcolwidth(dataset,headers=nil) click to toggle source

determines the max col width for each colum may need to be optimized in the future possible optimization may include randomly iterating through list for large lists if dataset is an array headers is ignored, also an integer is returned not an array of widths

# File libs/printer.rb, line 111
def getcolwidth(dataset,headers=nil)
  if dataset.class==Array then
    widths=headers.collect {|x| 0}  # setup our resultant array of widths

    # check the widths for the headers
    headers.each_with_index { |value, index| widths[index] = value.length }

    if (dataset.length>0) and (dataset[0].class!=Hash) then
      width=0
      dataset.each do |item|
        w=getitemwidth(item)
        widths[0] = widths[0]<w ? w : widths[0]    # 0 because there's one column
      end
      return widths
    elsif dataset[0].class==Hash then
      raise "getcolwidth headers are nil" if headers.nil?
      dataset.each do |row|
        headers.each_with_index do |value, index|
          width=getitemwidth(row[value])
          val= widths[index]  # storing value for efficiency, next statement might have two of this call
          widths[index]= val < width ? width : val
        end
      end

      return widths
    else
      raise "getcolwidth Unknown internal data type"
    end
  else
    raise "getcolwidth - dataset type not supported: #{dataset.class}"  # need to raise an error
  end
end
getitemwidth(item) click to toggle source
# File libs/printer.rb, line 85
def getitemwidth(item)
  retval=0
  return NILCHAR.length if item.nil?
  case item.class.to_s
    when "String"
      retval=item.length
    when "Fixnum"
      retval=item.to_s.length
    when "Float"
      retval=item.to_s.length
    when "Hash"
      retval=hash_width(item)
    when "Array"
      retval=array_width(item)
    else
      p item
      raise "getitemwidth - item.class: #{item.class} not supported"
  end
  retval
end
hash_width(item) click to toggle source
# File libs/printer.rb, line 67
def hash_width(item)
  w=0
  item.each do |value, index|
    w+= value.length + index.length + 6   # 6 is for " => " and ", "
  end
  w-=2  # subtract out last comma and space
  return w
end
pause?(lines=1) click to toggle source

Pause output function This function will pause output after n lines have been printed n is defined by the lines parameter If interactive output has been disabled pause will not stop after n lines have been printed If @lines is set to -1 a side effect is created where pause is disabled.

# File libs/printer.rb, line 172
  def pause? (lines=1)
    if @interactive and EnvVars.instance["lines"]>0 and (@lines>-1) then
      @lines += lines
      if @lines>=(EnvVars.instance["lines"]-1) then
        pause_msg = "Pause, q to quit, a to stop pausing output"
        Kernel.print pause_msg
        if RUBY_PLATFORM =~ /.*?mswin.*?/
          while kbhit==0
            sleep 0.3
#            putc('.')
          end
          chr=getch
          puts chr
        else
          begin
            chr=get_character
          rescue Interrupt  # trap ctrl-c and create side effect to behave like "q" was pressed
            chr=113
          end

          # erase characters on the current line, and move the cursor left the size of pause_msg
          Kernel.print "\033[2K\033[#{pause_msg.length}D"

          if (chr==113) or (chr==81) then  # 113="q"  81="Q"
            raise "quit"
          end
          if (chr==65) or (chr==97) then # 65="A"  97="a
            @lines=-1
          end
        end
        @lines= (@lines==-1) ? -1:0  # if we set @lines to -1 make sure the side effect propagates
      end
    end
  end
print(dataset,cols) click to toggle source
print_array(dataset,cols) click to toggle source
print_hash(dataset,cols) click to toggle source
printheader(header, widths=nil, separator=",") click to toggle source

Prints the table header header: Array of strings in the print order for the table header order: Array of strings denoting output order widths: (optional) Array of numbers denoting the width of each field separator: (optional) Separator character

# File libs/printer.rb, line 219
def printheader(header, widths=nil, separator=",")
  if widths.nil?
    output=""
    header.each do |value|
      output+="#{value}#{separator}"
    end
    separator.length.times {output.chop!}
  else
    output="|"
    header.each_with_index do |value, index|
      output+=" %-#{widths[index]}s |" % value
    end
  end
  puts output
  pause? 1
end
printline(widths) click to toggle source
# File libs/printer.rb, line 207
def printline(widths)
  output="+"
  widths.each { |width| output+="-"+("-"*width)+"-+" }
  puts output
  pause? 1
end
printrow(row, order, widths=nil, separator=',') click to toggle source

Requires 2 arguments and 2 optional arguments row: The Row of data order: An array of field names with the order in which they are to be printed Optional arguments widths: An array denoting the width of each field, if nul a table separated by separator will be printed separator: the separator character to be used

# File libs/printer.rb, line 242
def printrow(row, order, widths=nil, separator=',')
  if widths.nil?
    output=""
    order.each_with_index do |value, index|
      output+="#{row[value]}#{separator}"
    end
    separator.length.times { output.chop! }  #remove the last separator
    puts output
  else
    output="|"
    order.each_with_index do |value, index|
      output+=" %-#{widths[index]}s |" % format_for_print(row[value])
    end
    puts output
  end
  pause? 1
end