diff --git a/lib/playlist.rb b/lib/playlist.rb new file mode 100644 index 0000000..dd9e0fc --- /dev/null +++ b/lib/playlist.rb @@ -0,0 +1,63 @@ +require 'thread' +####### Queue methods +# << clear deq empty? +# enq length new num_waiting +# pop push shift size +class Queue + attr_reader :que +end + +class Playlist < Queue + + def insert(index,obj) + @que.insert(index,obj) # TODO + end + + def shuffle + @que.shuffle! # TODO + end + + def clear + @que = [] + end + + def move(cur_pos,new_pos) + cur_pos=cur_pos.to_i + new_pos=new_pos.to_i + if (@que.size<= cur_pos) || (@que.size<= new_pos) || (cur_pos<0) || (new_pos<0) + raise "Out of Bounds" + end + insert(new_pos,delete_at(cur_pos)) + end + + def copy(cur_pos,new_pos) + cur_pos=cur_pos.to_i + new_pos=new_pos.to_i + if (@que.size<= cur_pos) || (cur_pos<0) || (new_pos<0) + raise "Out of Bounds" + end + if (@que.size<= new_pos) + enq(@que.at(cur_pos)) + return @que.size + else + insert(new_pos,@que.at(cur_pos)) + return new_pos + end + end + + def delete_at(index) + @que.delete_at(index.to_i) # TODO + end + + def [](index) + @que[index] + end + + def pull(index) + # TODO + end +end + +if __FILE__ == $0 +# perhaps add some testing stuff here +end diff --git a/shoutr b/shoutr index 33b9e69..e6b09ed 100755 --- a/shoutr +++ b/shoutr @@ -38,7 +38,7 @@ ARGV.each do |filename| next unless target if File.extname(target).downcase == '.mp3' puts "got an mp3 file, trying new code" - cmd = ("mpg123 -q -w - '#{target}' | oggenc -c '#{target}' -Q -") + cmd = ("mpg123 -q -w - \"#{target}\" | oggenc -c \"#{target}\" -Q -") IO.popen(cmd,'r') { |p| send_io(s,p,filename) } elsif File.extname(target).downcase == '.ogg' puts "got an ogg file, using old code" diff --git a/spec/playlist_spec.rb b/spec/playlist_spec.rb new file mode 100644 index 0000000..fd32a65 --- /dev/null +++ b/spec/playlist_spec.rb @@ -0,0 +1,59 @@ +$: << File.join( File.dirname( __FILE__ ), '../lib' ) + +require 'rubygems' +require 'spec' +require 'playlist' +require 'pp' + +context "Testing shoutr playlist" do + setup do + @p = Playlist.new + @p.enq "a"; @p.enq "b"; @p.enq "c" + end + + specify "begins test with 3 elements and can be cleared" do + @p.size.should == 3 + @p.clear + @p.size.should == 0 + end + + specify "adding element should increase playlist size" do + @p.enq "d" + @p.size.should == 4 + @p.deq.should == "a" + @p.size.should == 3 + end + + specify "copying an element should move it" do + @p[0].should == "a" + @p[2].should == "c" + lambda {@p.move(2,0)}.should_not raise_error("Out of Bounds") + @p[0].should == "c" + @p[2].should == "b" + end + + specify "copying out of range should cause an error" do + lambda {@p.move(0,4)}.should raise_error("Out of Bounds") + end + + specify "removing an element" do + @p.delete_at(1) + @p[0].should == "a" + @p[1].should == "c" + @p.size.should == 2 + end + + specify "copying an element" do + @p.copy(0,3) # a b c a + @p[3].should == "a" + @p.copy(1,0) # b a b c a + @p[0].should == "b" + lambda {@p.copy(13,0)}.should raise_error("Out of Bounds") + end + + specify "shuffling test" do + @p.shuffle + pp @p + @p[0].should != "a" + end +end