Module: Fargo::CLI::Info

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

Instance Method Summary collapse

Instance Method Details

#setup_consoleObject



10
11
12
13
14
# File 'lib/fargo/cli/info.rb', line 10

def setup_console
  super

  add_completion(/^who\s+[^\s]*$/) { client.nicks + ['size', 'name'] }
end

#who(sort_by = nil) ⇒ Object



16
17
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
46
47
48
49
50
51
# File 'lib/fargo/cli/info.rb', line 16

def who sort_by = nil
  print_nick = lambda{ |p|
    printf "%10s %s\n", humanize_bytes(p[1]), p[0]
  }

  if client.nicks.include? sort_by
    info = client.info(sort_by)
    key_len = info.keys.map{ |k| k.to_s.length }.max

    info.each_pair do |k, v|
      next if k == :type
      printf "%#{key_len}s: %s\n", k,
        v.is_a?(Numeric) ? humanize_bytes(v) : v
    end
  elsif sort_by.nil?
    client.nicks.each do |n|
      print_nick.call [n, client.info(n)[:sharesize]]
    end
  else
    pairs = client.nicks.map{ |n|
      [n, client.info(n)[:sharesize]]
    }

    if sort_by == 'name'
      pairs = pairs.sort_by{ |p| p[0] }
    elsif sort_by == 'size'
      pairs = pairs.sort_by{ |p| p[1] }
    else
      pairs = []
      puts "Unknown sorting by: #{sort_by.inspect}"
    end
    pairs.each &print_nick
  end

  true
end