Sha256: 6bca3696649ce38372ce4331a973bb9a71483063554329de84051f7a8e0f1f8d

Contents?: true

Size: 1.47 KB

Versions: 123

Compression:

Stored size: 1.47 KB

Contents

module CF
  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 unless quiet?
      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

123 entries across 123 versions & 2 rubygems

Version Path
cf-5.4.7 lib/cf/spacing.rb
cf-5.4.7.rc1 lib/cf/spacing.rb
cf-5.4.5 lib/cf/spacing.rb
cf-5.4.4 lib/cf/spacing.rb
trucker-cli-0.0.3 lib/cf/spacing.rb
cf-5.4.3 lib/cf/spacing.rb
cf-5.4.2 lib/cf/spacing.rb
trucker-cli-0.0.2 lib/cf/spacing.rb
trucker-cli-0.0.1 lib/cf/spacing.rb
cf-5.4.1 lib/cf/spacing.rb
cf-5.4.1.rc1 lib/cf/spacing.rb
cf-5.4.0 lib/cf/spacing.rb
cf-5.3.1 lib/cf/spacing.rb
cf-5.3.0 lib/cf/spacing.rb
cf-5.2.2 lib/cf/spacing.rb
cf-5.2.1.rc15 lib/cf/spacing.rb
cf-5.2.1.rc14 lib/cf/spacing.rb
cf-5.2.1.rc13 lib/cf/spacing.rb
cf-5.2.1.rc12 lib/cf/spacing.rb
cf-5.2.1.rc11 lib/cf/spacing.rb