Class: Djinni::Wish::History

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

Instance Method Summary collapse

Instance Method Details

#aliasesObject



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

def aliases
    return ["hist", "history"]
end

#descriptionObject



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

def description
    return "Show history or execute commands from history"
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
37
38
39
40
41
42
43
44
45
46
# File 'lib/djinni/wish/history.rb', line 10

def execute(args, djinni_env = {})
    djinni = djinni_env["djinni"]
    history = djinni_env["djinni_history"]

    if (args.empty?)
        history.each_with_index do |hist, index|
            puts "#{index}:    #{hist}"
        end
        return
    end

    args.split(" ").each do |arg|
        case arg
        when "clear"
            # Do nothing
        when /^[0-9]+$/
            index = arg.to_i
            if ((index < 0) || (index >= history.length))
                puts "Index out of bounds; #{index}"
            end
        else
            usage
            return
        end
    end

    args.split(" ").each do |arg|
        case arg
        when "clear"
            history.clear
        when /^[0-9]+$/
            index = arg.to_i
            print "\e[F"
            djinni.grant_wish("#{history[index]}\n", djinni_env)
        end
    end
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/djinni/wish/history.rb', line 48

def tab_complete(input, djinni_env = {})
    history = djinni_env["djinni_history"]
    input, found, last = input.rpartition(" ")
    included = input.split(" ")

    completions = Hash.new
    (0...history.length).each do |i|
        completions[i.to_s] = history[i]
    end
    completions["clear"] = "Clear history"

    completions.keep_if do |item, desc|
        !included.include?(item)
    end

    if (!last.empty?)
        completions.keep_if do |item, desc|
            item.downcase.start_with?(last.downcase)
        end
    end

    return [completions, last, " "]
end

#usageObject



72
73
74
75
76
77
78
# File 'lib/djinni/wish/history.rb', line 72

def usage
    puts "history [option]"
    puts "    #{description}."
    puts "    OPTIONS"
    puts "        [0-9]+    Execute command from history"
    puts "        clear     Clear history"
end