Class: Origen::Clocks::ClocksCollection

Inherits:
Hash show all
Defined in:
lib/origen/clocks/clocks_collection.rb

Defined Under Namespace

Classes: ConsoleValue

Instance Method Summary collapse

Methods inherited from Hash

#filter, #ids, #intersect?, #intersections, #longest_key, #longest_value, #recursive_find_by_key, #update_common

Instance Method Details

#inspect(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/origen/clocks/clocks_collection.rb', line 7

def inspect(options = {})
  options = {
    fancy_output: true
  }.update(options)
  headers = []
  output_clock_list = []
  column_widths = {}.tap do |colhash|
    each do |clk_name, clk|
      output_attr_list = {}
      clk.instance_variables.each do |attr|
        value_color = :default
        attr_getter = attr.to_s[/\@(\S+)/, 1].to_sym
        attr_val = clk.send attr_getter
        next unless [String, Numeric, Float, Integer, Symbol, Range].include? attr_val.class

        headers << attr_getter unless headers.include?(attr_getter)
        str = case attr_val
        when Numeric
          value_color = :red unless clk.setpoint_ok?(attr_val)
          attr_val.as_Hz
        when Range
          start_frequency = attr_val.first
          end_frequency = attr_val.last
          "#{start_frequency.as_Hz}\.\.#{end_frequency.as_Hz}"
        else
          attr_val.to_s
              end
        curr_longest = [attr_getter, str].max_by(&:length).size + 2 # Add 2 for the whitespace
        if colhash[attr].nil? || (colhash[attr] < curr_longest)
          colhash[attr] = curr_longest
        end
        output_attr_list[attr_getter] = ConsoleValue.new(str, value_color)
      end
      output_clock_list << output_attr_list
    end
  end
  # Need to loop through the clock table values and find nils
  # and create ConsoleValue instances for them
  updated_clock_list = [].tap do |clk_ary|
    output_clock_list.each do |attrs|
      temp_hash = {}.tap do |tmphash|
        headers.each do |h|
          if attrs[h].nil?
            tmphash[h] = ConsoleValue.new('', :default)
          else
            tmphash[h] = ConsoleValue.new(attrs[h].value, attrs[h].color)
          end
        end
      end
      clk_ary << temp_hash
    end
  end
  if options[:fancy_output]
    puts '' + column_widths.values.each.map { |i| '' * i }.join('') + ''
    puts '' + headers.each_with_index.map { |col_val, i| " #{col_val} ".ljust(column_widths.values[i]) }.join('') + ''
    puts '' + column_widths.values.each.map { |i| '' * i }.join('') + ''
    puts updated_clock_list.each.map { |attributes| '' + headers.each_with_index.map { |value, attr_idx| attributes[value].color == :default ? " #{attributes[value].value} ".ljust(column_widths.values[attr_idx]) : " #{attributes[value].value} ".ljust(column_widths.values[attr_idx]).colorize(:red) }.join('') + '' }
    puts '' + column_widths.values.each.map { |i| '' * i }.join('') + ''
  else
    puts '.' + column_widths.values.each.map { |i| '-' * i }.join('-') + '.'
    puts '|' + headers.each_with_index.map { |col_val, i| " #{col_val} ".ljust(column_widths.values[i]) }.join('|') + '|'
    puts '|' + column_widths.values.each.map { |i| '-' * i }.join('+') + '|'
    puts updated_clock_list.each.map { |attributes| '|' + headers.each_with_index.map { |value, attr_idx| attributes[value].color == :default ? " #{attributes[value]} ".ljust(column_widths.values[attr_idx]) : " #{attributes[value]} ".ljust(column_widths.values[attr_idx]).colorize(:red) }.join('|') + '|' }
    puts '`' + column_widths.values.each.map { |i| '-' * i }.join('-') + '\''
  end
end