Class: Menu
- Inherits:
-
Object
- Object
- Menu
- Defined in:
- lib/mdisc/menu.rb
Constant Summary collapse
- WAIT_TIME =
0.1
Instance Attribute Summary collapse
-
#netease ⇒ Object
Returns the value of attribute netease.
-
#player ⇒ Object
Returns the value of attribute player.
-
#screen ⇒ Object
Returns the value of attribute screen.
-
#ui ⇒ Object
Returns the value of attribute ui.
Instance Method Summary collapse
- #choice_channel(idx) ⇒ Object
- #dispatch_enter(idx) ⇒ Object
- #favorite ⇒ Object
-
#initialize ⇒ Menu
constructor
A new instance of Menu.
- #search ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Menu
Returns a new instance of Menu.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mdisc/menu.rb', line 31 def initialize self.player = Player.new self.ui = Ui.new self.netease = NetEase.new self.screen = ui.screen @datatype = 'main' @title = '网易云音乐' @datalist = %w(排行榜 精选歌单 艺术家 新碟上架 我的歌单 DJ节目 本地收藏 搜索 帮助) @offset = 0 @index = 0 @present_songs = [] @step = 25 @stack = [] @userid = nil @username = nil @collection = [] @playlists = [] @account = {} @carousel = ->(left, right, x){x < left ? right : (x > right ? left : x)} end |
Instance Attribute Details
#netease ⇒ Object
Returns the value of attribute netease.
29 30 31 |
# File 'lib/mdisc/menu.rb', line 29 def netease @netease end |
#player ⇒ Object
Returns the value of attribute player.
29 30 31 |
# File 'lib/mdisc/menu.rb', line 29 def player @player end |
#screen ⇒ Object
Returns the value of attribute screen.
29 30 31 |
# File 'lib/mdisc/menu.rb', line 29 def screen @screen end |
#ui ⇒ Object
Returns the value of attribute ui.
29 30 31 |
# File 'lib/mdisc/menu.rb', line 29 def ui @ui end |
Instance Method Details
#choice_channel(idx) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/mdisc/menu.rb', line 252 def choice_channel(idx) case idx # Top when 0 songs = netease.top_songlist @datalist = netease.dig_info(songs, 'songs') @title += ' > 排行榜' @datatype = 'songs' # Playlists when 1 playlists = netease.top_playlists @datalist = netease.dig_info(playlists, 'playlists') @title += ' > 精选歌单' @datatype = 'playlists' # Artist when 2 artists = netease.top_artists @datalist = netease.dig_info(artists, 'artists') @title += ' > 艺术家' @datatype = 'artists' # New album when 3 albums = netease.new_albums @datalist = netease.dig_info(albums, 'albums') @title += ' > 新碟上架' @datatype = 'albums' # My playlist when 4 # Require user's account before fetching his playlists. check_user_id # Fetch this user's all playlists while he logs in successfully. my_playlist = netease.user_playlists @userid @datalist = netease.dig_info(my_playlist, 'playlists') @datatype = 'playlists' @title += " > #{@username} 的歌单" # DJ channels when 5 @datatype = 'djchannels' @title += ' > DJ 节目' @datalist = netease.djchannels # Favorite things when 6 favorite # Search when 7 search # Help when 8 @datatype = 'help' @title += ' > 帮助' @datalist = SHORTCUT end @offset = 0 @index = 0 end |
#dispatch_enter(idx) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/mdisc/menu.rb', line 214 def dispatch_enter(idx) datatype = @datatype title = @title datalist = @datalist offset = @offset index = @index @stack.push [datatype, title, datalist, offset, index] case datatype when 'main' choice_channel idx # Hot songs to which a artist belongs. when 'artists' id = datalist[idx]['artist_id'] songs = netease.artists id @datatype = 'songs' @datalist = netease.dig_info(songs, 'songs') @title += " > #{datalist[idx]['aritsts_name']}" # All songs to which an album belongs. when 'albums' id = datalist[idx]['album_id'] songs = netease.album id @datatype = 'songs' @datalist = netease.dig_info(songs, 'songs') @title += " > #{datalist[idx]['albums_name']}" # All songs to which a playlist belongs. when 'playlists' id = datalist[idx]['playlist_id'] songs = netease.playlist_detail id @datatype = 'songs' @datalist = netease.dig_info(songs, 'songs') @title += " > #{datalist[idx]['playlists_name']}" end end |
#favorite ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/mdisc/menu.rb', line 318 def favorite x = ui. if (1..4).include? x.to_i @stack.push [@datatype, @title, @datalist, @offset, @index] @index = 0 @offset = 0 end case x when '1' @datatype = 'songs' @datalist = @collection @title += ' > 收藏歌曲' when '2' @datatype = 'playlists' @datalist = @playlists @title += ' > 收藏歌单' when '3' @datatype = 'albums' @datalist = @albums @title += ' > 收藏专辑' when '4' @datatype = 'djchannels' @datalist = @djs @title += ' > 收藏 DJ 节目' end end |
#search ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/mdisc/menu.rb', line 351 def search x = ui. if (1..4).include? x.to_i @stack.push [@datatype, @title, @datalist, @offset, @index] @index = 0 @offset = 0 end case x when '1' @datatype = 'songs' @datalist = ui.build_search @datatype @title = '歌曲搜索列表' when '2' @datatype = 'artists' @datalist = ui.build_search @datatype @title = '艺术家搜索列表' when '3' @datatype = 'albums' @datalist = ui.build_search @datatype @title = '专辑搜索列表' when '4' @datatype = 'playlists' @datalist = ui.build_search @datatype @title = '精选歌单搜索列表' end end |
#start ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/mdisc/menu.rb', line 52 def start check_player read_data ui.(@datatype, @title, @datalist, @offset, @index, @step) @stack.push [@datatype, @title, @datalist, @offset, @index, @step] loop do datatype = @datatype title = @title datalist = @datalist offset = @offset idx = index = @index step = @step stack = @stack key = screen.getch screen.refresh case key # Quit when 'q' break # Up when 'k' @index = @carousel[@offset, [datalist.size, offset+step].min - 1, idx - 1] # Down when 'j' @index = @carousel[@offset, [datalist.size, offset+step].min - 1, idx + 1] # Previous page when 'u' next if offset == 0 @offset = @offset - step @index = (index - step).divmod(step)[0] * step # Next page when 'd' next if offset + step >= datalist.size @offset = @offset + step @index = (index + step).divmod(step)[0] * step # If highlighted item is a menu or playlists, just enter it. # If highlighted item is a song or an dj channel, just play it. when 'l' next if @datatype == 'help' if @datatype == 'songs' || @datatype == 'djchannels' player.play(@datatype, datalist, @index, true) @present_songs = [datatype, title, datalist, offset, index] else ui.build_loading dispatch_enter idx @index = 0 @offset = 0 end # Back when 'h' next if @stack.size == 1 up = stack.pop @datatype, @title, @datalist, @offset, @index = up[0], up[1], up[2], up[3], up[4] # Search when 'f' search # Next song when ']' player.next sleep WAIT_TIME # Previous song when '[' player.prev sleep WAIT_TIME # Play or pause a song. when ' ' player.play(datatype, datalist, idx) sleep WAIT_TIME # Load present playlist. when 'p' next if @present_songs.empty? @stack.push [datatype, title, datalist, offset, index] @datatype, @title, @datalist, @offset, @index = @present_songs[0], @present_songs[1], @present_songs[2], @present_songs[3], @present_songs[4] # Star a song, a playlist or an album. when 's' next if datalist.empty? if datatype == 'songs' @collection.push(datalist[idx]).uniq! elsif datatype == 'playlists' @playlists.push(datalist[idx]).uniq! elsif datatype == 'albums' @albums.push(datalist[idx]).uniq! elsif datatype == 'djchannels' @djs.push(datalist[idx]).uniq! end # Load favorite playlists. when 't' @stack.push [datatype, title, datalist, offset, index] @datatype = 'playlists' @title = '网易云音乐 > 收藏精选歌单' @datalist = @playlists @offset = 0 @index = 0 # Load favorite songs. when 'c' @stack.push [datatype, title, datalist, offset, index] @datatype = 'songs' @title = '网易云音乐 > 收藏歌曲列表' @datalist = @collection @offset = 0 @index = 0 # Load favorite albums. when 'a' @stack.push [datatype, title, datalist, offset, index] @datatype = 'albums' @title = '网易云音乐 > 收藏专辑' @datalist = @albums @offset = 0 @index = 0 # Load favorite dj channels. when 'z' @stack.push [datatype, title, datalist, offset, index] @datatype = 'djchannels' @title = '网易云音乐 > 收藏 DJ 节目' @datalist = @djs @offset = 0 @index = 0 # Remove an entry from the present list. when 'r' next if datatype == 'main' || datalist.empty? @datalist.delete_at idx @index = @carousel[@offset, [datalist.size, offset+step].min - 1, idx] # Main menu when 'm' next if datatype == 'main' @stack.push [datatype, title, datalist, offset, index] @datatype, @title, @datalist = @stack[0][0], @stack[0][1], @stack[0][2] @offset = 0 @index = 0 end write_data ui.(@datatype, @title, @datalist, @offset, @index, @step) end player.stop exit end |