Class: Zoom::Cache
- Inherits:
-
Object
- Object
- Zoom::Cache
- Defined in:
- lib/zoom/cache.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#zoom_args ⇒ Object
readonly
Returns the value of attribute zoom_args.
-
#zoom_pattern ⇒ Object
readonly
Returns the value of attribute zoom_pattern.
-
#zoom_profile_name ⇒ Object
readonly
Returns the value of attribute zoom_profile_name.
-
#zoom_pwd ⇒ Object
readonly
Returns the value of attribute zoom_pwd.
Instance Method Summary collapse
- #args(a = nil) ⇒ Object
- #available_tags ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #get_results(tags = nil) ⇒ Object
-
#initialize(file = nil) ⇒ Cache
constructor
A new instance of Cache.
- #pattern(p = nil) ⇒ Object
- #profile_name(name = nil) ⇒ Object
- #pwd(p = nil) ⇒ Object
- #read ⇒ Object
- #shortcut(config) ⇒ Object
- #write(str) ⇒ Object
Constructor Details
#initialize(file = nil) ⇒ Cache
Returns a new instance of Cache.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/zoom/cache.rb', line 89 def initialize(file = nil) file = "~/.cache/zoom/cache" if (file.nil?) @cache_file = Pathname.new(file). @results = nil @thread = nil @zoom_args = nil @zoom_pattern = nil @zoom_profile_name = nil @zoom_pwd = nil FileUtils.mkdir_p(@cache_file.dirname) read end |
Instance Attribute Details
#zoom_args ⇒ Object (readonly)
Returns the value of attribute zoom_args.
5 6 7 |
# File 'lib/zoom/cache.rb', line 5 def zoom_args @zoom_args end |
#zoom_pattern ⇒ Object (readonly)
Returns the value of attribute zoom_pattern.
6 7 8 |
# File 'lib/zoom/cache.rb', line 6 def zoom_pattern @zoom_pattern end |
#zoom_profile_name ⇒ Object (readonly)
Returns the value of attribute zoom_profile_name.
7 8 9 |
# File 'lib/zoom/cache.rb', line 7 def zoom_profile_name @zoom_profile_name end |
#zoom_pwd ⇒ Object (readonly)
Returns the value of attribute zoom_pwd.
8 9 10 |
# File 'lib/zoom/cache.rb', line 8 def zoom_pwd @zoom_pwd end |
Instance Method Details
#args(a = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/zoom/cache.rb', line 10 def args(a = nil) if (a.nil?) return nil if (empty?) return @zoom_args end File.open(@cache_file, "a") do |f| f.write("ZOOM_ARGS=#{a}\n") end end |
#available_tags ⇒ Object
21 22 23 |
# File 'lib/zoom/cache.rb', line 21 def return (1..get_results.length).to_a end |
#clear ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/zoom/cache.rb', line 25 def clear @cache_file.delete if (@cache_file.exist?) @results = nil @thread.kill if (@thread) @thread = nil @zoom_args = nil @zoom_pattern = nil @zoom_profile_name = nil @zoom_pwd = nil end |
#empty? ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zoom/cache.rb', line 36 def empty? read if (@thread.nil?) return true if (@thread.nil?) while ( @thread.alive? && ( @zoom_args.nil? || @zoom_pattern.nil? || @zoom_profile_name.nil? || @zoom_pwd.nil? ) ) sleep 0.1 end return ( @zoom_args.nil? || @zoom_pattern.nil? || @zoom_profile_name.nil? || @zoom_pwd.nil? ) end |
#get_results(tags = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/zoom/cache.rb', line 60 def get_results( = nil) if (.nil?) begin @thread.join if (@thread) rescue Interrupt puts end return Array.new if (empty?) return @results end results = Array.new ().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 |
#pattern(p = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/zoom/cache.rb', line 125 def pattern(p = nil) if (p.nil?) return nil if (empty?) return @zoom_pattern end File.open(@cache_file, "a") do |f| f.write("ZOOM_PATTERN=#{p}\n") end end |
#profile_name(name = nil) ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/zoom/cache.rb', line 136 def profile_name(name = nil) if (name.nil?) return nil if (empty?) return @zoom_profile_name end File.open(@cache_file, "a") do |f| f.write("ZOOM_PROFILE_NAME=#{name}\n") end end |
#pwd(p = nil) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/zoom/cache.rb', line 147 def pwd(p = nil) if (p.nil?) return nil if (empty?) return @zoom_pwd end File.open(@cache_file, "a") do |f| f.write("ZOOM_PWD=#{p}\n") end end |
#read ⇒ Object
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 194 195 196 |
# File 'lib/zoom/cache.rb', line 158 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_ARGS=")) @zoom_args = line.gsub("ZOOM_ARGS=", "") elsif (line.start_with?("ZOOM_PATTERN=")) @zoom_pattern = line.gsub("ZOOM_PATTERN=", "") elsif (line.start_with?("ZOOM_PROFILE_NAME=")) @zoom_profile_name = line.gsub( "ZOOM_PROFILE_NAME=", "" ) elsif (line.start_with?("ZOOM_PWD=")) @zoom_pwd = line.gsub("ZOOM_PWD=", "") elsif ( (line == "-") || (line == "--") || line.empty? ) # 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
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 234 |
# File 'lib/zoom/cache.rb', line 198 def shortcut(config) return if (empty?) config.validate_colors 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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/zoom/cache.rb', line 236 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 |