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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ Grabs a piece of text from a URL based on a css or xpath selector. Optionally op
| `type` | Symbol | Set to `:css` or `:xpath` to set which type of selector you have provided | `:css` |
| `url` | String | URL that you'd like to grab from | **REQUIRED** |

#### HueLight

**Requires [Hue](https://github.com/soffes/hue) gem to be installed and configured prior to use** Allows you to set buttons for controlling a single Philips Hue Light.

| Option | Value | Description | Default |
| --- | --- | --- | --- |
| `id` | `hue` light ID | You can list all of your lights and their IDs by running `hue lights`. The ID is the first column, e.g. `1`. | **REQUIRED** |
| `format` | string | Configurable format for

#### I3

**Requires i3wm**. Shows the current workspaces and highlights the active one. You can click a workspace name to change to there.
Expand Down
1 change: 1 addition & 0 deletions barr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "weather-api", "1.2.0"
spec.add_runtime_dependency "nokogiri", "~> 1.6"
spec.add_runtime_dependency "poltergeist", "~> 1.11"
spec.add_runtime_dependency "hue", "~> 0.2"

spec.requirements << "Lemonbar with XFT support (https://github.com/krypt-n/bar)"
spec.requirements << "(Optional) I3 for Workspace support"
Expand Down
14 changes: 14 additions & 0 deletions examples/hue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'barr'

@man = Barr::Manager.new

light = Barr::Blocks::HueLight.new id: "5",
icon: "\uf0eb",
format: "${OFF:T-Turn Off} ${ON} ${ON:B-50,T-Dim 25%} ${ON:A-lselect,T-Alert!}"

@man.add light

@man.run!
1 change: 1 addition & 0 deletions lib/barr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'barr/blocks/cpu'
require 'barr/blocks/hdd'
require 'barr/blocks/http_grab'
require 'barr/blocks/hue_light'
require 'barr/blocks/i3'
require 'barr/blocks/ip'
require 'barr/blocks/mem'
Expand Down
16 changes: 12 additions & 4 deletions lib/barr/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@ def tmp_filename
return @tmp_filename
end

def format_string_from_hash(hash)
formatted = @format.clone
matches = @format.scan(/([\$][\{](\w+)[\}])/)
def wrap_button text, action
"%{A:#{action}:}#{text}%{A}"
end

def format_string_from_hash(hash, sender=nil)
formatted = @format.clone
matches = @format.scan(/([\$][\{](\w+)(:?([^:\}]+)?)[\}])/)
# binding.pry
matches.each do |match|
key = match[1].downcase.to_sym
if hash.has_key? key
sub = hash[key]
if !match[3].nil? && sender && sender.respond_to?(:additions_for_format)
sub = sender.additions_for_format(key, match[3])
else
sub = hash[key]
end
else
sub = ""
end
Expand Down
81 changes: 81 additions & 0 deletions lib/barr/blocks/hue_light.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'barr/block'

module Barr
module Blocks
class HueLight < Block
attr_accessor :id

def initialize(opts = {})
super

@id = opts[:id]
@format = opts[:format] || "${ON} ${OFF}"
end

def update!
@output = format_string_from_hash(base_options, self)
end

def additions_for_format(key, option_str)
agg = []
text = ""
options = option_str.split(/(?<!\\),/)

if options.nil?
case key.downcase
when :on
agg << 'on'
text = "on"
when :off
agg << 'off'
text = "off"
end

else
options.each do |option|
option = option.split(/(?<!\\)-/)

case option[0].downcase
when 'b'
agg << "--brightness #{option[1]}"
when 'h'
agg << "--hue #{option[1]}"
when 'a'
agg << "--alert #{option[1]}"
when 't'
text = option[1]
end
end

end

if text.empty?
text = key == :off ? "off" : "on"
end

if agg.empty?
agg << key == :off ? "off" : "on"
end

return wrap_button("[#{text}]", "#{sys_cmd} #{agg.join(" ")}")
end

private

def base_options
@base_options ||= {}

return @base_options unless @base_options.empty?

@base_options[:on] = wrap_button("[on]", "#{sys_cmd} on")
@base_options[:off] = wrap_button("[off]", "#{sys_cmd} off")
return @base_options
end

def sys_cmd
"hue light #{@id}"
end

end
end
end
63 changes: 63 additions & 0 deletions spec/blocks/hue_light_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# coding: utf-8
require 'barr/blocks/hue_light'

RSpec.describe Barr::Blocks::HueLight do

describe '#update!' do
before do
subject.id = 5
end

describe "the output" do
it 'sets the default off button correctly' do
subject.format = '${OFF}'
subject.update!
expect(subject.output).to eq '%{A:hue light 5 off:}[off]%{A}'
end

it 'sets the default on button correctly' do
subject.format = '${ON}'
subject.update!
expect(subject.output).to eq '%{A:hue light 5 on:}[on]%{A}'
end

it "sets custom text correctly" do
subject.format = '${OFF:T-custom off}'
subject.update!
expect(subject.output).to eq '%{A:hue light 5 off:}[custom off]%{A}'
end

it "sets hue correctly" do
subject.format = '${ON:H-1500}'
subject.update!
expect(subject.output).to eq '%{A:hue light 5 --hue 1500:}[on]%{A}'
end

it "sets alerts correctly" do
subject.format = '${ON:A-lselect}'
subject.update!
expect(subject.output).to eq '%{A:hue light 5 --alert lselect:}[on]%{A}'
end

it "sets brightness correctly" do
subject.format = '${ON:B-25}'
subject.update!
expect(subject.output).to eq '%{A:hue light 5 --brightness 25:}[on]%{A}'
end

it "sets multiple options correctly" do
subject.format = "${ON:B-200,T-Quite Bright,H-65000}"
subject.update!
expect(subject.output).to eq '%{A:hue light 5 --brightness 200 --hue 65000:}[Quite Bright]%{A}'
end

it "sets multiple buttons correctly" do
subject.format = "${OFF:T-turn off} ${ON}"
subject.update!
expect(subject.output).to eq("%{A:hue light 5 off:}[turn off]%{A} %{A:hue light 5 on:}[on]%{A}")
end

end
end

end