File: help.rb

package info (click to toggle)
ruby-spring 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 364 kB
  • ctags: 429
  • sloc: ruby: 2,762; makefile: 6
file content (62 lines) | stat: -rw-r--r-- 1,584 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require "spring/version"

module Spring
  module Client
    class Help < Command
      attr_reader :spring_commands, :application_commands

      def self.description
        "Print available commands."
      end

      def self.call(args)
        require "spring/commands"
        super
      end

      def initialize(args, spring_commands = nil, application_commands = nil)
        super args

        @spring_commands      = spring_commands      || Spring::Client::COMMANDS.dup
        @application_commands = application_commands || Spring.commands.dup

        @spring_commands.delete_if { |k, v| k.start_with?("-") }

        @application_commands["rails"] = @spring_commands.delete("rails")
      end

      def call
        puts formatted_help
      end

      def formatted_help
        ["Version: #{env.version}\n",
         "Usage: spring COMMAND [ARGS]\n",
         *command_help("spring itself", spring_commands),
         '',
         *command_help("your application", application_commands)].join("\n")
      end

      def command_help(subject, commands)
        ["Commands for #{subject}:\n",
        *commands.sort_by(&:first).map { |name, command| display(name, command) }.compact]
      end

      private

      def all_commands
        spring_commands.merge application_commands
      end

      def display(name, command)
        if command.description
          "  #{name.ljust(max_name_width)}  #{command.description}"
        end
      end

      def max_name_width
        @max_name_width ||= all_commands.keys.map(&:length).max
      end
    end
  end
end