Class: Zoom::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/zoom/cache.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(config, file = nil) ⇒ Cache

Returns a new instance of Cache.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/zoom/cache.rb', line 77

def initialize(config, file = nil)
    file = "~/.cache/zoom/cache" if (file.nil?)

    @cache_file = Pathname.new(file).expand_path
    @config = config
    @results = nil
    @thread = nil
    @header = Hash.new

    FileUtils.mkdir_p(@cache_file.dirname)
    read
end

Instance Method Details

#argsObject



6
7
8
9
# File 'lib/zoom/cache.rb', line 6

def args
    return nil if (@header.nil?)
    return @header["args"]
end

#available_tagsObject



11
12
13
# File 'lib/zoom/cache.rb', line 11

def available_tags
    return (1..get_results.length).to_a
end

#clearObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/zoom/cache.rb', line 15

def clear
    # Kill thread first since it was using the file
    @thread.kill if (@thread)
    @thread = nil

    # Delete cache file and reset header and results
    @cache_file.delete if (@cache_file.exist?)
    @header = nil
    @results = nil
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/zoom/cache.rb', line 26

def empty?
    read if (@thread.nil?)
    return true if (@thread.nil?)
    while (@thread.alive? && (@header.nil? || @header.empty?))
        sleep 0.1
    end
    return @header.empty?
end

#get_results(tags = nil) ⇒ Object



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
# File 'lib/zoom/cache.rb', line 35

def get_results(tags = nil)
    if (tags.nil?)
        begin
            @thread.join if (@thread)
        rescue Interrupt
            puts
        end
        return Array.new if (empty?)
        return @results
    end

    results = Array.new
    parse_tags(tags).each do |tag|
        raise Zoom::Error::InvalidTag.new(tag) if (@thread.nil?)

        while ((tag > @results.length) && @thread.alive?)
            sleep 0.1
        end

        if (tag > @results.length)
            raise Zoom::Error::InvalidTag.new(tag)
        end

        results.push(@results.at(tag - 1))
    end

    return results
end

#header(header = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zoom/cache.rb', line 64

def header(header = nil)
    return nil if (header.nil? && empty?)
    return @header if (header.nil?)

    # This causes the cache to be "empty" so don't do it! Leaving
    # this here so I don't forget.
    # @header = header

    File.open(@cache_file, "a") do |f|
        f.write("ZOOM_HEADER=#{JSON.generate(header)}\n")
    end
end

#pathsObject



113
114
115
116
# File 'lib/zoom/cache.rb', line 113

def paths
    return nil if (@header.nil?)
    return @header["paths"]
end

#profile_nameObject



118
119
120
121
# File 'lib/zoom/cache.rb', line 118

def profile_name
    return nil if (@header.nil?)
    return @header["profile_name"]
end

#pwdObject



123
124
125
126
# File 'lib/zoom/cache.rb', line 123

def pwd
    return nil if (@header.nil?)
    return @header["pwd"]
end

#readObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/zoom/cache.rb', line 128

def read
    return if (!@cache_file.exist?)

    @results = Array.new
    tag = 1
    @thread.kill if (@thread)
    @thread = Thread.new do
        profile = nil

        # Read in cache
        File.open(@cache_file) do |cache|
            cache.each do |line|
                line.chomp!
                if (line.start_with?("ZOOM_HEADER="))
                    @header = JSON.parse(
                        line.gsub("ZOOM_HEADER=", "")
                    )

                    if (!@config.has_profile?(profile_name))
                        raise
                        Zoom::Error::ProfileDoesNotExist.new(
                            profile_name
                        )
                    end
                    profile = @config.get_profile(profile_name)
                elsif (line.match(/^-?-?$/))
                    # Ignore dividers when searching with context
                    # and empty lines
                else
                    @results.push(
                        Zoom::Cache::Result.new(
                            tag,
                            line,
                            self,
                            profile.grep_like_tags?
                        )
                    )
                    tag += 1
                end
            end
        end
    end
end

#regexObject



172
173
174
175
# File 'lib/zoom/cache.rb', line 172

def regex
    return nil if (@header.nil?)
    return @header["regex"]
end

#shortcutObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/zoom/cache.rb', line 177

def shortcut
    return if (empty?)

    @config.validate_colors
    if (!@config.has_profile?(profile_name))
        raise Zoom::Error::ProfileDoesNotExist.new(profile_name)
    end

    profile = @config.get_profile(profile_name)
    if (!profile.taggable)
        get_results.each do |result|
            puts(result.contents)
        end
        return
    end

    curr_filename = nil
    get_results.each do |result|
        if (result.grep_like?)
            if (result.filename != curr_filename)
                puts if (curr_filename)
                puts(@config.hilight_filename(result.filename))
                curr_filename = result.filename
            end

            puts(
                [
                    @config.hilight_tag("[#{result.tag}]"),
                    "#{@config.hilight_lineno(result.lineno)}:",
                    result.match.gsub(
                        /(#{regex})/i,
                        @config.hilight_match("\\1")
                    )
                ].join(" ")
            )
        else
            if (result.filename)
                if (result.filename != curr_filename)
                    puts if (curr_filename)
                    puts(
                        @config.hilight_filename(result.filename)
                    )
                    curr_filename = result.filename
                end
            end

            tag = result.tag
            line = result.contents
            puts(
                [
                    @config.hilight_tag("[#{tag}]"),
                    line
                ].join(" ")
            )
        end
    end
end

#write(str, r = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/zoom/cache.rb', line 235

def write(str, r = nil)
    return if (str.nil?)

    if (!str.valid_encoding?)
        str = str.encode(
            "UTF-16be",
            :invalid => :replace,
            :replace => "?"
        ).encode("UTF-8")
    end

    File.open(@cache_file, "a") do |f|
        str.gsub(/\r/, "^M").split("\n").each do |line|
            if (r && line.match(/^[^:]+:[0-9]+:.+$/))
                line.match(/^([^:]+:[0-9]+:)(.+)$/) do |m|
                    m[2].scan(
                        /(.{0,128}#{r}.{0,128})/i
                    ) do |n|
                        f.write("#{m[1]}#{n[0]}\n")
                    end
                end
            else
                line.gsub!(/^(.{128}).*(.{128})$/, "\\1...\\2")
                f.write("#{line}\n")
            end
        end
    end
end