diff --git a/i3blocks/mpc.rb b/i3blocks/mpc.rb new file mode 100644 index 0000000..c502d7f --- /dev/null +++ b/i3blocks/mpc.rb @@ -0,0 +1,65 @@ +class MPC + def initialize + @mods = Hash[run[2].scan(/(\w+): (off|on)/).map do |m| + [m.first.to_sym, m.last == 'on'] + end] + end + + def [](mode) + @mods[mode] + end + + def available + run.split("\n").size > 1 + end + + def status + run.split("\n")[1].match(/^\[(\w+)\]/)[1].to_sym + end + + def play + run 'play' + end + + def pause + run 'pause' + end + + def stop + run 'stop' + end + + def toggle + run 'toggle' + end + + def next + run 'next' + end + + def previous + run 'previous' + end + + def long + run('-f [[%artist% - ]%title%[ (%album%)]]|[%file%]').split("\n").first + end + + def short + run('-f "%title%"').split("\n").first + end + + def file + run('-f "%file%"').split("\n").first + end + + def to_s + run.split("\n").first + end + + private + + def run(command = '') + `mpc #{command}`.strip + end +end diff --git a/i3blocks/sharp-music.rb b/i3blocks/sharp-music.rb index fad5399..f5e5c88 100755 --- a/i3blocks/sharp-music.rb +++ b/i3blocks/sharp-music.rb @@ -1,6 +1,13 @@ #!/usr/bin/env ruby # encoding: utf-8 +require_relative 'mpc' +require_relative 'spotify' + +PLAYER = [MPC.new, Spotify.new].find { |p| p.available } + +exit unless PLAYER + ICONS = { playing: '', paused: '', stopped: '' }.freeze MODS = { repeat: '', random: '', single: '', consume: '' }.freeze @@ -9,36 +16,16 @@ def copy(s) Open3.popen2('xclip -sel c') { |i, o, w| i << s } end -def mpcf(f) - `mpc -f "#{f}"`.split("\n").first -end - -mouse = ENV['BLOCK_BUTTON'].to_i - -mpc = `mpc`.split "\n" -mpc_short = `mpc -f "%title%"`.split "\n" - -case mouse +case ENV['BLOCK_BUTTON'].to_i when 2 - copy mpcf '[[%artist% - ]%title%[ (%album%)]]|[%file%]' + copy PLAYER.long when 3 - copy mpcf '%file%' + copy PLAYER.file end -# If we get less than 3 lines of output, it means playback is stopped -exit if mpc.size < 3 - -mods = Hash[mpc[2].scan(/(\w+): (off|on)/).map do |m| - [m.first.to_sym, m.last == 'on'] -end] - -data = { - now_playing: mpc[0], - status: mpc[1].match(/^\[(\w+)\]/)[1].to_sym, - mods: [:repeat, :random, :single, :consume].map { |m| MODS[m] if mods[m] }.compact.join(' ') -} +mods = [:repeat, :random, :single, :consume].map { |m| MODS[m] if PLAYER[m] }.compact.join(' ') -print "#{ICONS[data[:status]]} #{data[:now_playing]}" -print " #{data[:mods]}" if data[:mods].size > 0 -puts "\n#{ICONS[data[:status]]} #{mpc_short[0]}" -puts '#a0a0a0' if data[:status] == :paused +print "#{ICONS[PLAYER.status]} #{PLAYER}" +print " #{mods}" if mods.size > 0 +puts "\n#{ICONS[PLAYER.status]} #{PLAYER.short}" +puts '#a0a0a0' if PLAYER.status == :paused diff --git a/i3blocks/spotify.rb b/i3blocks/spotify.rb new file mode 100644 index 0000000..78f105b --- /dev/null +++ b/i3blocks/spotify.rb @@ -0,0 +1,90 @@ +class Spotify + def [](_) + false + end + + def available + run('status') != 'Not available' + end + + def status + run('status').downcase.to_sym + end + + def play + run 'play' + end + + def pause + run 'pause' + end + + def stop + run 'stop' + end + + def toggle + run 'play-pause' + end + + def next + run 'next' + end + + def previous + run 'previous' + end + + def long + data = metadata + + return file if data[:title].empty? + + output = data[:title] + + output = "#{data[:artist]} - #{output}" unless data[:artist].empty? + output = "#{output} (#{data[:album]}" unless data[:album].empty? + + output + end + + def short + metadata[:title] + end + + def file + run 'metadata mpris:trackid' + end + + def to_s + data = metadata + return data[:title] if data[:artist].empty? + "#{data[:artist]} - #{data[:title]}" + end + + private + + def run(command) + `playerctl -p spotify #{command}`.strip + end + + def artist + run 'metadata artist' + end + + def title + run 'metadata title' + end + + def album + run 'metadata album' + end + + def metadata + { + artist: artist.strip, + title: title.strip, + album: album.strip + } + end +end