Class: CopyWish

Inherits:
Djinni::Wish
  • Object
show all
Defined in:
lib/rubeepass/wish/copy_wish.rb

Instance Method Summary collapse

Constructor Details

#initializeCopyWish

Returns a new instance of CopyWish.



73
74
75
# File 'lib/rubeepass/wish/copy_wish.rb', line 73

def initialize
    @fields = [ "pass", "url", "user" ]
end

Instance Method Details

#aliasesObject



5
6
7
# File 'lib/rubeepass/wish/copy_wish.rb', line 5

def aliases
    return [ "copy", "cp" ]
end

#descriptionObject



9
10
11
# File 'lib/rubeepass/wish/copy_wish.rb', line 9

def description
    return "Copy specified field to the clipboard"
end

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



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
63
64
65
66
67
68
69
70
71
# File 'lib/rubeepass/wish/copy_wish.rb', line 13

def execute(args, djinni_env = {})
    if (args.nil? || args.empty?)
        puts usage
        return
    end

    field, args = args.split(" ", 2)
    if (!@fields.include?(field))
        puts usage
        return
    end

    if (ENV["DISPLAY"].nil? || ENV["DISPLAY"].empty?)
        puts "DISPLAY not set!"
        return
    end

    keepass = djinni_env["keepass"]
    cwd = djinni_env["cwd"]
    args = cwd.path if (args.nil? || args.empty?)

    args = keepass.absolute_path(args, cwd.path)
    path, target = args.rsplit("/")
    new_cwd = keepass.find_group(path)

    if (new_cwd)
        if (target.empty?)
            usage
        elsif (new_cwd.has_entry?(target))
            target = new_cwd.entry_titles.select do |entry|
                target.downcase == entry.downcase
            end.first

            timeout = djinni_env["clipboard_timeout"]

            case field
            when "pass"
                new_cwd.entries[target].copy_password_to_clipboard
                keepass.send(
                    "clear_clipboard_after_#{timeout}_seconds"
                )
            when "url"
                new_cwd.entries[target].copy_url_to_clipboard
                keepass.send(
                    "clear_clipboard_after_#{timeout}_seconds"
                )
            when "user"
                new_cwd.entries[target].copy_username_to_clipboard
                keepass.send(
                    "clear_clipboard_after_#{timeout}_seconds"
                )
            end
        else
            puts "Entry \"#{args}\" doesn't exist!"
        end
    else
        puts "Entry \"#{args}\" doesn't exist!"
    end
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rubeepass/wish/copy_wish.rb', line 77

def tab_complete(input, djinni_env = {})
    if (input.nil? || input.empty?)
        puts
        puts @fields
        return ""
    end

    field, input = input.split(" ", 2)

    if (input.nil? || input.empty?)
        @fields.each do |f|
            break if (f == field)
            if (f.start_with?(field))
                return "#{f} "
            end
        end
    end

    input = "" if (input.nil?)

    cwd = djinni_env["cwd"]
    input, groups, entries = cwd.fuzzy_find(input)
    if (groups.empty? && entries.empty?)
        return "#{field} #{input.gsub(%r{^#{cwd.path}/?}, "")}"
    end

    path, target = input.rsplit("/")

    if (target.empty?)
        if ((groups.length == 1) && entries.empty?)
            input = "#{path}/#{groups.first}/"
            return "#{field} #{input.gsub(%r{^#{cwd.path}/?}, "")}"
        elsif (groups.empty? && (entries.length == 1))
            input = "#{path}/#{entries.first}"
            return "#{field} #{input.gsub(%r{^#{cwd.path}/?}, "")}"
        end
        puts
        groups.each do |group|
            puts "#{group}/"
        end
        puts entries
        return "#{field} #{input.gsub(%r{^#{cwd.path}/?}, "")}"
    end

    if (!groups.empty?)
        input = "#{path}/#{groups.first}/"
    elsif (!entries.empty?)
        input = "#{path}/#{entries.first}"
    end

    return "#{field} #{input.gsub(%r{^#{cwd.path}/?}, "")}"
end

#usageObject



130
131
132
133
134
135
136
137
138
# File 'lib/rubeepass/wish/copy_wish.rb', line 130

def usage
    puts "#{aliases.join(", ")} <field> <entry>"
    puts "\t#{description}."
    puts
    puts "FIELDS"
    @fields.each do |field|
        puts "\t#{field}"
    end
end