Class: NCursesUI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cloud, options = {}, params = {}) ⇒ NCursesUI

Returns a new instance of NCursesUI.



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
41
42
43
44
45
# File 'lib/ncurses_ui.rb', line 13

def initialize cloud, options = {}, params = {}
  params.each { |key, value| send "#{key}=", value }

  if not @audio_backend
    @audio_backend = Object.new
    def @audio_backend.version
      "Audio backend: None" 
    end 
  end

  defaults = {
    :colors => {
      :default => [:cyan, :blue],
      :playlist => [:cyan, :blue],
      :playlist_active => [:white, :blue],
      :progress => [:cyan, :blue],
      :progress_bar => [:blue, :cyan],
      :title => [:cyan, :black],
      :artist => [:cyan, :black],
      :status => [:magenta, :black]
    }
  }.freeze

  @options = defaults.deep_merge(options || {})
  @cloud = cloud
  @state = :running
  @frac = 0
  @title = "None"
  @op = " "
  @time = 0
  @timeleft = 0
  @playlist = []
end

Instance Attribute Details

#audio_backendObject

Returns the value of attribute audio_backend.



11
12
13
# File 'lib/ncurses_ui.rb', line 11

def audio_backend
  @audio_backend
end

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/ncurses_ui.rb', line 11

def logger
  @logger
end

Instance Method Details

#closeObject



204
205
206
# File 'lib/ncurses_ui.rb', line 204

def close
  @state = :close
end

#cloud_update(arg) ⇒ Object



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
# File 'lib/ncurses_ui.rb', line 131

def cloud_update(arg)
  case arg[:state]
  when :load
    @playlist |= arg[:tracks]
    @l.list = @playlist if @l
  when :shuffle
    @playlist = arg[:tracks]
    @l.list = @playlist if @l
  when :next, :previous
    pos = arg[:position]
    @l.active = pos if @l
  when :download
    if arg[:error]
      @error = "Error: #{arg[:error]}"
    end
    if arg[:count]
      count = arg[:count]
      if(count > 0)
        @l.height = -1
        @d.visible = true
        @d.count = count
        @d.title = arg[:name] if arg[:name]
      else
        @l.height = 0
        @d.visible = false
        @d.title = ""
      end
    end
  end
end

#player_update(arg) ⇒ Object



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
# File 'lib/ncurses_ui.rb', line 162

def player_update(arg)

  case arg[:state]
  when :load
    track = arg[:track]
    if track.nil?
      @error = "Error: Nothing found!"
    else
      @error = nil
      @title = track["title"]
      @username = track["user"]["username"]
      @timetotal = track["duration"]
      @error = "Error: #{track[:error]}" if track[:error]
    end
  when :info
    frame = arg[:frame].to_f
    frames = frame + arg[:frameleft]
    @frac = frame/frames
    @time = arg[:time].to_i
  when :pause
    @op = "\u2161"
  when :resume, :play
    @op = "\u25B6"
  when :stop
    @op = "\u25FC"
  when :error
    @error = "Error: #{arg[:error]}"
  when :status
    if arg[:type]
      @status = "#{arg[:type]}: #{arg[:value]}"
      if @statusTimeout
        @statusTimeout.exit
      end
      @statusTimeout = Thread.new do
        sleep 5
        @status = nil
        @statusTimeout = nil
      end
    end
  end
end

#runObject



47
48
49
50
51
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
# File 'lib/ncurses_ui.rb', line 47

def run
  begin
    stdscr = Curses.init_screen
    Curses.start_color
    Colors.init @options[:colors]
    stdscr.keypad true
    Curses.nonl
    Curses.cbreak
    Curses.noecho
    Curses.curs_set 0
    Curses.timeout = 5
    @p = NProgress.new stdscr, 0, 0, :progress, :progress_bar
    @l = NPlaylist.new stdscr, 4, 0, :playlist, :playlist_active, 0, 0, @playlist
    @i = NInfobox.new self, stdscr, 4, 0, :playlist, 0, 9
    @d = NDownloadBox.new stdscr, Curses.lines-1, 0, :default, 0, 1
    @l.active = 0
    last_ch = nil
    while(@state != :close)
      ch = Curses.getch
      last_ch = ch if ch
      Curses.setpos 3, 0
      Curses.clrtoeol
      # Nutils.print stdscr, 3, 0, "Test %s" % [last_ch], :red
      case ch
      when Curses::KEY_RESIZE
        @p.resize
        @l.resize
        @i.resize
        @d.resize
        Curses.refresh
      when 110, 78, 'n', 'N', Curses::KEY_DOWN
        @cloud.nextTrack
      when 112, 80, 'p', 'P', Curses::KEY_UP
        @cloud.prevTrack
      when 113, 81, 'q', 'Q', 27, Curses::KEY_EXIT
        @cloud.quit
      when 61, 43, '=', '+'
        @cloud.volumeUp
      when 45, 95, '-', '_'
        @cloud.volumeDown
      when 109, 77, 'm', 'M'
        @cloud.toggleMute
      when 68, 100, 'd', 'D'
        @cloud.download
      when 118, 86, 'v', 'V'
        @i.visible = !@i.visible
        @l.dirty = true
      when 32, ' '
        @cloud.pause
      end

      statusLine = @status || @error

      if statusLine
        Nutils.print stdscr, 3, 0, "#{statusLine}", :status
        Curses.refresh
      end
      tr = " %s " % [Nutils.timestr(@timetotal)]
      t = " %-#{Curses.cols-tr.size-1}s%s" % [Nutils.timestr(@time), tr]
      @p.value = @frac
      @p.text = t
      @p.refresh
      Nutils.print stdscr, 1, 0, "#{@op} #{@title}", :title
      Nutils.print stdscr, 2, 0, "  by #{@username}", :artist
      @l.refresh
      @i.refresh
      @d.refresh
      stdscr.refresh
    end
  rescue => ex
  ensure
    @l.close if @l
    @p.close if @p
    stdscr.close
    Curses.echo
    Curses.nocbreak
    Curses.nl
    Curses.close_screen
    puts ex.inspect if ex
    puts ex.backtrace if ex
    # Colors.debug
  end
end