cosmetic changes
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
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 6a41dd7..c57b9dc 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -161,6 +161,7 @@ module MPD
def add_command(name, retval)
escaped_name = name.tr(' ', '_')
+
define_method escaped_name.to_sym do |*args|
ensure_connected
@@ -190,11 +191,12 @@ module MPD
def reconnect
log&.info("MPD (re)connect #{@host}, #{@port}")
- @socket = if @host.start_with?('/')
- UNIXSocket.new(@host)
- else
- TCPSocket.new(@host, @port)
- end
+ @socket =
+ if @host.start_with?('/')
+ UNIXSocket.new(@host)
+ else
+ TCPSocket.new(@host, @port)
+ end
hello
@connected = true
@@ -223,6 +225,7 @@ module MPD
raise 'Already in command list' unless @command_list.nil?
write_command('command_list_ok_begin')
+
@command_list = []
end
@@ -271,20 +274,24 @@ module MPD
reconnect
@socket.puts line
end
+
@socket.flush
end
def write_command(command, *args)
parts = [command]
+
args.each do |arg|
- line = if arg.is_a?(Array)
- arg.size == 1 ? "\"#{arg[0].to_i}:\"" : "\"#{arg[0].to_i}:#{arg[1].to_i}\""
- else
- "\"#{escape(arg)}\""
- end
+ line =
+ if arg.is_a?(Array)
+ arg.size == 1 ? "\"#{arg[0].to_i}:\"" : "\"#{arg[0].to_i}:#{arg[1].to_i}\""
+ else
+ "\"#{escape(arg)}\""
+ end
parts << line
end
+
# log.debug("Calling MPD: #{command}#{args}") if log
log&.debug("Calling MPD: #{parts.join(' ')}")
write_line(parts.join(' '))
@@ -312,6 +319,7 @@ module MPD
def read_pair(separator)
line = read_line
+
return if line.nil?
line.split(separator, 2)
@@ -319,7 +327,9 @@ module MPD
def read_pairs(separator = ': ')
result = []
+
pair = read_pair(separator)
+
while pair
result << pair
pair = read_pair(separator)
@@ -330,6 +340,7 @@ module MPD
def fetch_item
pairs = read_pairs
+
return nil if pairs.size != 1
pairs[0][1]
@@ -337,6 +348,7 @@ module MPD
def fetch_nothing
line = read_line
+
raise "Got unexpected value: #{line}" unless line.nil?
end
@@ -461,6 +473,7 @@ module MPD
def fetch_playlist
result = []
+
read_pairs(':').each do |_key, value|
value = value.chomp.force_encoding('utf-8')
result << value
@@ -471,6 +484,7 @@ module MPD
def fetch_stickers
result = []
+
read_pairs.each do |_key, sticker|
value = sticker.split('=', 2)
raise "Could now parse sticker: #{sticker}" if value.size < 2
@@ -487,6 +501,7 @@ module MPD
def fetch_command_list
result = []
+
begin
@command_list.each do |retval|
result << (eval retval)
@@ -500,9 +515,11 @@ module MPD
def hello
line = @socket.gets
+
raise 'Connection lost while reading MPD hello' unless line.end_with?("\n")
line.chomp!
+
raise "Got invalid MPD hello: #{line}" unless line.start_with?(HELLO_PREFIX)
@mpd_version = line[/#{HELLO_PREFIX}(.*)/, 1]