about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-01-25 11:52:43 +0000
committerEric Wong <e@80x24.org>2016-01-25 11:54:16 +0000
commit64247079b9ae4ec58e801658ba62c2a64fee1e72 (patch)
tree5e5399e10c33e01bea8f03ff8dbd5a2643b7cf39
parent6c06185c7267567fe1237737342d3d8059d4bfc3 (diff)
downloaddtas-64247079b9ae4ec58e801658ba62c2a64fee1e72.tar.gz
When activated, this boolean deletes a song from the tracklist
after it is played.
-rwxr-xr-xbin/dtas-console2
-rw-r--r--lib/dtas/player/client_handler.rb14
-rw-r--r--lib/dtas/tracklist.rb27
3 files changed, 34 insertions, 9 deletions
diff --git a/bin/dtas-console b/bin/dtas-console
index 1494bfb..4199583 100755
--- a/bin/dtas-console
+++ b/bin/dtas-console
@@ -200,7 +200,7 @@ begin
     cur_vol = rg['volume'] || 1.0
     extra = [ "volume=#{cur_vol}" ]
     tl = cur['tracklist'] || {}
-    %w(repeat shuffle).each { |x| extra << "#{x}=#{tl[x] || 'false'}" }
+    %w(repeat shuffle consume).each { |x| extra << "#{x}=#{tl[x] || 'false'}" }
     trim = cur['trim'] || 'off'
     extra << "trim=#{trim}"
     Curses.addstr(extra.join(' '))
diff --git a/lib/dtas/player/client_handler.rb b/lib/dtas/player/client_handler.rb
index a6f5dba..7b33967 100644
--- a/lib/dtas/player/client_handler.rb
+++ b/lib/dtas/player/client_handler.rb
@@ -630,6 +630,20 @@ module DTAS::Player::ClientHandler # :nodoc:
     io.emit("tl shuffle #{prev}")
   end
 
+  def _dpc_tl_consume(io, msg)
+    prev = (!!@tl.consume).to_s
+    v = msg.shift
+    case v
+    when nil
+    else
+      set_bool(io, 'tl consume', v) do |b|
+        @tl.consume = b
+        __wall("tl consume #{b}")
+      end
+    end
+    io.emit("tl consume #{prev}")
+  end
+
   def _dpc_tl_max(io, msg)
     prev = @tl.max
     case msg.shift
diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb
index 6e0dd75..779b973 100644
--- a/lib/dtas/tracklist.rb
+++ b/lib/dtas/tracklist.rb
@@ -12,11 +12,13 @@ class DTAS::Tracklist # :nodoc:
   attr_accessor :repeat # true, false, 1
   attr_reader :shuffle  # false or shuffled @list
   attr_accessor :max # integer
+  attr_accessor :consume # boolean
 
   TL_DEFAULTS = {
     'pos' => -1,
     'repeat' => false,
     'max' => 20_000,
+    'consume' => false,
   }
   SIVS = TL_DEFAULTS.keys
 
@@ -116,20 +118,25 @@ class DTAS::Tracklist # :nodoc:
   def advance_track(repeat_ok = true)
     cur = @shuffle || @list
     return if cur.empty?
+    prev = cur[@pos] if @consume && @pos >= 0
     # @repeat == 1 for single track repeat
     repeat = repeat_ok ? @repeat : false
     next_pos = @goto_pos || @pos + (repeat == 1 ? 0 : 1)
     next_off = @goto_off # nil by default
     @goto_pos = @goto_off = nil
-    if cur[next_pos]
+
+    if nxt = cur[next_pos]
       @pos = next_pos
+      remove_track(prev.track_id) if prev
     else
+      remove_track(prev.track_id) if prev
       # reshuffle the tracklist when we've exhausted it
       cur.shuffle! if @shuffle
-      return unless repeat
+      return if !repeat || cur.empty?
       next_pos = @pos = 0
+      nxt = cur[0]
     end
-    [ cur[next_pos].to_path, next_off ]
+    [ nxt.to_path, next_off ]
   end
 
   def cur_track
@@ -189,14 +196,18 @@ class DTAS::Tracklist # :nodoc:
 
   def remove_track(track_id)
     idx = _idx_of(@list, track_id) or return false
+    track = @list.delete_at(idx)
     if @shuffle
-      si = _idx_of(@shuffle, track_id) or return false
-      @shuffle.delete_at(si)
+      idx = _idx_of(@shuffle, track_id) or return false
+      @shuffle.delete_at(idx)
     end
-    track = @list.delete_at(idx)
     len = @list.size
-    @pos = len - 1 if @pos >= len
-    @goto_pos = @goto_pos = nil # TODO: reposition?
+    if @pos >= len
+      @pos = len - 1
+    elsif idx <= @pos
+      @pos -= 1
+    end
+    @goto_pos = @goto_off = nil # TODO: reposition?
     track.to_path
   end