Class: Nyaa::UI
- Inherits:
-
Object
- Object
- Nyaa::UI
- Defined in:
- lib/nyaa/ui.rb
Constant Summary collapse
- DOC =
"#{Nyaa::Utils.gem_libdir}/HELP"
Instance Attribute Summary collapse
-
#menusize ⇒ Object
Returns the value of attribute menusize.
-
#page ⇒ Object
Returns the value of attribute page.
Instance Method Summary collapse
- #footer ⇒ Object
- #get(choice) ⇒ Object
- #harvester ⇒ Object
- #header ⇒ Object
- #help ⇒ Object
-
#initialize(config, search) ⇒ UI
constructor
A new instance of UI.
- #menu(highlight) ⇒ Object
- #move(cursor, increment) ⇒ Object
- #next_page ⇒ Object
- #open(choice) ⇒ Object
- #prev_page ⇒ Object
- #resize_handler(cursor) ⇒ Object
- #start(choice) ⇒ Object
- #status(text = nil, type = nil) ⇒ Object
Constructor Details
#initialize(config, search) ⇒ UI
Returns a new instance of UI.
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 39 40 41 42 |
# File 'lib/nyaa/ui.rb', line 10 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', 's' => 'start', 'i' => 'info', 'n' => 'next', 'p' => 'prev', 'q' => 'quit', } @search = search @torrents = @search.more.results @num_torrents = @torrents.size @loading = @torrents.size % 100 == 0 ? true : false; 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
#menusize ⇒ Object
Returns the value of attribute menusize.
6 7 8 |
# File 'lib/nyaa/ui.rb', line 6 def @menusize end |
#page ⇒ Object
Returns the value of attribute page.
6 7 8 |
# File 'lib/nyaa/ui.rb', line 6 def page @page end |
Instance Method Details
#footer ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nyaa/ui.rb', line 60 def = @commands.map { |k,v| "#{k}: #{v}" }.join(' ') attrset(color_pair(2)) setpos(lines - 1, 0) addstr(sprintf " %-#{cols}s", ) search_summary = sprintf " %-14s %-14s %-14s", "view: [#{@offset+1}-#{@offset+@menusize}]/#{@search.count}", "recv: #{@num_torrents}/#{@loading ? "unk" : @search.count}", "page: #{@page}/#{@loading ? "unk" : @num_pages}" attrset(color_pair(2)) setpos(lines - 2, 0) addstr(sprintf "%-#{cols}s", search_summary) end |
#get(choice) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/nyaa/ui.rb', line 155 def get(choice) torrent = @torrents[@offset + choice - 1] download = Downloader.new(torrent.link, @config[:output]) path = download.save unless download.failed? status("Downloaded successful: #{torrent.tid}", :success) else status("Download failed (3 attempts): #{torrent.tid}", :failure) return nil; end return path; end |
#harvester ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/nyaa/ui.rb', line 208 def harvester Thread.new do last = @torrents.size; loop do @torrents = @search.more.results @num_torrents = @torrents.size if last == @torrents.size then @loading = false; @num_pages = (@torrents.size / @menusize.to_f).ceil; @page = @page > @num_pages ? @num_pages : @page; @offset = (@page - 1) * @menusize; break; end last = @torrents.size; sleep(2); end Thread.kill end end |
#header ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/nyaa/ui.rb', line 44 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 |
#help ⇒ Object
128 129 130 131 |
# File 'lib/nyaa/ui.rb', line 128 def help system("less #{DOC}") clear end |
#menu(highlight) ⇒ Object
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 |
# File 'lib/nyaa/ui.rb', line 92 def (highlight) xpos = 1 attrset(color_pair(0)) setpos(xpos, 0) (0..@menusize-1).each do |i| if i < @search.count - @offset 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 else # blank lines if there's < @menusize of results line_text = " "*cols addstr(line_text) xpos += 1 end end status("Fetching more results, try again.", :failure) if @loading && @torrents[@offset + 1].nil?; end |
#move(cursor, increment) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/nyaa/ui.rb', line 133 def move(cursor, increment) if increment < 0 # negative, retreat! if cursor == 1 if @offset == 0 cursor = 1 else prev_page cursor = end else cursor = cursor + increment end else # non-negative, advance! if cursor == next_page cursor = 1 else cursor = cursor + increment end end end |
#next_page ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/nyaa/ui.rb', line 187 def next_page status("Ready.") unless @page + 1 > @num_pages && @loading == false @page += 1 end @offset = (page - 1) * @menusize; end |
#open(choice) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/nyaa/ui.rb', line 175 def open(choice) link = choice.class == Fixnum ? @torrents[@offset + choice - 1].info.to_s : choice; 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_page ⇒ Object
195 196 197 198 199 200 |
# File 'lib/nyaa/ui.rb', line 195 def prev_page unless @page - 1 < 1 @page += -1 end @offset = (@page - 1) * @menusize; end |
#resize_handler(cursor) ⇒ Object
202 203 204 205 206 |
# File 'lib/nyaa/ui.rb', line 202 def resize_handler(cursor) @menusize = lines - 4 (cursor) refresh end |
#start(choice) ⇒ Object
170 171 172 173 |
# File 'lib/nyaa/ui.rb', line 170 def start(choice) path = self.get(choice); self.open(path) if path != nil; end |
#status(text = nil, type = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/nyaa/ui.rb', line 75 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 |