Class: Zoom::Cache

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

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Cache

Returns a new instance of Cache.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zoom/cache.rb', line 72

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

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

    FileUtils.mkdir_p(@cache_file.dirname)
    read
end

Instance Attribute Details

#header(header = nil) ⇒ Object (readonly)

Returns the value of attribute header.



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

def header
  @header
end

Instance Method Details

#args ⇒ Object



8
9
10
11
# File 'lib/zoom/cache.rb', line 8

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

#available_tags ⇒ Object



13
14
15
# File 'lib/zoom/cache.rb', line 13

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

#clear ⇒ Object



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

def clear
    @cache_file.delete if (@cache_file.exist?)
    @results = nil
    @thread.kill if (@thread)
    @thread = nil
    @header = nil
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#paths ⇒ Object



105
106
107
108
# File 'lib/zoom/cache.rb', line 105

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

#pattern ⇒ Object



110
111
112
113
# File 'lib/zoom/cache.rb', line 110

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

#profile_name ⇒ Object



115
116
117
118
# File 'lib/zoom/cache.rb', line 115

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

#pwd ⇒ Object



120
121
122
123
# File 'lib/zoom/cache.rb', line 120

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

#read ⇒ Object



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

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

    @results = Array.new
    tag = 1
    @thread.kill if (@thread)
    @thread = Thread.new do
        # 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=", "")
                    )
                elsif (line.match(/^-?-?$/))
                    # Ignore dividers when searching with context
                    # and empty lines
                else
                    @results.push(
                        Zoom::Cache::Result.new(tag, line, self)
                    )
                    tag += 1
                end
            end
        end
    end
end

#shortcut(config) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/zoom/cache.rb', line 154

def shortcut(config)
    return if (empty?)

    config.validate_colors
    if (!config.has_profile?(profile_name))
        raise Zoom::Error::ProfileDoesNotExists.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(
                    /(#{pattern})/i,
                    config.hilight_match("\\1")
                )
            ].join(" ")
        else
            tag = result.tag
            line = result.contents
            puts [config.hilight_tag("[#{tag}]"), line].join(" ")
        end
    end
end

#write(str) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/zoom/cache.rb', line 195

def write(str)
    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|
        f.write(str.gsub(/\r/, "^M"))
    end
end