add support for ranges
1 2 3 4 5 6 7 8 9 10 11 12 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 46 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 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189
diff --git a/README.md b/README.md
index 6313fe3..6875f22 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,12 @@ Once you have an instance of the `MPDClient` class, start by connecting to the s
client.connect('localhost', 6600)
```
+or Unix domain socket
+
+```ruby
+client.connect('/var/run/mpd/socket')
+```
+
The client library can be used as follows:
```ruby
@@ -54,6 +60,48 @@ client.status # insert the status command into the list
client.command_list_end # result will be a Array with the results
```
+### Ranges
+
+Some commands(e.g. `move`, `delete`, `load`, `shuffle`, `playlistinfo`) support integer ranges(`[START:END]`) as argument. This is done in `mpd_client` by using two element array:
+
+```ruby
+# move the first three songs after the fifth number in the playlist
+client.move([0, 3], 5)
+```
+
+Second element can be omitted. MPD will assumes the biggest possible number then:
+
+```ruby
+# delete all songs from the current playlist, except for the firts ten
+client.delete([10,])
+```
+
+### Logging
+
+Default logger for all MPDClient instances:
+
+```ruby
+require 'logger'
+require 'mpd_client'
+
+MPDClient.log = Logger.new($stderr)
+
+client = MPDClient.new
+```
+
+Sets the logger used by this instance of MPDClient:
+
+```ruby
+require 'logger'
+require 'mpd_client'
+
+client = MPDClient.new
+client.log = Logger.new($stderr)
+```
+
+
+For more information about logging configuration, see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html
+
## Contributing
1. Fork it
@@ -64,4 +112,4 @@ client.command_list_end # result will be a Array with the results
## License
-This library is distributed under the MIT license. Please see the LICENSE file.
\ No newline at end of file
+This library is distributed under the MIT license. Please see the LICENSE file.
diff --git a/examples/range.rb b/examples/range.rb
new file mode 100644
index 0000000..50deebe
--- /dev/null
+++ b/examples/range.rb
@@ -0,0 +1,19 @@
+require 'bundler'
+Bundler.setup :default
+
+require 'pp'
+require 'logger'
+require 'mpd_client'
+
+MPDClient.log = Logger.new($stderr)
+
+client = MPDClient.new
+client.connect('localhost', 6600)
+
+# delete all songs from the current playlist, except for the firts ten
+client.delete([10,])
+
+# move the first three songs after the fifth number in the playlist
+client.move([0, 3], 69)
+
+pp client.playlistinfo([69, 71])
diff --git a/examples/stickers.rb b/examples/stickers.rb
index b4b0cd5..b56d58f 100644
--- a/examples/stickers.rb
+++ b/examples/stickers.rb
@@ -6,11 +6,12 @@ Bundler.setup :default
require 'logger'
require 'mpd_client'
-type = ARGV[0]
-what = ARGV[1]
+MPDClient.log = Logger.new($stderr)
+
+# Stickers
+# http://www.musicpd.org/doc/protocol/ch03s07.html
client = MPDClient.new
-client.log = Logger.new($stderr)
# Connecting to the server
client.connect('/var/run/mpd/socket')
@@ -44,7 +45,7 @@ puts client.sticker_find('song', '/', 'rating')
# sticker delete {TYPE} {URI} [NAME]
# Deletes a sticker value from the specified object. If you do not specify a sticker name, all sticker values are deleted.
#
-puts client.sticker_delete('song', uri, 'rating')
+client.sticker_delete('song', uri, 'rating')
client.close
client.disconnect
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 70872b1..063ffb2 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
require 'socket'
require "mpd_client/version"
@@ -155,6 +155,7 @@ class MPDClient
end
def connect(host = 'localhost', port = 6600)
+ log.info("MPD connect #{host}, #{port}") if log
if host.start_with?('/')
@socket = UNIXSocket.new(host)
hello
@@ -165,6 +166,7 @@ class MPDClient
end
def disconnect
+ log.info("MPD disconnect")
@socket.close
reset
end
@@ -208,14 +210,21 @@ class MPDClient
end
def write_line(line)
- log.debug("MPD command: #{line}") if log
@socket.puts line
@socket.flush
end
def write_command(command, *args)
parts = [command]
- args.each{|arg| parts << "\"#{escape(arg)}\""}
+ args.each do |arg|
+ if arg.kind_of?(Array)
+ parts << (arg.size == 1 ? "\"#{arg[0].to_i}:\"" : "\"#{arg[0].to_i}:#{arg[1].to_i}\"")
+ else
+ parts << "\"#{escape(arg)}\""
+ end
+ end
+ #log.debug("Calling MPD: #{command}#{args}") if log
+ log.debug("Calling MPD: #{parts.join(' ')}") if log
write_line(parts.join(' '))
end
diff --git a/lib/mpd_client/version.rb b/lib/mpd_client/version.rb
index c00860a..7955d49 100644
--- a/lib/mpd_client/version.rb
+++ b/lib/mpd_client/version.rb
@@ -1,5 +1,5 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
class MPDClient
- VERSION = "0.0.2"
+ VERSION = "0.0.3"
end