From 1ef187a5bd73932d00ce9cca79fcc87e305fe38f Mon Sep 17 00:00:00 2001 From: Javier Pollak Date: Sat, 9 Jul 2016 17:40:22 -0400 Subject: [PATCH 1/3] Added MPD block --- README.md | 14 ++++++++ lib/barr.rb | 1 + lib/barr/blocks/mpd.rb | 69 ++++++++++++++++++++++++++++++++++++++ spec/blocks/mpd_spec.rb | 74 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 lib/barr/blocks/mpd.rb create mode 100644 spec/blocks/mpd_spec.rb diff --git a/README.md b/README.md index 7275ee1..d7c00c9 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 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..337b3ed --- /dev/null +++ b/spec/blocks/mpd_spec.rb @@ -0,0 +1,74 @@ +# coding: utf-8 +require 'barr/blocks/rhythmbox' + +RSpec.describe Barr::Blocks::MPD do + + let(:sys_cmd) { 'Marilyn Manson - Into The Fire' } + + 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('Marilyn Manson - Into The Fire %{A:rhythmbox-client --previous:}%{A} %{A:rhythmbox-client --play-pause:}%{A} %{A:rhythmbox-client --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('Marilyn Manson') + 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('Into The Fire') + 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:rhythmbox-client --previous:}%{A} %{A:rhythmbox-client --play-pause:}%{A} %{A:rhythmbox-client --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 From 3dd476653780051291b96b3cac01c803900e5e56 Mon Sep 17 00:00:00 2001 From: Javier Pollak Date: Sat, 9 Jul 2016 17:43:53 -0400 Subject: [PATCH 2/3] ;) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d7c00c9..566bfc3 100644 --- a/README.md +++ b/README.md @@ -353,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 From bd4decfca77c4b1f9817ac3629e92317713de3fb Mon Sep 17 00:00:00 2001 From: Javier Pollak Date: Sat, 9 Jul 2016 18:01:39 -0400 Subject: [PATCH 3/3] Fixed the spec file Its rakes with no errors --- spec/blocks/mpd_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/blocks/mpd_spec.rb b/spec/blocks/mpd_spec.rb index 337b3ed..cc849d8 100644 --- a/spec/blocks/mpd_spec.rb +++ b/spec/blocks/mpd_spec.rb @@ -1,9 +1,9 @@ # coding: utf-8 -require 'barr/blocks/rhythmbox' +require 'barr/blocks/mpd' RSpec.describe Barr::Blocks::MPD do - let(:sys_cmd) { 'Marilyn Manson - Into The Fire' } + let(:sys_cmd) { 'Muse - Knights Of Cydonia' } before do allow(subject).to receive(:running?).and_return(true) @@ -23,7 +23,7 @@ before { subject.update! } it 'sets the output correctly' do - expect(subject.output).to eq('Marilyn Manson - Into The Fire %{A:rhythmbox-client --previous:}%{A} %{A:rhythmbox-client --play-pause:}%{A} %{A:rhythmbox-client --next:}%{A}') + expect(subject.output).to eq('Muse - Knights Of Cydonia %{A:mpc prev:}%{A} %{A:mpc toggle:}%{A} %{A:mpc next:}%{A}') end end @@ -33,7 +33,7 @@ before { subject.update! } it 'sets the output correctly' do - expect(subject.output).to eq('Marilyn Manson') + expect(subject.output).to eq('Muse') end end @@ -43,7 +43,7 @@ before { subject.update! } it 'sets the output correctly' do - expect(subject.output).to eq('Into The Fire') + expect(subject.output).to eq('Knights Of Cydonia') end end @@ -53,7 +53,7 @@ before { subject.update! } it 'sets the output correctly' do - expect(subject.output).to eq('%{A:rhythmbox-client --previous:}%{A} %{A:rhythmbox-client --play-pause:}%{A} %{A:rhythmbox-client --next:}%{A}') + expect(subject.output).to eq('%{A:mpc prev:}%{A} %{A:mpc toggle:}%{A} %{A:mpc next:}%{A}') end end