Class: Nyaa::UI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, search) ⇒ UI

Returns a new instance of UI.



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
35
36
37
38
# File 'lib/nyaa/ui.rb', line 8

def initialize (config, search)
  @config = config
  @status = { :text => 'Ready.', :type => :default }
  setup_curses

  # columns
  @info_columns = %w[ Size SE LE ]
  @info_column_width = 10
  @name_column_width = 0

  # commands
  @commands = {
    '?' => 'help',
    'g' => 'get',
    'i' => 'info',
    'n' => 'next',
    'p' => 'prev',
    'q' => 'quit',
  }

  @search = search
  @torrents = @search.more.results
  @num_torrents = @torrents.size
  harvester # start bg harvester
  
  @menusize = lines - 4 # lines - header + footer + status
  @page = 1
  @num_pages = (@search.count/@menusize.to_f).ceil
  @offset = 0
  # TODO: on hold state: prevent paging forward when waiting on results
end

Instance Attribute Details

Returns the value of attribute menusize.



6
7
8
# File 'lib/nyaa/ui.rb', line 6

def menusize
  @menusize
end

#pageObject

Returns the value of attribute page.



6
7
8
# File 'lib/nyaa/ui.rb', line 6

def page
  @page
end

Instance Method Details



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

def footer
  footer_text = @commands.map { |k,v| "#{k}: #{v}" }.join('  ')
  attrset(color_pair(2))
  setpos(lines - 1, 0)
  addstr(sprintf " %-#{cols}s", footer_text)

  search_summary = sprintf " %-14s %-14s %-14s",
    "view: [#{@offset+1}-#{@offset+@menusize}]/#{@search.count}",
    "recv: #{@num_torrents}/#{@search.count}",
    "page: #{@page}/#{@num_pages}"
  attrset(color_pair(2))
  setpos(lines - 2, 0)
  addstr(sprintf "%-#{cols}s", search_summary)
end

#get(choice) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/nyaa/ui.rb', line 151

def get(choice)
  torrent = @torrents[@offset + choice - 1]
  download = Downloader.new(torrent.link, @config[:output])
  download.save
  unless download.failed?
    status("Downloaded successful: #{torrent.tid}", :success)
  else
    status("Download failed (3 attempts): #{torrent.tid}", :failure)
  end
end

#harvesterObject



202
203
204
205
206
207
208
209
210
211
# File 'lib/nyaa/ui.rb', line 202

def harvester
  Thread.new do
    until @num_torrents == @search.count
      @torrents = @search.more.results
      @num_torrents = @torrents.size
      sleep 2
    end
    Thread.kill
  end
end

#headerObject



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

def header
  # pad info columns to column width
  @info_columns = @info_columns.map do |column|
    sprintf("%#{@info_column_width}s", column)
  end
  # key column width is whatever is leftover
  @name_column_width = cols - (@info_columns.length * @info_column_width)

  # header bar
  cat = CATS[@config[:category].to_sym][:title]
  header_text = sprintf " %-#{@name_column_width-1}s%s", "Nyaa - #{cat}", @info_columns.join
  attrset(color_pair(1))
  setpos(0,0)
  addstr(sprintf "%-#{cols}s", header_text)
end

#helpObject

TODO: Pop up help window



126
127
# File 'lib/nyaa/ui.rb', line 126

def help
end


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
# File 'lib/nyaa/ui.rb', line 88

def menu(highlight)
  xpos = 1
  attrset(color_pair(0))
  setpos(xpos, 0)

  (0..@menusize-1).each do |i|
    if i < @search.count - @offset
      if @torrents[@offset + i].nil?
        status("Fetching more results, try again.", :failure)
      else
        line_text = sprintf("% -#{@name_column_width}s %9s %9s %9s",
          truncate("#{@torrents[@offset + i].name}", @name_column_width),
          @torrents[@offset + i].filesize,
          @torrents[@offset + i].seeders,
          @torrents[@offset + i].leechers)

        attrset(color_pair(torrent_status(@torrents[@offset + i])))
        setpos(xpos, 0)
        # highlight the present choice
        if highlight == i + 1
          attron(A_STANDOUT)
          addstr(line_text)
          attroff(A_STANDOUT)
        else
          addstr(line_text)
        end
        xpos += 1
      end
    else
      # blank lines if there's < @menusize of results
      line_text = " "*cols
      addstr(line_text)
      xpos += 1
    end 
  end
end

#move(cursor, increment) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nyaa/ui.rb', line 129

def move(cursor, increment)
  if increment < 0 # negative, retreat!
    if cursor == 1
      if @offset == 0
        cursor = 1
      else
        prev_page
        cursor = menusize
      end
    else
      cursor = cursor + increment
    end
  else # non-negative, advance!
    if cursor == menusize
      next_page
      cursor = 1
    else
      cursor = cursor + increment
    end
  end
end

#next_pageObject



175
176
177
178
179
180
181
182
183
184
# File 'lib/nyaa/ui.rb', line 175

def next_page
  status("Ready.")
  unless @page + 1 > @num_pages
    @page += 1
  end

  unless @offset + @menusize > @num_torrents
    @offset += @menusize
  end
end

#open(choice) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/nyaa/ui.rb', line 162

def open(choice)
  torrent = @torrents[@offset + choice - 1]
  link = "#{torrent.info}"
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ then
      system("start #{link}")
  elsif RbConfig::CONFIG['host_os'] =~ /darwin/ then
      system("open '#{link}'", [:out, :err]=>'/dev/null')
  elsif RbConfig::CONFIG['host_os'] =~ /linux/ then
      system("xdg-open '#{link}'", [:out, :err]=>'/dev/null')
  end
  status("Opened '#{link}'", :success)
end

#prev_pageObject



186
187
188
189
190
191
192
193
194
# File 'lib/nyaa/ui.rb', line 186

def prev_page
  unless @page - 1 < 1
    @page += -1
  end

  unless @offset == 0
    @offset -= @menusize
  end
end

#resize_handler(cursor) ⇒ Object



196
197
198
199
200
# File 'lib/nyaa/ui.rb', line 196

def resize_handler(cursor)
  @menusize = lines - 4
  menu(cursor)
  refresh
end

#status(text = nil, type = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nyaa/ui.rb', line 71

def status(text = nil, type = nil)
  @status[:text] = text if text
  @status[:type] = type

  case @status[:type]
  when :success then profile = 8
  when :failure then profile = 9
  else profile = 1
  end

  status_text = sprintf " Status: %-s", @status[:text]
  attrset(color_pair(profile))
  setpos(lines-3,0)
  addstr(sprintf "%-#{cols}s", status_text)
  refresh
end