From e20ce96ef2b0416e113955f227418eae5d4a7128 Mon Sep 17 00:00:00 2001 From: Dave Russell Date: Tue, 8 Nov 2016 04:18:05 +0000 Subject: [PATCH] Added HueLight Block --- README.md | 9 ++++ barr.gemspec | 1 + examples/hue.rb | 14 ++++++ lib/barr.rb | 1 + lib/barr/block.rb | 16 +++++-- lib/barr/blocks/hue_light.rb | 81 +++++++++++++++++++++++++++++++++++ spec/blocks/hue_light_spec.rb | 63 +++++++++++++++++++++++++++ 7 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 examples/hue.rb create mode 100644 lib/barr/blocks/hue_light.rb create mode 100644 spec/blocks/hue_light_spec.rb diff --git a/README.md b/README.md index 70bd4fa..c5bba6d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/barr.gemspec b/barr.gemspec index a360215..2cdb0dd 100644 --- a/barr.gemspec +++ b/barr.gemspec @@ -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" diff --git a/examples/hue.rb b/examples/hue.rb new file mode 100644 index 0000000..6ef032d --- /dev/null +++ b/examples/hue.rb @@ -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! diff --git a/lib/barr.rb b/lib/barr.rb index fb8c18e..4c7156f 100644 --- a/lib/barr.rb +++ b/lib/barr.rb @@ -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' diff --git a/lib/barr/block.rb b/lib/barr/block.rb index aef40f0..8cdc5ce 100644 --- a/lib/barr/block.rb +++ b/lib/barr/block.rb @@ -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 diff --git a/lib/barr/blocks/hue_light.rb b/lib/barr/blocks/hue_light.rb new file mode 100644 index 0000000..0ba4af1 --- /dev/null +++ b/lib/barr/blocks/hue_light.rb @@ -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(/(?