class KXI::CLI::PropertyList
Property list renderer
Public Class Methods
new()
click to toggle source
Instantiates the {KXI::CLI::PropertyList} class
# File lib/kxi/cli/property_list.rb, line 8 def initialize @align = 0 @data = {} end
Public Instance Methods
field(name, value)
click to toggle source
Sets a property @param [string] name Name of the property @param [string,Array<string>] value Value of the property
# File lib/kxi/cli/property_list.rb, line 16 def field(name, value) @data[name] = value @align = name.length if name.length > @align end
render(cols = 4)
click to toggle source
Renders the property list into stdout @param [integer] cols Determines the maximal number of columns for array rendering
# File lib/kxi/cli/property_list.rb, line 23 def render(cols = 4) @data.each_pair do |k, v| if v.kind_of?(Array) print("#{pad(k)}: ") v.each_index do |idx| if idx > 0 and idx % cols == 0 puts('') print(' ' * (@align + 2)) elsif idx > 0 print(', ') end print(v[idx]) end puts('') else puts("#{pad(k)}: #{v}") end end end
Private Instance Methods
pad(key)
click to toggle source
Adds a space-dot-space padding @param [string] key Key to add padding to
# File lib/kxi/cli/property_list.rb, line 45 def pad(key) d = false p = '' (@align - key.length).times do |i| p = (d ? '.' : ' ') + p d = (not d) end return key + p end