Class: Zoom

Inherits:
Object
  • Object
show all
Includes:
Singleton, ZoomError
Defined in:
lib/zoom.rb

Instance Method Summary collapse

Constructor Details

#initializeZoom

Returns a new instance of Zoom.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/zoom.rb', line 231

def initialize
    # Load custom profiles
    custom_profs = Pathname.new("~/.zoom_profiles.rb").expand_path
    require_relative custom_profs if (custom_profs.exist?)

    @cache_file = Pathname.new("~/.zoom_cache").expand_path
    @info_file = Pathname.new("~/.zoominfo").expand_path
    @rc_file = Pathname.new("~/.zoomrc").expand_path
    @shortcut_file = Pathname.new("~/.zoom_shortcuts").expand_path

    read_zoomrc
    read_zoominfo

    # Setup editor
    @editor = @rc["editor"]
    @editor = ENV["EDITOR"] if (@editor.nil? || @editor.empty?)
    @editor = "vim" if (@editor.nil? || @editor.empty?)
    @editor = ScoobyDoo.where_are_you(@editor)
    @editor = ScoobyDoo.where_are_you("vi") if (@editor.nil?)
end

Instance Method Details

#add_profile(name, clas, operator = nil, flags = nil, envprepend = nil, append = nil) ⇒ Object



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

def add_profile(
    name,
    clas,
    operator = nil,
    flags = nil,
    envprepend = nil,
    append = nil
)
    if (@profiles.has_key?(name))
        raise ZoomError::ProfileAlreadyExistsError.new(name)
    end

    default_class = nil
    begin
        default_class = Object::const_get(clas).new
    rescue NameError => e
        raise ZoomError::ProfileClassUnknownError.new(clas)
    end

    edit_profile(
        name,
        default_class,
        operator,
        flags,
        envprepend,
        append
    )
end

#clear_cacheObject



47
48
49
# File 'lib/zoom.rb', line 47

def clear_cache
    @cache_file.delete if (@cache_file.exist?)
end

#configure_editor(editor) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/zoom.rb', line 51

def configure_editor(editor)
    e = ScoobyDoo.where_are_you(editor)
    if (e.nil?)
        raise ZoomError::ExecutableNotFoundError.new(editor)
    end

    @rc["editor"] = e
    write_zoomrc
end

#defaultObject



61
62
63
64
# File 'lib/zoom.rb', line 61

def default
    default_zoominfo
    default_zoomrc
end

#delete_profile(name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/zoom.rb', line 136

def delete_profile(name)
    if (name == "default")
        raise ZoomError::ProfileCanNotBeModifiedError.new(name)
    end

    if (name == "zoom_find")
        raise ZoomError::ProfileCanNotBeModifiedError.new(name)
    end

    if (!@profiles.has_key?(name))
        raise ZoomError::ProfileDoesNotExistError.new(name)
    end

    @profiles.delete(name)
    write_zoomrc

    if (name == @info["profile"])
        @info["profile"] = "default"
        write_zoominfo
    end
end

#edit_profile(name, profile = nil, operator = nil, flags = nil, envprepend = nil, append = nil) ⇒ 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
# File 'lib/zoom.rb', line 158

def edit_profile(
    name,
    profile = nil,
    operator = nil,
    flags = nil,
    envprepend = nil,
    append = nil
)
    if (name == "zoom_find")
        raise ZoomError::ProfileCanNotBeModified.new(name)
    end

    profile = @profiles[name] if (profile.nil?)

    if (profile.nil?)
        raise ZoomError::ProfileDoesNotExists.new(name)
    end

    profile.operator(operator) if (operator)
    profile.flags(flags) if (flags)
    profile.prepend(envprepend) if (envprepend)
    profile.append(append) if (append)

    @profiles[name] = profile
    write_zoomrc
end

#exec_profile(name, args, pattern) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/zoom.rb', line 185

def exec_profile(name, args, pattern)
    name = @info["profile"] if (name.nil?)

    if (!@profiles.has_key?(name))
        raise ZoomError::ProfileDoesNotExistError.new(name)
    end

    @info["last_command"] = {
        "profile" => name,
        "subargs" => args.nil? ? "": args,
        "pattern" => pattern.nil? ? "" : pattern
    }
    write_zoominfo

    profile = @profiles[name]
    begin
        clear_cache if (profile.taggable)
        profile.exe(args, pattern)
        shortcut_cache(profile) if (profile.taggable)
    rescue Interrupt
        # ^C
    end
end

#interactive_add_profile(name) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/zoom.rb', line 252

def interactive_add_profile(name)
    if (@profiles.has_key?(name))
        raise ZoomError::ProfileAlreadyExistsError.new(name)
    end

    default_op = "grep"
    if (ScoobyDoo.where_are_you("ag"))
        default_op = "ag"
    elsif (ScoobyDoo.where_are_you("ack"))
        default_op = "ack"
    elsif (ScoobyDoo.where_are_you("ack-grep"))
        default_op = "ack-grep"
    end

    case default_op
    when "ack", "ack-grep"
        puts "Enter class (default AckProfile):"
    when "ag"
        puts "Enter class (default AgProfile):"
    when "grep"
        puts "Enter class (default GrepProfile):"
    end

    clas = gets.chomp
    puts if (clas && !clas.empty?)

    case default_op
    when "ack", "ack-grep"
        clas = "AckProfile" if (clas.nil? || clas.empty?)
    when "ag"
        clas = "AgProfile" if (clas.nil? || clas.empty?)
    when "grep"
        clas = "GrepProfile" if (clas.nil? || clas.empty?)
    end

    add_profile(name, clas)
    interactive_edit_profile(name)
end

#interactive_edit_profile(name, profile = nil) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/zoom.rb', line 291

def interactive_edit_profile(name, profile = nil)
    if (name == "zoom_find")
        raise ZoomError::ProfileCanNotBeModifiedError.new(name)
    end

    profile = @profiles[name] if (profile.nil?)

    if (profile.nil?)
        raise ZoomError::ProfileDoesNotExistError.new(name)
    end

    # Get new operator
    puts "Enter operator (default #{profile.operator}):"

    op = ScoobyDoo.where_are_you(gets.chomp)
    puts if (op && !op.empty?)
    op = profile.operator if (op.nil? || op.empty?)

    # Get new flags
    puts "For empty string put \"empty\""
    puts "Enter flags (default \"#{profile.flags}\"):"

    flags = gets.chomp
    puts if (flags && !flags.empty?)
    flags = get_new_value(flags, profile.flags)

    # Get new prepend
    puts "For empty string put \"empty\""
    puts "Enter prepend (default \"#{profile.prepend}\"):"

    envprepend = gets.chomp
    puts if (envprepend && !envprepend.empty?)
    envprepend = get_new_value(envprepend, profile.prepend)

    # Get new append
    puts "For empty string put \"empty\""
    puts "Enter append (default \"#{profile.append}\"):"

    append = gets.chomp
    puts if (append && !append.empty?)
    append = get_new_value(append, profile.append)

    edit_profile(name, profile, op, flags, envprepend, append)
end

#list_profile_namesObject



336
337
338
339
340
# File 'lib/zoom.rb', line 336

def list_profile_names
    @profiles.keys.sort.each do |name|
        puts name
    end
end

#list_profilesObject



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/zoom.rb', line 342

def list_profiles
    @profiles.keys.sort.each do |name|
        if (name == @info["profile"])
            puts "### #{name} ###".green
        else
            puts "### #{name} ###"
        end
        puts @profiles[name].info
        puts
    end
end

#list_tagsObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/zoom.rb', line 354

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

    # Open shortcut file for writing
    shct = File.open(@shortcut_file, "r")

    # Read in cache
    File.open(@cache_file) do |cache|
        count = 1

        cache.each do |line|
            line.chomp!
            plain = remove_colors(line)
            if (line.start_with?("ZOOM_EXE_DIR="))
                # Ignore this line
            elsif ((line == "-") || (line == "--") || line.empty?)
                # Ignore dividers when searching with context and
                # empty lines
            elsif (plain.scan(/^[0-9]+[:-]/).empty?)
                if (!plain.scan(/^\.\//).empty?)
                    # Operator was probably find
                    puts count
                    count += 1
                end
            else
                puts count
                count += 1
            end
        end
    end
end

#loop_through_results(results) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/zoom.rb', line 386

def loop_through_results(results)
    tags = parse_tags(results)
    return if (tags.empty?)

    tag = tags.delete_at(0)
    open_editor_to_result(tag)

    tags.each do |tag|
        print "Do you want to open result #{tag} [y]/n/q/l?: "

        answer = nil
        while (answer.nil?)
            begin
                system("stty raw -echo")
                if ($stdin.ready?)
                    answer = $stdin.getc
                else
                    sleep 0.1
                end
            ensure
                system("stty -raw echo")
            end
        end
        puts

        case answer
        when "n", "N"
            # Do nothing
        when "l", "L"
            # Open this result then exit
            open_editor_to_result(tag)
            return
        when "q", "Q", "\x03"
            # Quit or ^C
            return
        else
            open_editor_to_result(tag)
        end
    end
end

#pagerObject



437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/zoom.rb', line 437

def pager
    File.open(@cache_file, "w") do |f|
        f.write("ZOOM_EXE_DIR=#{Dir.pwd}\n")
        begin
            $stdin.each_line do |line|
                f.write(line)
            end
        rescue Interrupt
            # ^C
        end
    end
end

#rename_profile(rename, name = nil) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/zoom.rb', line 489

def rename_profile(rename, name = nil)
    name = @info["profile"] if (name.nil?)

    if ((name == "default") || (name == "zoom_find"))
        raise ZoomError::ProfileCanNotBeModifiedError.new(name)
    end

    if (!@profiles.has_key?(name))
        raise ZoomError::ProfileDoesNotExistError.new(name)
    end

    if (@profiles.has_key?(rename))
        raise ZoomError::ProfileAlreadyExistsError.new(rename)
    end

    @profiles[rename] = @profiles[name]
    @profiles.delete(name)
    write_zoomrc

    if (name == @info["profile"])
        @info["profile"] = rename
        write_zoominfo
    end
end

#repeatObject



514
515
516
517
518
519
520
521
522
# File 'lib/zoom.rb', line 514

def repeat
    return if (@info["last_command"].empty?)

    exe_command(
        @info["last_command"]["profile"],
        @info["last_command"]["subargs"],
        @info["last_command"]["pattern"]
    )
end

#shortcut_cache(profile = nil) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/zoom.rb', line 529

def shortcut_cache(profile = nil)
    return if (!@cache_file.exist?)
    return if (@info["last_command"].empty?)

    if (profile.nil?)
        profile = @profiles[@info["last_command"]["profile"]]
    end

    # Open shortcut file for writing
    shct = File.open(@shortcut_file, "w")

    # Read in cache
    File.open(@cache_file) do |cache|
        start_dir = ""
        file = nil
        filename = ""
        first_time = true
        count = 1

        cache.each do |line|
            line.chomp!
            plain = remove_colors(line)
            if (line.start_with?("ZOOM_EXE_DIR="))
                # Get directory where search was ran
                start_dir = line.gsub("ZOOM_EXE_DIR=", "")
            elsif ((line == "-") || (line == "--") || line.empty?)
                # Ignore dividers when searching with context and
                # empty lines
            elsif (plain.scan(/^[0-9]+[:-]/).empty?)
                operator = profile.operator.split("/").last
                if (operator != "find")
                    if (file != line)
                        # Filename
                        file = line
                        filename = remove_colors(file)

                        puts if (!first_time)
                        first_time = false

                        puts "\e[0m#{file}"
                    end
                else
                    # Operator was find
                    puts "\e[1;31m[#{count}]\e[0m #{line}"
                    shct.write("'#{start_dir}/#{line}'\n")
                    count += 1
                end
            elsif (file)
                # Match
                sanitized = line.unpack("C*").pack("U*")
                    .gsub(/[\u0080-\u00ff]+/, "\1".dump[1..-2])
                puts "\e[1;31m[#{count}]\e[0m #{sanitized}"

                lineno = remove_colors(line).split(/[:-]/)[0]
                shct.write(
                    "+#{lineno} '#{start_dir}/#{filename}'\n"
                )

                count += 1
            end
        end
    end
end

#show_currentObject



593
594
595
596
# File 'lib/zoom.rb', line 593

def show_current
    puts "### #{@info["profile"]} ###".green
    puts @profiles[@info["profile"]].info
end

#switch_profile(name) ⇒ Object



598
599
600
601
602
603
604
605
# File 'lib/zoom.rb', line 598

def switch_profile(name)
    if (!@profiles.has_key?(name))
        raise ZoomError::ProfileDoesNotExistError.new(name)
    end

    @info["profile"] = name
    write_zoominfo
end