class Ui

Player UI

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

Constants

PLAYER_CONTENT_Y
PLAYER_INFO_Y
PLAYER_NOTE_X
PLAYER_POINTER_X
PLAYER_STATUS_Y
PLAYER_TITLE_Y
PLAYER_X
SCREEN_HEIGHT
SCREEN_WIDTH

Attributes

netease[RW]
screen[RW]

Public Class Methods

new() click to toggle source
# File lib/mdisc/ui.rb, line 44
def initialize
  self.screen = Screen.new(SCREEN_HEIGHT, SCREEN_WIDTH)
  self.netease = NetEase.new
end

Public Instance Methods

build_favorite_menu() click to toggle source
# File lib/mdisc/ui.rb, line 179
def build_favorite_menu
  screen.clear(PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  screen.line(PLAYER_CONTENT_Y, PLAYER_X, '选择收藏条目类型:', 1)
  screen.line(PLAYER_CONTENT_Y + 1, PLAYER_X, '1 - 歌曲')
  screen.line(PLAYER_CONTENT_Y + 2, PLAYER_X, '2 - 精选歌单')
  screen.line(PLAYER_CONTENT_Y + 3, PLAYER_X, '3 - 专辑')
  screen.line(PLAYER_CONTENT_Y + 4, PLAYER_X, '4 - DJ 节目')
  screen.line(PLAYER_CONTENT_Y + 6, PLAYER_X, '请键入对应数字:', 2)
  screen.refresh
  screen.getch
end
build_loading() click to toggle source
# File lib/mdisc/ui.rb, line 63
def build_loading
  screen.clear(PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  screen.line(PLAYER_CONTENT_Y, PLAYER_X, 'loading...', 1)
  screen.refresh
end
build_login() click to toggle source
# File lib/mdisc/ui.rb, line 203
def build_login
  params = get_param('请输入登录信息:(e.g. foobar@163.com foobar)')
  account = params.split(' ')
  return build_login if account.size != 2

  login_info = netease.login(account[0], account[1])
  if login_info['code'] != 200
    x = build_login_error
    return x == '1' ? build_login : -1
  else
    return [login_info, account]
  end
end
build_login_error() click to toggle source
# File lib/mdisc/ui.rb, line 217
def build_login_error
  screen.clear(PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  screen.line(PLAYER_CONTENT_Y + 1, PLAYER_X, 'oh,出现错误 Orz', 2)
  screen.line(PLAYER_CONTENT_Y + 2, PLAYER_X, '1 - 再试一次')
  screen.line(PLAYER_CONTENT_Y + 3, PLAYER_X, '2 - 稍后再试')
  screen.line(PLAYER_CONTENT_Y + 5, PLAYER_X, '请键入对应数字:', 2)
  screen.refresh
  screen.getch
end
build_menu(datatype, title, datalist, offset, index, step) click to toggle source
# File lib/mdisc/ui.rb, line 69
def build_menu(datatype, title, datalist, offset, index, step)
  title = pretty(title, 0, 50)

  screen.clear(PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  screen.line(PLAYER_TITLE_Y, PLAYER_X, title, 1)

  if datalist.size == 0
    screen.line(PLAYER_CONTENT_Y, PLAYER_X, '没有内容 Orz')
  else
    entries = offset...[datalist.length, offset + step].min

    case datatype
    when 'main'
      show(entries, index, offset, datalist) do |i, datalist|
        "#{i} #{datalist[i]}"
      end

      screen.line(PLAYER_INFO_Y, PLAYER_X, 'Crafted with <3 by cosmtrek', 3)

    when 'songs'
      show(entries, index, offset, datalist) do |i, datalist|
        sn = pretty(datalist[i]['song_name'], 0, 28)
        at = pretty(datalist[i]['artist'], 0, 24)
        "#{i} #{sn} - #{at}"
      end

    when 'artists'
      show(entries, index, offset, datalist) do |i, datalist|
        an = pretty(datalist[i]['artists_name'], 0, 28)
        "#{i} #{an}"
      end

    when 'albums'
      show(entries, index, offset, datalist) do |i, datalist|
        al = pretty(datalist[i]['albums_name'], 0, 28)
        an = pretty(datalist[i]['artists_name'], 0, 24)
        "#{i} #{al} - #{an}"
      end

    when 'playlists'
      show(entries, index, offset, datalist) do |i, datalist|
        pn = pretty(datalist[i]['playlists_name'], 0, 28);
        cn = pretty(datalist[i]['creator_name'], 0, 24);
        "#{i} #{pn}"
      end

    when 'djchannels'
      show(entries, index, offset, datalist) do |i, datalist|
        sn = pretty(datalist[i][0]['song_name'], 0, 28)
        "#{i} #{sn}"
      end

    when 'help'
      entries.each do |i|
        info = "#{i} #{datalist[i][0]} #{datalist[i][1]} #{datalist[i][2]}"
        screen.line(i-offset+PLAYER_CONTENT_Y, PLAYER_X, info)
      end
    end
  end

end
build_playinfo(song_name, artist, pause = false) click to toggle source
# File lib/mdisc/ui.rb, line 49
def build_playinfo(song_name, artist, pause = false)
  if pause
    screen.line(PLAYER_STATUS_Y, PLAYER_NOTE_X, 'S', 3)
  else
    screen.line(PLAYER_STATUS_Y, PLAYER_NOTE_X, 'P', 3)
  end

  sn = pretty(song_name, 0, 28)
  at = pretty(artist, 0, 24)
  info = "#{sn} - #{at}"
  screen.line(PLAYER_STATUS_Y, PLAYER_X, info, 4)
  screen.refresh
end
build_search_menu() click to toggle source
# File lib/mdisc/ui.rb, line 191
def build_search_menu
  screen.clear(PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  screen.line(PLAYER_CONTENT_Y, PLAYER_X, '选择搜索类型:', 1)
  screen.line(PLAYER_CONTENT_Y + 1, PLAYER_X, '1 - 歌曲')
  screen.line(PLAYER_CONTENT_Y + 2, PLAYER_X, '2 - 艺术家')
  screen.line(PLAYER_CONTENT_Y + 3, PLAYER_X, '3 - 专辑')
  screen.line(PLAYER_CONTENT_Y + 4, PLAYER_X, '4 - 精选歌单')
  screen.line(PLAYER_CONTENT_Y + 6, PLAYER_X, '请键入对应数字:', 2)
  screen.refresh
  screen.getch
end
get_param(prompt_str) click to toggle source
# File lib/mdisc/ui.rb, line 227
def get_param(prompt_str)
  screen.clear(PLAYER_CONTENT_Y, SCREEN_HEIGHT)
  screen.line(PLAYER_CONTENT_Y, PLAYER_X, prompt_str, 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

Private Instance Methods

highlight_or_not(i, index, offset, info) click to toggle source
# File lib/mdisc/ui.rb, line 249
def highlight_or_not(i, index, offset, info)
  if i == index
    highlight = "=> #{info}"
    screen.line(i-offset+PLAYER_CONTENT_Y, PLAYER_POINTER_X, highlight, 2)
  else
    screen.line(i-offset+PLAYER_CONTENT_Y, PLAYER_X, info)
  end
end
pretty(info, start, length) click to toggle source
# File lib/mdisc/ui.rb, line 241
def pretty(info, start, length)
  if info.size >= length
    "#{info[start, length]}..."
  else
    info
  end
end
show(entries, index, offset, datalist) { |i, datalist| ... } click to toggle source
# File lib/mdisc/ui.rb, line 258
def show(entries, index, offset, datalist)
  entries.each do |i|
    info = yield(i, datalist) # Get custom info.
    highlight_or_not(i, index, offset, info)
  end
end