Sha256: 0a0e3584be86bb9e0340e8b5225686958dc82302300374495aea78f445507ec7

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

module SimpleCommander
  module HelpFormatter
    autoload :Base, 'simple_commander/help_formatters/base'
    autoload :Terminal, 'simple_commander/help_formatters/terminal'
    autoload :TerminalCompact, 'simple_commander/help_formatters/terminal_compact'

    class Context
      def initialize(target)
        @target = target
      end

      def get_binding
        @target.instance_eval { binding }.tap do |bind|
          decorate_binding(bind)
        end
      end

      # No-op, override in subclasses.
      def decorate_binding(_bind)
      end
    end

    class ProgramContext < Context
      def decorate_binding(bind)
        bind.eval("max_command_length = #{max_command_length(bind)}")
        bind.eval("max_aliases_length = #{max_aliases_length(bind)}")
      end

      def max_command_length(bind)
        max_key_length(bind.eval('@commands'))
      end

      def max_aliases_length(bind)
        max_key_length(bind.eval('@aliases'))
      end

      def max_key_length(hash, default = 20)
        longest = hash.keys.max_by(&:size)
        longest ? longest.size : default
      end
    end

    module_function

    def indent(amount, text)
      text.to_s.gsub("\n", "\n" + (' ' * amount))
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
simple_commander-0.6.1 lib/simple_commander/help_formatters.rb
simple_commander-0.6.0 lib/simple_commander/help_formatters.rb
simple_commander-0.5.1 lib/simple_commander/help_formatters.rb
simple_commander-0.5.0 lib/simple_commander/help_formatters.rb
simple_commander-0.4.0 lib/simple_commander/help_formatters.rb
simple_commander-0.3.1 lib/simple_commander/help_formatters.rb
simple_commander-0.3.0 lib/simple_commander/help_formatters.rb
simple_commander-0.1.0 lib/simple_commander/help_formatters.rb