Sha256: 57903a869877eb0941ff29195ba99d48b7ba5adfce4d0507fcb81f434b7e4568

Contents?: true

Size: 1.18 KB

Versions: 18

Compression:

Stored size: 1.18 KB

Contents

module Commander
  module HelpFormatter
    autoload :Base, 'commander/help_formatters/base'
    autoload :Terminal, 'commander/help_formatters/terminal'
    autoload :TerminalCompact, '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

18 entries across 18 versions & 3 rubygems

Version Path
commander-4.5.2 lib/commander/help_formatters.rb
commander-4.5.1 lib/commander/help_formatters.rb
commander-4.5.0 lib/commander/help_formatters.rb
commander-4.4.7 lib/commander/help_formatters.rb
commander-4.4.6 lib/commander/help_formatters.rb
commander-4.4.5 lib/commander/help_formatters.rb
commander-fastlane-4.4.6 lib/commander/help_formatters.rb
commander-4.4.4 lib/commander/help_formatters.rb
commander-fastlane-4.4.5 lib/commander/help_formatters.rb
commander-fastlane-4.4.4 lib/commander/help_formatters.rb
commander-fastlane-4.4.3 lib/commander/help_formatters.rb
commander-4.4.3 lib/commander/help_formatters.rb
commander-4.4.2 lib/commander/help_formatters.rb
commander-4.4.1 lib/commander/help_formatters.rb
simple_commander-0.0.1 lib/simple_commander/help_formatters.rb
commander-4.4.0 lib/commander/help_formatters.rb
commander-4.3.8 lib/commander/help_formatters.rb
commander-4.3.7 lib/commander/help_formatters.rb