Sha256: f4453180e2b087c18f1c4a32404d6766537f6fbdd982c08c0a03b466a592bc90

Contents?: true

Size: 1.45 KB

Versions: 13

Compression:

Stored size: 1.45 KB

Contents

module VMC
  module Spacing
    @@indentation = 0

    def indented
      @@indentation += 1
      yield
    ensure
      @@indentation -= 1
    end

    def line(msg = "")
      return puts "" if msg.empty?

      start_line(msg)
      puts ""
    end

    def start_line(msg)
      print "  " * @@indentation
      print msg
    end

    def lines(blob)
      blob.each_line do |line|
        start_line(line)
      end

      line
    end

    def quiet?
      false
    end

    def spaced(vals)
      num = 0
      vals.each do |val|
        line unless quiet? || num == 0
        yield val
        num += 1
      end
    end

    def tabular(*rows)
      spacings = []
      rows.each do |row|
        next unless row

        row.each.with_index do |col, i|
          next unless col

          width = text_width(col)

          if !spacings[i] || width > spacings[i]
            spacings[i] = width
          end
        end
      end

      columns = spacings.size
      rows.each do |row|
        next unless row

        row.each.with_index do |col, i|
          next unless col

          start_line justify(col, spacings[i])
          print "   " unless i + 1 == columns
        end

        line
      end
    end

    def trim_escapes(str)
      str.gsub(/\e\[\d+m/, "")
    end

    def text_width(str)
      trim_escapes(str).size
    end

    def justify(str, width)
      trimmed = trim_escapes(str)
      str.ljust(width + (str.size - trimmed.size))
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
vmc-0.4.0.beta.66 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.65 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.64 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.63 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.62 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.61 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.60 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.59 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.58 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.57 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.56 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.55 vmc-ng/lib/vmc/spacing.rb
vmc-0.4.0.beta.54 vmc-ng/lib/vmc/spacing.rb