Sha256: e958d0e480d9845888e31f45623be53e016d684d64b565c9a7ede59bc97d86ee

Contents?: true

Size: 1.48 KB

Versions: 31

Compression:

Stored size: 1.48 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].max
          top_line  = "  #{key}#{" "*blanks} "
          indent    = top_line.length
          first     = true

          each_slice_words(desc, line_len - indent).each_with_object([]) do |line, lines| # rubocop:disable Style/RedundantEach
            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|
            next unless out.empty? || !liner.empty?

            yield(liner) if block_given?

            out << liner if liner
          end
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/cli/config/help.rb
eco-helpers-3.0.20 lib/eco/cli/config/help.rb
eco-helpers-3.0.19 lib/eco/cli/config/help.rb
eco-helpers-3.0.18 lib/eco/cli/config/help.rb
eco-helpers-3.0.17 lib/eco/cli/config/help.rb
eco-helpers-3.0.16 lib/eco/cli/config/help.rb
eco-helpers-3.0.15 lib/eco/cli/config/help.rb
eco-helpers-3.0.14 lib/eco/cli/config/help.rb
eco-helpers-3.0.13 lib/eco/cli/config/help.rb
eco-helpers-3.0.12 lib/eco/cli/config/help.rb
eco-helpers-3.0.11 lib/eco/cli/config/help.rb
eco-helpers-3.0.10 lib/eco/cli/config/help.rb
eco-helpers-3.0.9 lib/eco/cli/config/help.rb
eco-helpers-3.0.8 lib/eco/cli/config/help.rb
eco-helpers-3.0.7 lib/eco/cli/config/help.rb
eco-helpers-3.0.6 lib/eco/cli/config/help.rb
eco-helpers-3.0.5 lib/eco/cli/config/help.rb
eco-helpers-3.0.4 lib/eco/cli/config/help.rb
eco-helpers-3.0.3 lib/eco/cli/config/help.rb
eco-helpers-3.0.2 lib/eco/cli/config/help.rb