Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/barr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
69 changes: 69 additions & 0 deletions lib/barr/blocks/mpd.rb
Original file line number Diff line number Diff line change
@@ -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
74 changes: 74 additions & 0 deletions spec/blocks/mpd_spec.rb
Original file line number Diff line number Diff line change
@@ -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