Class: Ui

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

Overview

Player UI

SCREEN_TOP, SCREEN_LEFT |—–|————-SCREEN_WIDTH———————–| | PLAYER_X | | |————————————————| | |PLAYER_TITLE_Y | | |————————————————| | |PLAYER_STATUS_Y | | | | | |————————————————| | |PLAYER_CONTENT_Y | | | | | | | | | |SCREEN_HEIGHT | | | | | | | | | | | | | | | | | | | | | | |————————————————| | |PLAYER_INFO_Y | |—–|————————————————|

Constant Summary collapse

SCREEN_HEIGHT =
40
SCREEN_WIDTH =
80
SCREEN_TOP =
0
SCREEN_LEFT =
0
PLAYER_X =
8
PLAYER_TITLE_Y =
4
PLAYER_STATUS_Y =
5
PLAYER_CONTENT_Y =
7
PLAYER_INFO_Y =
19
PLAYER_NOTE_X =
PLAYER_X - 2
PLAYER_POINTER_X =
PLAYER_X - 3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUi

Returns a new instance of Ui.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mplug163/ui.rb', line 46

def initialize
  Curses.init_screen
  Curses.start_color
  Curses.cbreak
  Curses.stdscr.keypad(true)
  Curses.init_pair(1, Curses::COLOR_BLUE, Curses::COLOR_BLACK)
  Curses.init_pair(2, Curses::COLOR_CYAN, Curses::COLOR_BLACK)
  Curses.init_pair(3, Curses::COLOR_RED, Curses::COLOR_BLACK)
  Curses.init_pair(4, Curses::COLOR_MAGENTA, Curses::COLOR_BLACK)

  # height, width, top, left
  @screen = Curses::Window.new(SCREEN_HEIGHT, SCREEN_WIDTH, 0, 0)

  @netease = NetEase.new
end

Instance Attribute Details

#screenObject (readonly)

Returns the value of attribute screen.



30
31
32
# File 'lib/mplug163/ui.rb', line 30

def screen
  @screen
end

Instance Method Details

#build_favorite_menuObject



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mplug163/ui.rb', line 222

def build_favorite_menu
  clear_to_bottom(@screen, PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  putstr(@screen, PLAYER_CONTENT_Y, PLAYER_X, '选择收藏条目类型:', Curses.color_pair(1))
  putstr(@screen, PLAYER_CONTENT_Y + 1, PLAYER_X, '1 - 歌曲')
  putstr(@screen, PLAYER_CONTENT_Y + 2, PLAYER_X, '2 - 精选歌单')
  putstr(@screen, PLAYER_CONTENT_Y + 3, PLAYER_X, '3 - 专辑')
  putstr(@screen, PLAYER_CONTENT_Y + 4, PLAYER_X, '4 - DJ 节目')
  putstr(@screen, PLAYER_CONTENT_Y + 6, PLAYER_X, '请键入对应数字:', Curses.color_pair(2))
  @screen.refresh
  @screen.getch
end

#build_loadingObject



75
76
77
78
79
# File 'lib/mplug163/ui.rb', line 75

def build_loading
  clear_to_bottom(@screen, PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  putstr(@screen, PLAYER_CONTENT_Y, PLAYER_X, 'loading...', Curses.color_pair(1))
  @screen.refresh
end

#build_loginObject



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mplug163/ui.rb', line 246

def 
  params = get_param('请输入登录信息:(e.g. [email protected] foobar)')
   = params.split(' ')
  return  if .size != 2

   = @netease.([0], [1])
  if ['code'] != 200
    x = 
    return x == '1' ?  : -1
  else
    return [, ]
  end
end

#build_login_errorObject



260
261
262
263
264
265
266
267
268
# File 'lib/mplug163/ui.rb', line 260

def 
  clear_to_bottom(@screen, PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  putstr(@screen, PLAYER_CONTENT_Y + 1, PLAYER_X, 'oh,出现错误 Orz', Curses.color_pair(2))
  putstr(@screen, PLAYER_CONTENT_Y + 2, PLAYER_X, '1 - 再试一次')
  putstr(@screen, PLAYER_CONTENT_Y + 3, PLAYER_X, '2 - 稍后再试')
  putstr(@screen, PLAYER_CONTENT_Y + 5, PLAYER_X, '请键入对应数字:', Curses.color_pair(2))
  @screen.refresh
  @screen.getch
end

#build_menu(datatype, title, datalist, offset, index, step) ⇒ Object



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

def build_menu(datatype, title, datalist, offset, index, step)
  title = pretty_format(title, 0, 52)

  clear_to_bottom(@screen, PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  putstr(@screen, PLAYER_TITLE_Y, PLAYER_X, title, Curses.color_pair(1))

  if datalist.size == 0
    putstr(@screen, PLAYER_CONTENT_Y, PLAYER_X, '没有内容 Orz')
  else
    case datatype
    when 'main'
      (offset...[datalist.length, offset + step].min).each do |i|
        if i == index
          line_info = "#{i}. #{datalist[i]}"
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, line_info, Curses.color_pair(2))
        else
          line_info = "#{i}. #{datalist[i]}"
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
        end
      end

      putstr(@screen, PLAYER_INFO_Y, PLAYER_X, 'Crafted with ❤ by Ripple Yui', Curses.color_pair(3))

    when 'songs'
      (offset...[datalist.length, offset + step].min).each do |i|
        if i == index
          info = "#{i}. #{datalist[i]['song_name']} - #{datalist[i]['artist']} - #{datalist[i]['album_name']}"
          line_info = pretty_format(info, 0, 52)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, line_info, Curses.color_pair(2))
        else
          info = "#{i}. #{datalist[i]['song_name']} - #{datalist[i]['artist']} - #{datalist[i]['album_name']}"
          line_info = pretty_format(info, 0, 50)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
        end
      end

    when 'artists'
      (offset...[datalist.length, offset + step].min).each do |i|
        if i == index
          line_info = "#{i}. #{datalist[i]['artists_name']} - #{datalist[i]['artist']} #{datalist[i]['alias']}"
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, line_info, Curses.color_pair(2))
        else
          line_info = "#{i}. #{datalist[i]['artists_name']} - #{datalist[i]['artist']} #{datalist[i]['alias']}"
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
        end
      end

    when 'albums'
      (offset...[datalist.length, offset + step].min).each do |i|
        if i == index
          info = "#{i}. #{datalist[i]['albums_name']} - #{datalist[i]['artists_name']}"
          line_info = pretty_format(info, 0, 52)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, line_info, Curses.color_pair(2))
        else
          info = "#{i}. #{datalist[i]['albums_name']} - #{datalist[i]['artists_name']}"
          line_info = pretty_format(info, 0, 50)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
        end
      end

    when 'playlists'
      (offset...[datalist.length, offset + step].min).each do |i|
        if i == index
          info = "#{i}. #{datalist[i]['playlists_name']} - #{datalist[i]['creator_name']}"
          line_info = pretty_format(info, 0, 52)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, line_info, Curses.color_pair(2))
        else
          info = "#{i}. #{datalist[i]['playlists_name']} - #{datalist[i]['creator_name']}"
          line_info = pretty_format(info, 0, 50)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
        end
      end

    when 'djchannels'
      (offset...[datalist.length, offset + step].min).each do |i|
        if i == index
          info = "#{i}. #{datalist[i][0]['song_name']}"
          line_info = pretty_format(info, 0, 52)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, line_info, Curses.color_pair(2))
        else
          info = "#{i}. #{datalist[i][0]['song_name']}"
          line_info = pretty_format(info, 0, 50)
          putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
        end
      end

    when 'help'
      (offset...[datalist.length, offset + step].min).each do |i|
        line_info = "#{i}. #{datalist[i][0]} #{datalist[i][1]} #{datalist[i][2]}"
        putstr(@screen, i-offset+PLAYER_CONTENT_Y, PLAYER_X, line_info)
      end
    end
  end

end

#build_playinfo(song_name, artist, pause = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mplug163/ui.rb', line 62

def build_playinfo(song_name, artist, pause = false)
  if pause
    putstr(@screen, PLAYER_STATUS_Y, PLAYER_NOTE_X, '', Curses.color_pair(3))
  else
    putstr(@screen, PLAYER_STATUS_Y, PLAYER_NOTE_X, '', Curses.color_pair(3))
  end

  info = "#{song_name} - #{artist}"
  song_info = pretty_format(info, 0, 50)
  putstr(@screen, PLAYER_STATUS_Y, PLAYER_X, song_info, Curses.color_pair(4))
  @screen.refresh
end

#build_search(stype) ⇒ Object



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
213
214
215
216
217
218
219
220
# File 'lib/mplug163/ui.rb', line 177

def build_search(stype)
  case stype
  when 'songs'
    song_name = get_param('搜索歌曲:')
    data = @netease.search(song_name, stype = 1)
    song_ids = []
    if data['result'].include? 'songs'
      if data['result']['songs'].include? 'mp3Url'
        songs = data['result']['songs']
      else
        (0...data['result']['songs'].size).each do |i|
          song_ids.push data['result']['songs'][i]['id']
        end
        songs = @netease.songs_detail(song_ids)
      end
      return @netease.dig_info(songs, 'songs')
    end

  when 'artists'
    artist_name = get_param('搜索艺术家:')
    data = @netease.search(artist_name, stype = 100)
    if data['result'].include? 'artists'
      artists = data['result']['artists']
      return @netease.dig_info(artists, 'artists')
    end

  when 'albums'
    artist_name = get_param('搜索专辑:')
    data = @netease.search(artist_name, stype = 10)
    if data['result'].include? 'albums'
      albums = data['result']['albums']
      return @netease.dig_info(albums, 'albums')
    end

  when 'playlists'
    artist_name = get_param('搜索精选歌单:')
    data = @netease.search(artist_name, stype = 1000)
    if data['result'].include? 'playlists'
      playlists = data['result']['playlists']
      return @netease.dig_info(playlists, 'playlists')
    end

  end
end

#build_search_menuObject



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mplug163/ui.rb', line 234

def build_search_menu
  clear_to_bottom(@screen, PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  putstr(@screen, PLAYER_CONTENT_Y, PLAYER_X, '选择搜索类型:', Curses.color_pair(1))
  putstr(@screen, PLAYER_CONTENT_Y + 1, PLAYER_X, '1 - 歌曲')
  putstr(@screen, PLAYER_CONTENT_Y + 2, PLAYER_X, '2 - 艺术家')
  putstr(@screen, PLAYER_CONTENT_Y + 3, PLAYER_X, '3 - 专辑')
  putstr(@screen, PLAYER_CONTENT_Y + 4, PLAYER_X, '4 - 精选歌单')
  putstr(@screen, PLAYER_CONTENT_Y + 6, PLAYER_X, '请键入对应数字:', Curses.color_pair(2))
  @screen.refresh
  @screen.getch
end

#get_param(prompt_str) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/mplug163/ui.rb', line 270

def get_param(prompt_str)
  clear_to_bottom(@screen, PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  putstr(@screen, PLAYER_CONTENT_Y, PLAYER_X, prompt_str, Curses.color_pair(1))
  @screen.setpos(PLAYER_CONTENT_Y + 2, PLAYER_X)
  params = @screen.getstr
  if params.strip.nil?
    return get_param(prompt_str)
  else
    return params
  end
end