Module: Fargo::CLI::Searches

Included in:
Console
Defined in:
lib/fargo/cli/searches.rb

Instance Method Summary collapse

Instance Method Details

#results(str = nil, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/fargo/cli/searches.rb', line 23

def results str = nil, opts = {}
  str ||= client.searches.last
  results = client.search_results(str)

  if results.nil?
    puts "No search results for: #{str.inspect}!"
    return
  end

  results = results.map{ |r| r.dup }

  results.each_with_index{ |r, i|
    r[:file]  = r[:file].gsub("\\", '/')
    r[:file]  = File.basename(r[:file]) unless opts[:full]
    r[:index] = i
  }

  max_nick_size = results.map{ |r| r[:nick].size }.max

  if opts[:sort] == 'size'
    results = results.sort_by{ |r| r[:size] }
  elsif !opts[:sort].nil?
    puts "Unknown sort value: #{opts[:sort]}"
    results = []
  end

  if opts[:grep]
    results = results.select{ |r| r[:file].match opts[:grep] }
  end

  results.each do |r|
    printf "%3d: %#{max_nick_size}s %9s -- %s\n", r[:index],
      r[:nick], humanize_bytes(r[:size]), r[:file]
  end

  true
end

#setup_consoleObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fargo/cli/searches.rb', line 8

def setup_console
  super

  add_completion(/^results\s+[^\s]+$/){ client.searches }

  add_logger(:search_result) do |message|
    obj = client.search_objects.detect{ |s| s.matches? message }
    if obj
      "New search result for: #{obj.query.inspect}"
    else
      'New search result'
    end
  end
end