Sha256: 360781e1426d1e275ba85521664c0957dbdece0bdd855b4f0c5bc2bd73cfb004

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

module Troo
  module DecoratorHelpers
    module ClassMethods
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

    private

    def word_wrap(text, line_width = 70)
      return text if line_width <= 0
      text.gsub(/\n/, ' ').gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
    end

    def highlight(value, options = {})
      if options.fetch(:ansicolor, true)
        [ options.fetch(:colour, nil),
          options.fetch(:underline, nil),
          value,
          Esc.reset ].join
      else
        value
      end
    end

    def brackets(value)
      "(#{value})"
    end

    def label(string)
      highlight(string, { ansicolor: true,
                          colour:    Esc.yellow,
                          underline: Esc.underline })
    end

    def member(string)
      highlight(string, { ansicolor: true,
                          colour:    Esc.magenta,
                          underline: Esc.underline })
    end

    def metadata(label, string)
      [ highlight(label.rjust(9), { ansicolor: true,
                                    colour:    Esc.cyan,
                                    underline: nil }),
        string ].join(" ")
    end

    def spacing(options = {}, &block)
      puts if options.fetch(:head, true)
      yield
      puts if options.fetch(:foot, true)
    end

    def indent(&block)
      @indent ||= 0
      @indent += 4
      print indentation
      yield
    ensure
      @indent -= 4
    end

    def indentation
      ' ' * @indent
    end

    def print_error(message)
      indent do
        print [(" " * 2), [Esc.red, message, Esc.reset].join].join(" ") + "\n"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
troo-0.0.4 lib/troo/display/decorator_helpers.rb
troo-0.0.3 lib/troo/display/decorator_helpers.rb
troo-0.0.2 lib/troo/display/decorator_helpers.rb