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
- #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 |
# 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', '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
#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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/nyaa/ui.rb', line 58 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}/#{@search.count}", "page: #{@page}/#{@num_pages}" attrset(color_pair(2)) setpos(lines - 2, 0) addstr(sprintf "%-#{cols}s", search_summary) end |
#get(choice) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/nyaa/ui.rb', line 154 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 |
#harvester ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/nyaa/ui.rb', line 205 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 |
#header ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/nyaa/ui.rb', line 42 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
127 128 129 130 |
# File 'lib/nyaa/ui.rb', line 127 def help system("less #{DOC}") clear end |
#menu(highlight) ⇒ Object
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 124 125 |
# File 'lib/nyaa/ui.rb', line 90 def (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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/nyaa/ui.rb', line 132 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
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/nyaa/ui.rb', line 178 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
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/nyaa/ui.rb', line 165 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_page ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/nyaa/ui.rb', line 189 def prev_page unless @page - 1 < 1 @page += -1 end unless @offset == 0 @offset -= @menusize end end |
#resize_handler(cursor) ⇒ Object
199 200 201 202 203 |
# File 'lib/nyaa/ui.rb', line 199 def resize_handler(cursor) @menusize = lines - 4 (cursor) refresh end |
#status(text = nil, type = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/nyaa/ui.rb', line 73 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 |