Class: Nanny::CLI

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

Instance Method Summary collapse

Instance Method Details

#formatted_torrents(torrents) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nanny/cli.rb', line 39

def formatted_torrents(torrents)
  rows = torrents.each_with_index.map do |torrent, i|
    [
      i,
      truncate_string(torrent.title),
      torrent.human_size,
      torrent.seeds.to_s.red,
      torrent.peers.to_s.green
    ]
  end
  rows = [
    [ "#", "Title", "Size", "Seeds", "Peers" ],
    :separator
  ] + rows
  Terminal::Table.new(rows: rows).to_s
end

#highlineObject



84
85
86
# File 'lib/nanny/cli.rb', line 84

def highline
  @highline ||= HighLine.new
end

#process_options!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nanny/cli.rb', line 56

def process_options!
  Trollop::options do
    banner <<-RAW.strip_heredoc

    nanny is your friendly and lovely torrent finder

    Usage:
      nanny [options] <query>

    Options:
    RAW
    version "Nanny #{Nanny::VERSION} (c) Stefano Verna"
    opt :limit, "The number of results", default: 5
  end
end

#queryObject



72
73
74
# File 'lib/nanny/cli.rb', line 72

def query
  ARGV.join(" ")
end

#run!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nanny/cli.rb', line 11

def run!
  options = process_options!

  if query.size.zero?
    Trollop::die "A query is required"
  end

  torrents = Nanny::Search.new.search_torrents(query)
  results_range = 0 .. options[:limit] - 1

  puts formatted_torrents(torrents[results_range])
  puts
  torrent_index = highline.ask("Choose a torrent to download", Integer) do |q|
    q.default = 0
    q.in = results_range
  end
  torrent = torrents[torrent_index]

  puts

  bar = ProgressBar.create(title: "Searching")
  progress = Progress.new do |p|
    bar.progress = 100.0 * p.total_done / p.total_todo
  end

  highline.say torrent.torrent_url(progress)
end

#truncate_string(text) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/nanny/cli.rb', line 76

def truncate_string(text)
  if text.size > 50
    text[0..50] + "..."
  else
    text
  end
end