Sha256: 6df40fc60cdd7d6436a0ec420248f5b21168423533273301d7e4af7a5c6bb10a

Contents?: true

Size: 1.45 KB

Versions: 14

Compression:

Stored size: 1.45 KB

Contents

module Eco
  class CLI
    class Config
      module Help

        def help(*args)
          raise "this needs to be reimplemented in the child class and return a string"
        end

        private

        def keys_max_len(keys)
          keys.max {|a, b| a.length <=> b.length}.length
        end

        # Creatas a well aligned line
        def help_line(key, desc, keys_max_len = key.length, line_len = 100)
          blanks    = keys_max_len + 3 - key.length
          blanks    = blanks < 0 ? 0 : blanks
          top_line  = "  #{key}#{" "*blanks} "
          indent    = top_line.length
          first     = true
          each_slice_words(desc, line_len - indent).each_with_object([]) do |line, lines|
            lines << (first ? "#{top_line}#{line}" : "#{" " * indent}#{line}")
            first = false
          end.join("\n")
        end

        def each_slice_words(str, max_len = 100)
          liner = ""
          str.to_s.scan(/[^\s]+|\s+/).each_with_object([]) do |part, out|
            if "#{liner}#{part}".length <= max_len
              liner << part
            else
              yield(liner) if block_given?
              out << liner
              liner = part.strip
            end
          end.tap do |out|
            if out.empty? || !liner.empty?
              yield(liner) if block_given?
              out << liner if liner
            end
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
eco-helpers-2.7.16 lib/eco/cli/config/help.rb
eco-helpers-2.7.15 lib/eco/cli/config/help.rb
eco-helpers-2.7.14 lib/eco/cli/config/help.rb
eco-helpers-2.7.13 lib/eco/cli/config/help.rb
eco-helpers-2.7.12 lib/eco/cli/config/help.rb
eco-helpers-2.7.4 lib/eco/cli/config/help.rb
eco-helpers-2.7.2 lib/eco/cli/config/help.rb
eco-helpers-2.7.1 lib/eco/cli/config/help.rb
eco-helpers-2.7.0 lib/eco/cli/config/help.rb
eco-helpers-2.6.4 lib/eco/cli/config/help.rb
eco-helpers-2.6.3 lib/eco/cli/config/help.rb
eco-helpers-2.6.2 lib/eco/cli/config/help.rb
eco-helpers-2.6.1 lib/eco/cli/config/help.rb
eco-helpers-2.6.0 lib/eco/cli/config/help.rb