Method: Menu#start

Defined in:
lib/mdisc/menu.rb

#startObject



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.build_menu(@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.build_menu(@datatype, @title, @datalist, @offset, @index, @step)
  end

  player.stop
  exit
end