Sha256: b80b2599c9dd09b6419e9ce4ec8517631c25783760de475d9726a3ce0b4975fc

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

require "terminal-table"
require_relative "../labels"

# TODO:
# Shorten very long rows to max. display width or 80 characters

class Ecu
  class ValuePrinter
    def self.call(label)
      Terminal::Table.new(style: { alignment: :right }) do |t|
        case label
        when Kennfeld
          t << truncate(["y\\x"] + label.xvalue)
          t << :separator
          rows = label.yvalue.zip(label.value).map { |h, r| [h] + r }
          display_rows(rows).each do |row|
            t << truncate(row)
          end
        when Kennlinie
          t << truncate(["x"] + label.xvalue)
          t << :separator
          t << truncate(["y"] + label.value)
        when Stuetzstellenverteilung
          t << truncate(["x"] + label.xvalue)
        when Festwerteblock
          label.value.each do |row|
            t << truncate(row)
          end
        else
          fail "Cannot print value for #{label}"
        end
      end.to_s
    end

    def self.display_rows(rows)
      disp = rows.uniq
      return rows unless rows.size > 1 && disp.size < rows.size
      redundant_row = rows[0].map { "" }.tap { |r| r[0] = "..." }
      disp.unshift(redundant_row) if disp[0] == rows[1]
      disp.push(redundant_row) if disp[-1] == rows[-2]
      disp
    end

    def self.truncate(row, maxcols=10)
      return row if row.length <= maxcols
      row[0..(maxcols - 1)] + ["..."]
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
automotive-ecu-0.1.9 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.8 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.7 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.6 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.5 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.4 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.3 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.2 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.1 lib/ecu/labels/value_printer.rb
automotive-ecu-0.1.0 lib/ecu/labels/value_printer.rb