ruby-zoom 3.5.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/bin/z +189 -147
  3. data/bin/zc +189 -147
  4. data/bin/zf +189 -147
  5. data/bin/zg +189 -147
  6. data/bin/zl +189 -147
  7. data/bin/zr +189 -147
  8. data/lib/zoom.rb +48 -604
  9. data/lib/zoom/cache.rb +253 -0
  10. data/lib/zoom/cache/result.rb +49 -0
  11. data/lib/zoom/config.rb +156 -0
  12. data/lib/zoom/editor.rb +121 -0
  13. data/lib/zoom/error.rb +6 -6
  14. data/lib/zoom/error/{executable_not_found_error.rb → executable_not_found.rb} +1 -3
  15. data/lib/zoom/error/invalid_color.rb +5 -0
  16. data/lib/zoom/error/{invalid_tag_error.rb → invalid_tag.rb} +1 -3
  17. data/lib/zoom/error/{profile_class_unknown_error.rb → profile_class_unknown.rb} +1 -3
  18. data/lib/zoom/error/{profile_does_not_exist_error.rb → profile_does_not_exist.rb} +1 -3
  19. data/lib/zoom/error/profile_not_named.rb +7 -0
  20. data/lib/zoom/profile.rb +162 -66
  21. data/lib/zoom/profile/ack.rb +5 -30
  22. data/lib/zoom/profile/ag.rb +2 -37
  23. data/lib/zoom/profile/find.rb +2 -25
  24. data/lib/zoom/profile/grep.rb +2 -44
  25. data/lib/zoom/profile/passwords.rb +27 -59
  26. data/lib/zoom/profile/pt.rb +2 -26
  27. data/lib/zoom/profile_manager.rb +49 -0
  28. data/lib/zoom/wish/add_wish.rb +58 -0
  29. data/lib/zoom/wish/color_wish.rb +147 -0
  30. data/lib/zoom/wish/copy_wish.rb +57 -0
  31. data/lib/zoom/wish/delete_wish.rb +51 -0
  32. data/lib/zoom/wish/edit_wish.rb +130 -0
  33. data/lib/zoom/wish/editor_wish.rb +31 -0
  34. data/lib/zoom/wish/list_wish.rb +58 -0
  35. data/lib/zoom/wish/rename_wish.rb +69 -0
  36. data/lib/zoom/wish/reset_wish.rb +40 -0
  37. data/lib/zoom/wish/use_wish.rb +61 -0
  38. metadata +118 -24
  39. data/lib/string.rb +0 -48
  40. data/lib/zoom/error/profile_already_exists_error.rb +0 -8
  41. data/lib/zoom/error/profile_can_not_be_modified_error.rb +0 -8
@@ -0,0 +1,253 @@
1
+ require "io/wait"
2
+ require "pathname"
3
+
4
+ class Zoom::Cache
5
+ attr_reader :zoom_args
6
+ attr_reader :zoom_pattern
7
+ attr_reader :zoom_profile_name
8
+ attr_reader :zoom_pwd
9
+
10
+ def args(a = nil)
11
+ if (a.nil?)
12
+ return nil if (empty?)
13
+ return @zoom_args
14
+ end
15
+
16
+ File.open(@cache_file, "a") do |f|
17
+ f.write("ZOOM_ARGS=#{a}\n")
18
+ end
19
+ end
20
+
21
+ def available_tags
22
+ return (1..get_results.length).to_a
23
+ end
24
+
25
+ def clear
26
+ @cache_file.delete if (@cache_file.exist?)
27
+ @results = nil
28
+ @thread.kill if (@thread)
29
+ @thread = nil
30
+ @zoom_args = nil
31
+ @zoom_pattern = nil
32
+ @zoom_profile_name = nil
33
+ @zoom_pwd = nil
34
+ end
35
+
36
+ def empty?
37
+ read if (@thread.nil?)
38
+ return true if (@thread.nil?)
39
+
40
+ while (
41
+ @thread.alive? &&
42
+ (
43
+ @zoom_args.nil? ||
44
+ @zoom_pattern.nil? ||
45
+ @zoom_profile_name.nil? ||
46
+ @zoom_pwd.nil?
47
+ )
48
+ )
49
+ sleep 0.1
50
+ end
51
+
52
+ return (
53
+ @zoom_args.nil? ||
54
+ @zoom_pattern.nil? ||
55
+ @zoom_profile_name.nil? ||
56
+ @zoom_pwd.nil?
57
+ )
58
+ end
59
+
60
+ def get_results(tags = nil)
61
+ if (tags.nil?)
62
+ begin
63
+ @thread.join if (@thread)
64
+ rescue Interrupt
65
+ puts
66
+ end
67
+ return Array.new if (empty?)
68
+ return @results
69
+ end
70
+
71
+ results = Array.new
72
+ parse_tags(tags).each do |tag|
73
+ raise Zoom::Error::InvalidTag.new(tag) if (@thread.nil?)
74
+
75
+ while ((tag > @results.length) && @thread.alive?)
76
+ sleep 0.1
77
+ end
78
+
79
+ if (tag > @results.length)
80
+ raise Zoom::Error::InvalidTag.new(tag)
81
+ end
82
+
83
+ results.push(@results.at(tag - 1))
84
+ end
85
+
86
+ return results
87
+ end
88
+
89
+ def initialize(file = nil)
90
+ file = "~/.cache/zoom/cache" if (file.nil?)
91
+
92
+ @cache_file = Pathname.new(file).expand_path
93
+ @results = nil
94
+ @thread = nil
95
+ @zoom_args = nil
96
+ @zoom_pattern = nil
97
+ @zoom_profile_name = nil
98
+ @zoom_pwd = nil
99
+
100
+ FileUtils.mkdir_p(@cache_file.dirname)
101
+ read
102
+ end
103
+
104
+ def parse_tags(input)
105
+ raise Zoom::Error::InvalidTag.new if (input.nil? || input.empty?)
106
+
107
+ tags = Array.new
108
+ input.split(",").each do |num|
109
+ if (!num.scan(/^\d+$/).empty?)
110
+ tags.push(num.to_i)
111
+ elsif (!num.scan(/^\d+-\d+$/).empty?)
112
+ range = num.split("-")
113
+ (range[0].to_i..range[1].to_i).each do |i|
114
+ tags.push(i)
115
+ end
116
+ else
117
+ raise Zoom::Error::InvalidTag.new(num)
118
+ end
119
+ end
120
+ raise Zoom::Error::InvalidTag.new(0) if (tags.include?(0))
121
+ return tags
122
+ end
123
+ private :parse_tags
124
+
125
+ def pattern(p = nil)
126
+ if (p.nil?)
127
+ return nil if (empty?)
128
+ return @zoom_pattern
129
+ end
130
+
131
+ File.open(@cache_file, "a") do |f|
132
+ f.write("ZOOM_PATTERN=#{p}\n")
133
+ end
134
+ end
135
+
136
+ def profile_name(name = nil)
137
+ if (name.nil?)
138
+ return nil if (empty?)
139
+ return @zoom_profile_name
140
+ end
141
+
142
+ File.open(@cache_file, "a") do |f|
143
+ f.write("ZOOM_PROFILE_NAME=#{name}\n")
144
+ end
145
+ end
146
+
147
+ def pwd(p = nil)
148
+ if (p.nil?)
149
+ return nil if (empty?)
150
+ return @zoom_pwd
151
+ end
152
+
153
+ File.open(@cache_file, "a") do |f|
154
+ f.write("ZOOM_PWD=#{p}\n")
155
+ end
156
+ end
157
+
158
+ def read
159
+ return if (!@cache_file.exist?)
160
+
161
+ @results = Array.new
162
+ tag = 1
163
+ @thread.kill if (@thread)
164
+ @thread = Thread.new do
165
+ # Read in cache
166
+ File.open(@cache_file) do |cache|
167
+ cache.each do |line|
168
+ line.chomp!
169
+ if (line.start_with?("ZOOM_ARGS="))
170
+ @zoom_args = line.gsub("ZOOM_ARGS=", "")
171
+ elsif (line.start_with?("ZOOM_PATTERN="))
172
+ @zoom_pattern = line.gsub("ZOOM_PATTERN=", "")
173
+ elsif (line.start_with?("ZOOM_PROFILE_NAME="))
174
+ @zoom_profile_name = line.gsub(
175
+ "ZOOM_PROFILE_NAME=",
176
+ ""
177
+ )
178
+ elsif (line.start_with?("ZOOM_PWD="))
179
+ @zoom_pwd = line.gsub("ZOOM_PWD=", "")
180
+ elsif (
181
+ (line == "-") ||
182
+ (line == "--") ||
183
+ line.empty?
184
+ )
185
+ # Ignore dividers when searching with context
186
+ # and empty lines
187
+ else
188
+ @results.push(
189
+ Zoom::Cache::Result.new(tag, line, self)
190
+ )
191
+ tag += 1
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end
197
+
198
+ def shortcut(config)
199
+ return if (empty?)
200
+
201
+ config.validate_colors
202
+ profile = config.get_profile(profile_name)
203
+
204
+ if (!profile.taggable)
205
+ get_results.each do |result|
206
+ puts result.contents
207
+ end
208
+ return
209
+ end
210
+
211
+ curr_filename = nil
212
+ get_results.each do |result|
213
+ if (result.grep_like?)
214
+ if (result.filename != curr_filename)
215
+ puts if (curr_filename)
216
+ puts config.hilight_filename(result.filename)
217
+ curr_filename = result.filename
218
+ end
219
+
220
+ puts [
221
+ config.hilight_tag("[#{result.tag}]"),
222
+ "#{config.hilight_lineno(result.lineno)}:",
223
+ result.match.gsub(
224
+ /(#{pattern})/i,
225
+ config.hilight_match("\\1")
226
+ )
227
+ ].join(" ")
228
+ else
229
+ tag = result.tag
230
+ line = result.contents
231
+ puts [config.hilight_tag("[#{tag}]"), line].join(" ")
232
+ end
233
+ end
234
+ end
235
+
236
+ def write(str)
237
+ return if (str.nil?)
238
+
239
+ if (!str.valid_encoding?)
240
+ str = str.encode(
241
+ "UTF-16be",
242
+ :invalid => :replace,
243
+ :replace => "?"
244
+ ).encode("UTF-8")
245
+ end
246
+
247
+ File.open(@cache_file, "a") do |f|
248
+ f.write(str.gsub(/\r/, "^M"))
249
+ end
250
+ end
251
+ end
252
+
253
+ require "zoom/cache/result"
@@ -0,0 +1,49 @@
1
+ class Zoom::Cache::Result
2
+ attr_reader :contents
3
+ attr_reader :filename
4
+ attr_reader :lineno
5
+ attr_reader :match
6
+ attr_reader :tag
7
+
8
+ def args
9
+ return @cache.zoom_args
10
+ end
11
+
12
+ def grep_like?
13
+ return @grep_like
14
+ end
15
+
16
+ def initialize(tag, contents, cache)
17
+ @cache = cache
18
+ @contents = contents
19
+ @filename = nil
20
+ @grep_like = false
21
+ @lineno = nil
22
+ @match = nil
23
+ @tag = tag
24
+
25
+ @contents.unpack("C*").pack("U*").gsub(
26
+ /([\u0080-\u00ff]+)/,
27
+ "\\1".dump[1..-2]
28
+ ).match(/^([^:]+):(\d+)[:-](.*)/) do |m|
29
+ next if (m.nil?)
30
+
31
+ @grep_like = true
32
+ @filename = m[1]
33
+ @lineno = m[2]
34
+ @match = m[3]
35
+ end
36
+ end
37
+
38
+ def pattern
39
+ return @cache.zoom_pattern
40
+ end
41
+
42
+ def profile_name
43
+ return @cache.zoom_profile_name
44
+ end
45
+
46
+ def pwd
47
+ return @cache.zoom_pwd
48
+ end
49
+ end
@@ -0,0 +1,156 @@
1
+ require "hilighter"
2
+ require "json_config"
3
+ require "scoobydoo"
4
+
5
+ class Zoom::Config < JSONConfig
6
+ def color(key, value)
7
+ if (value)
8
+ validate_color(value)
9
+ set(key, value)
10
+ end
11
+
12
+ value = get(key)
13
+ validate_color(value)
14
+ return value
15
+ end
16
+ private :color
17
+
18
+ def color_filename(clr = nil)
19
+ return color("color_filename", clr)
20
+ end
21
+ private :color_filename
22
+
23
+ def color_lineno(clr = nil)
24
+ return color("color_lineno", clr)
25
+ end
26
+ private :color_lineno
27
+
28
+ def color_match(clr = nil)
29
+ return color("color_match", clr)
30
+ end
31
+ private :color_match
32
+
33
+ def color_tag(clr = nil)
34
+ return color("color_tag", clr)
35
+ end
36
+ private :color_tag
37
+
38
+ def current_profile_name(name = nil)
39
+ set("current_profile_name", name) if (name)
40
+ return get("current_profile_name")
41
+ end
42
+
43
+ def default_config
44
+ default = Zoom::ProfileManager.default_profile
45
+ profiles = Zoom::ProfileManager.default_profiles
46
+
47
+ clear
48
+ set("color", true)
49
+ set("color_filename", "green")
50
+ set("color_lineno", "white")
51
+ set("color_match", "black.on_white")
52
+ set("color_tag", "red")
53
+ set("current_profile_name", default)
54
+ set("editor", "")
55
+ set("profiles", profiles)
56
+ end
57
+
58
+ def editor(ed = nil)
59
+ if (ed)
60
+ e = ScoobyDoo.where_are_you(ed)
61
+ raise Zoom::Error::ExecutableNotFound.new(ed) if (e.nil?)
62
+ set("editor", ed)
63
+ end
64
+
65
+ e = get("editor")
66
+ e = ENV["EDITOR"] if (e.nil? || e.empty?)
67
+ e = "vim" if (e.nil? || e.empty?)
68
+ e = ScoobyDoo.where_are_you(e)
69
+ e = ScoobyDoo.where_are_you("vi") if (e.nil?)
70
+ raise Zoom::Error::ExecutableNotFound.new("vi") if (e.nil?)
71
+
72
+ return e
73
+ end
74
+
75
+ def has_profile?(name)
76
+ return get("profiles").has_key?(name)
77
+ end
78
+
79
+ def hilight(flag = nil)
80
+ set("color", flag) if (!flag.nil?)
81
+ return get("color")
82
+ end
83
+
84
+ def hilight_filename(str)
85
+ return str if (!hilight)
86
+ color_filename.split(".").inject(str, :send)
87
+ end
88
+
89
+ def hilight_lineno(str)
90
+ return str if (!hilight)
91
+ color_lineno.split(".").inject(str, :send)
92
+ end
93
+
94
+ def hilight_match(str)
95
+ return str if (!hilight)
96
+ color_match.split(".").inject(str, :send)
97
+ end
98
+
99
+ def hilight_tag(str)
100
+ return str if (!hilight)
101
+ color_tag.split(".").inject(str, :send)
102
+ end
103
+
104
+ def initialize(file = nil)
105
+ file ||= "~/.zoomrc"
106
+ super(file)
107
+ end
108
+
109
+ def get_profile(name)
110
+ return get_profiles(false)[name]
111
+ end
112
+
113
+ def get_profile_names
114
+ return get("profiles").keys.sort do |a, b|
115
+ a.downcase <=> b.downcase
116
+ end
117
+ end
118
+
119
+ def get_profiles(display_error = true)
120
+ profiles = Hash.new
121
+ get("profiles").each do |name, prof|
122
+ begin
123
+ profiles[name] = Zoom::Profile.from_json(prof)
124
+ rescue Zoom::Error => e
125
+ puts e.message if (display_error)
126
+ end
127
+ end
128
+ return profiles
129
+ end
130
+
131
+ def set_profiles(profiles)
132
+ set("profiles", profiles)
133
+ end
134
+
135
+ def validate_color(clr)
136
+ clr.split(".").each do |c|
137
+ if (
138
+ String.colors.keys.include?(c.gsub(/^on_/, "")) ||
139
+ String.modes.keys.include?(c)
140
+ )
141
+ next
142
+ end
143
+
144
+ raise Zoom::Error::InvalidColor.new(clr)
145
+ end
146
+ end
147
+ private :validate_color
148
+
149
+ def validate_colors
150
+ # Validate colors
151
+ color_filename
152
+ color_lineno
153
+ color_match
154
+ color_tag
155
+ end
156
+ end