diff --git a/README.md b/README.md index 7275ee1..566bfc3 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,20 @@ There are no `Processes` block specific configurable options. | `buttons` | bool | As above, but for the player control buttons | `true` | | `title` | bool | As above, but for the track title | `true` | +#### MPD + +**Requires MPD server (mpd or mopidy) and MPC**. Shows currently playing artist and/or track, as well as control buttons. Control buttons use FontAwesome. + +`mpd = Barr::Blocks::MPD.new buttons: false` + +| Option | Value | Description | Default | +| --- | --- | --- | --- | +| `artist` | bool | Set to `true` or `false` to set whether or not the currently playing artist should be shown. | `true` | +| `buttons` | bool | As above, but for the player control buttons | `true` | +| `title` | bool | As above, but for the track title | `true` | + +_ps. Most of the code for this block was directly taken from the above Rhythmbox +block so most of the credit should go to them :wink:_ #### Separator @@ -339,7 +353,7 @@ block = Incrementer.new count: 1, align: :r Here are a few things I have planned -* MPD support +* ~~MPD support~~ * Powerline styling options * More configuration for existing blocks * Some form of Conky support diff --git a/lib/barr.rb b/lib/barr.rb index ba862e2..cfc67dd 100644 --- a/lib/barr.rb +++ b/lib/barr.rb @@ -13,6 +13,7 @@ require 'barr/blocks/mem' require 'barr/blocks/processes' require 'barr/blocks/rhythmbox' +require 'barr/blocks/mpd' require 'barr/blocks/temperature' require 'barr/blocks/whoami' require 'barr/blocks/separator' diff --git a/lib/barr/blocks/mpd.rb b/lib/barr/blocks/mpd.rb new file mode 100644 index 0000000..7e75201 --- /dev/null +++ b/lib/barr/blocks/mpd.rb @@ -0,0 +1,69 @@ +require 'barr/block' + +module Barr + module Blocks + class MPD < Block + attr_reader :view_opts + + def initialize(opts = {}) + super + reassign_deprecated_option opts, :show_artist, :artist + reassign_deprecated_option opts, :show_title, :title + reassign_deprecated_option opts, :show_buttons, :buttons + + @view_opts = { + artist: opts[:artist].nil? || opts[:artist], + buttons: opts[:buttons].nil? || opts[:buttons], + title: opts[:title].nil? || opts[:title] + } + end + + def update! + op = [] + + if @view_opts[:artist] || @view_opts[:title] + if(running?) + info = sys_cmd.split(' - ') + + if @view_opts[:artist] && @view_opts[:title] + op << info.join(' - ') + elsif @view_opts[:artist] + op << info[0] + elsif @view_opts[:title] + op << info[1] + end + else + op << 'None' + end + end + + op << buttons if @view_opts[:buttons] + + @output = op.join(' ') + + end + + def running? + if `pgrep mopidy`.chomp.length != 0 + true + else + `pgrep mpd`.chomp.length != 0 + end + end + + def buttons + [ + "%{A:mpc prev:}\uf048%{A}", + "%{A:mpc toggle:}\uf04b%{A}", + "%{A:mpc next:}\uf051%{A}" + ].join(' ').freeze + end + + private + + def sys_cmd + `mpc | sed -n 1p`.chomp + end + end + end +end diff --git a/spec/blocks/mpd_spec.rb b/spec/blocks/mpd_spec.rb new file mode 100644 index 0000000..cc849d8 --- /dev/null +++ b/spec/blocks/mpd_spec.rb @@ -0,0 +1,74 @@ +# coding: utf-8 +require 'barr/blocks/mpd' + +RSpec.describe Barr::Blocks::MPD do + + let(:sys_cmd) { 'Muse - Knights Of Cydonia' } + + before do + allow(subject).to receive(:running?).and_return(true) + allow(subject).to receive(:sys_cmd).and_return(sys_cmd) + end + + describe '#initialize' do + it 'sets default options' do + expect(subject.view_opts[:artist]).to eq true + expect(subject.view_opts[:buttons]).to eq true + expect(subject.view_opts[:title]).to eq true + end + end + + describe '#update!' do + context 'with everything enabled' do + before { subject.update! } + + it 'sets the output correctly' do + expect(subject.output).to eq('Muse - Knights Of Cydonia %{A:mpc prev:}%{A} %{A:mpc toggle:}%{A} %{A:mpc next:}%{A}') + end + end + + context 'with only artist enabled' do + subject { described_class.new title: false, buttons: false } + + before { subject.update! } + + it 'sets the output correctly' do + expect(subject.output).to eq('Muse') + end + end + + context 'with only title enabled' do + subject { described_class.new artist: false, buttons: false } + + before { subject.update! } + + it 'sets the output correctly' do + expect(subject.output).to eq('Knights Of Cydonia') + end + end + + context 'with only buttons enabled' do + subject { described_class.new title: false, artist: false } + + before { subject.update! } + + it 'sets the output correctly' do + expect(subject.output).to eq('%{A:mpc prev:}%{A} %{A:mpc toggle:}%{A} %{A:mpc next:}%{A}') + end + end + + context 'when nothing is playing' do + subject { described_class.new buttons: false } + + before do + allow(subject).to receive(:running?).and_return(false) + + subject.update! + end + + it 'sets the output correctly' do + expect(subject.output).to eq('None') + end + end + end +end