Class: GrepWish

Inherits:
Djinni::Wish
  • Object
show all
Defined in:
lib/git_xplorer/wish/grep_wish.rb

Instance Method Summary collapse

Instance Method Details

#aliasesObject



4
5
6
# File 'lib/git_xplorer/wish/grep_wish.rb', line 4

def aliases
    return ["grep"]
end

#descriptionObject



8
9
10
# File 'lib/git_xplorer/wish/grep_wish.rb', line 8

def description
    return "Similar to \"git grep -HIinP PATTERN\""
end

#execute(args, djinni_env = Hash.new) ⇒ Object



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
# File 'lib/git_xplorer/wish/grep_wish.rb', line 12

def execute(args, djinni_env = Hash.new)
    gitx = djinni_env["gitXplorer"]
    args.gsub!(/^(["'])(.*)\1$/, "\\2")
    dir = gitx.pwd.gsub(%r{^[^/]+(/|$)}, "")

    if (args.empty?)
        usage
        return
    end

    begin
        gitx.search(dir, args).each do |line|
            m = line.match(/^([^:]+):([^:]+):(\d+):(.*)$/)
            if (m && (m.length == 5))
                puts [
                    m[1].green,
                    m[2].cyan,
                    m[3].white,
                    m[4].gsub(/(#{args})/i, "\\1".black.on_white)
                ].join(":")
            else
                puts line
            end
        end
    rescue GitXplorer::Error => e
        puts e.message
    end
end

#usageObject



41
42
43
44
# File 'lib/git_xplorer/wish/grep_wish.rb', line 41

def usage
    puts "#{aliases.join(", ")} <PATTERN>"
    puts "    #{description}."
end