Sha256: e545ae62cd0989e089a020da4b3c7f1be6aa69b90c3d7e50e1285937938d4af5

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 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

      # NOTE: `get_binding` has been stubbed! This version will be ignored
      # See patch for actual version
      prepend Patches::HelpFormatterBinding
      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

9 entries across 9 versions & 1 rubygems

Version Path
commander-openflighthpc-2.0.2 lib/commander/help_formatters.rb
commander-openflighthpc-2.0.1 lib/commander/help_formatters.rb
commander-openflighthpc-2.0.0 lib/commander/help_formatters.rb
commander-openflighthpc-1.2.0 lib/commander/help_formatters.rb
commander-openflighthpc-1.1.2 lib/commander/help_formatters.rb
commander-openflighthpc-1.1.1 lib/commander/help_formatters.rb
commander-openflighthpc-1.1.0 lib/commander/help_formatters.rb
commander-openflighthpc-1.0.0 lib/commander/help_formatters.rb
commander-openflighthpc-1.0.0.pre.alpha1 lib/commander/help_formatters.rb