Class: Djinni::Wish::Help

Inherits:
Djinni::Wish show all
Defined in:
lib/djinni/wish/help.rb

Instance Method Summary collapse

Instance Method Details

#aliasesObject



2
3
4
# File 'lib/djinni/wish/help.rb', line 2

def aliases
    return ["?", "help"]
end

#descriptionObject



6
7
8
# File 'lib/djinni/wish/help.rb', line 6

def description
    return "Show helpful information for a command or commands"
end

#execute(args, djinni_env = {}) ⇒ Object



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
# File 'lib/djinni/wish/help.rb', line 10

def execute(args, djinni_env = {})
    wishes = djinni_env["djinni_wishes"]
    max = wishes.keys.max_by(&:length).length
    if (args.empty?)
        fill = Array.new(max - 7 + 4, " ").join
        puts "COMMAND#{fill}DESCRIPTION"
        puts "-------#{fill}-----------"
        wishes.sort do |a, b|
            a.first.downcase <=> b.first.downcase
        end.each do |aliaz, wish|
            fill = Array.new(max - aliaz.length + 4, " ").join
            puts "#{aliaz}#{fill}#{wish.description}"
        end
    elsif (args.split(" ").length == 1)
        wishes.sort do |a, b|
            a.first.downcase <=> b.first.downcase
        end.each do |aliaz, wish|
            if (aliaz == args)
                wish.usage
                return
            end
        end
        puts "Command #{args} not found!"
    else
        usage
    end
end

#tab_complete(input, djinni_env = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/djinni/wish/help.rb', line 38

def tab_complete(input, djinni_env = {})
    return [{}, "", " "] if (input.include?(" "))

    completions = Hash.new
    djinni_env["djinni_wishes"].select do |aliaz, wish|
        aliaz.downcase.start_with?(input.downcase)
    end.sort do |a, b|
        a.first.downcase <=> b.first.downcase
    end.each do |aliaz, wish|
        completions[aliaz] = wish.description
    end

    return [completions, input, " "]
end

#usageObject



53
54
55
56
57
# File 'lib/djinni/wish/help.rb', line 53

def usage
    puts "help [command]"
    puts "    Print usage for specified command. If no command is"
    puts "    specified, print description of all commands."
end