Class: Zoom

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

Defined Under Namespace

Classes: Error, Profile

Instance Method Summary collapse

Constructor Details

#initialize(cache_file = nil) ⇒ Zoom

Returns a new instance of Zoom.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/zoom.rb', line 224

def initialize(cache_file = nil)
    cache_file ||= "~/.zoom_cache"
    Zoom::Profile.cache_file(cache_file)
    @cache_file = Pathname.new(cache_file).expand_path
    @info_file = Pathname.new("~/.zoominfo").expand_path
    @rc_file = Pathname.new("~/.zoomrc").expand_path
    @shortcut_file = Pathname.new("~/.zoom_shortcuts").expand_path

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

    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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zoom.rb', line 7

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

    default_class = nil
    begin
        default_class = Zoom::Profile.profile_by_name(clas).new
    rescue NameError => e
        raise Zoom::Error::ProfileClassUnknownError.new(clas)
    end

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

#clear_cache ⇒ Object



36
37
38
# File 'lib/zoom.rb', line 36

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

#configure_editor(editor) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/zoom.rb', line 40

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

    @rc["editor"] = e
    write_zoomrc
end

#default ⇒ Object



50
51
52
53
# File 'lib/zoom.rb', line 50

def default
    default_zoominfo
    default_zoomrc
end

#default_zoominfo ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zoom.rb', line 55

def default_zoominfo
    info = Hash.new
    info["profile"] = "default"

    # Reset last command to be empty
    info["last_command"] = Hash.new

    File.open(@info_file, "w") do |file|
        file.write(JSON.pretty_generate(info))
    end
end

#default_zoomrc ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zoom.rb', line 67

def default_zoomrc
    rc = Hash.new
    profiles = Hash.new

    all = nil
    ag = nil
    pt = nil
    ack = nil

    # Default ag profiles
    if (ScoobyDoo.where_are_you("ag"))
        ag = Zoom::Profile::Ag.new
        all = Zoom::Profile::Ag.new(nil, "-u")
    end

    # Default pt profiles
    if (ScoobyDoo.where_are_you("pt"))
        pt = Zoom::Profile::Pt.new
        all ||= Zoom::Profile::Pt.new(nil, "--color -SeU")
    end

    # Default ack profile
    if (
        ScoobyDoo.where_are_you("ack") ||
        ScoobyDoo.where_are_you("ack-grep")
    )
        ack = Zoom::Profile::Ack.new
    end

    # Default grep profile (emulate ag/ack as much as possible)
    grep = Zoom::Profile::Grep.new
    all ||= Zoom::Profile::Grep.new(nil, "--color=always -EHinR")

    # Create default profile
    if (ag)
        default = ag
    elsif (pt)
        default = pt
    elsif (ack)
        default = ack
    else
        default = grep
    end

    # Create find profile
    find = Zoom::Profile::Find.new

    # Put profiles into rc
    profiles["ack"] = ack if (ack)
    profiles["ag"] = ag if (ag)
    profiles["all"] = all if (all)
    profiles["default"] = default
    profiles["grep"] = grep
    profiles["passwords"] = Zoom::Profile::Passwords.new
    profiles["pt"] = pt if (pt)
    profiles["zoom_find"] = find
    rc["profiles"] = profiles

    # Default editor (use $EDITOR)
    rc["editor"] = ""

    File.open(@rc_file, "w") do |file|
        file.write(JSON.pretty_generate(rc))
    end
end

#delete_profile(name) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/zoom.rb', line 133

def delete_profile(name)
    if (!@profiles.has_key?(name))
        raise Zoom::Error::ProfileDoesNotExistError.new(name)
    end

    if ((name == "default") || @profiles[name].immutable)
        raise Zoom::Error::ProfileCanNotBeModifiedError.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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/zoom.rb', line 151

def edit_profile(
    name,
    profile = nil,
    operator = nil,
    flags = nil,
    envprepend = nil,
    append = nil
)
    profile = @profiles[name] if (profile.nil?)

    if (profile.nil?)
        raise Zoom::Error::ProfileDoesNotExistsError.new(name)
    end

    if (profile.immutable)
        raise Zoom::Error::ProfileCanNotBeModifiedError.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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/zoom.rb', line 178

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

    if (!@profiles.has_key?(name))
        raise Zoom::Error::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



248
249
250
251
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
290
291
292
293
294
295
296
# File 'lib/zoom.rb', line 248

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

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

    ack_class = Zoom::Profile::Ack.to_s
    ag_class = Zoom::Profile::Ag.to_s
    grep_class = Zoom::Profile::Grep.to_s
    pt_class = Zoom::Profile::Pt.to_s

    case default_op
    when "ack", "ack-grep"
        puts "Enter class (default #{ack_class}):"
    when "ag"
        puts "Enter class (default #{ag_class}):"
    when "grep"
        puts "Enter class (default #{grep_class}):"
    when "pt"
        puts "Enter class (default #{pt_class}):"
    end

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

    case default_op
    when "ack", "ack-grep"
        clas = ack_class if (clas.nil? || clas.empty?)
    when "ag"
        clas = ag_class if (clas.nil? || clas.empty?)
    when "grep"
        clas = grep_class if (clas.nil? || clas.empty?)
    when "pt"
        clas = pt_class if (clas.nil? || clas.empty?)
    end

    add_profile(name, clas)
    interactive_edit_profile(name)
end

#interactive_edit_profile(name, profile = nil) ⇒ Object



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
335
336
337
338
339
340
341
# File 'lib/zoom.rb', line 298

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

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

    if (profile.immutable)
        raise Zoom::Error::ProfileCanNotBeModifiedError.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_names ⇒ Object



343
344
345
346
347
# File 'lib/zoom.rb', line 343

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

#list_profiles ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
# File 'lib/zoom.rb', line 349

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_tags ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/zoom.rb', line 361

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



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
426
427
428
429
430
431
432
# File 'lib/zoom.rb', line 393

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

#pager ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/zoom.rb', line 444

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



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/zoom.rb', line 502

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

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

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

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

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

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

#repeat ⇒ Object



527
528
529
530
531
532
533
534
535
# File 'lib/zoom.rb', line 527

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

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

#shortcut_cache(profile = nil) ⇒ Object



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
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/zoom.rb', line 542

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_current ⇒ Object



606
607
608
609
# File 'lib/zoom.rb', line 606

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

#switch_profile(name) ⇒ Object



611
612
613
614
615
616
617
618
# File 'lib/zoom.rb', line 611

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

    @info["profile"] = name
    write_zoominfo
end