From 6c313204118890b05350615d1c9ad29903055cfd Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 5 Aug 2014 21:53:03 -0400 Subject: [PATCH] Custom colors, closing #1 --- API.md | 15 +++++++++++++++ lib/squib/constants.rb | 3 ++- lib/squib/deck.rb | 2 ++ lib/squib/input_helpers.rb | 8 ++++---- lib/squib/project_template/config.yml | 4 ++++ samples/custom-config.yml | 4 +++- samples/custom_config.rb | 5 ++++- spec/input_helpers_spec.rb | 17 ++++++++++++++++- 8 files changed, 50 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index f946c02..83222e1 100644 --- a/API.md +++ b/API.md @@ -66,3 +66,18 @@ Layouts also allow for a special `extends` field that will copy all of the setti See the `use_layout` sample found [here](https://github.com/andymeneely/squib/tree/master/samples/) {include:file:samples/use_layout.rb} + +# Configuration File + +Squib supports various configuration properties that can be specified in an external file. The `config:` option in `Deck.new` can specify an optional configuration file in YML format. The properties there are intended to be immutable for the life of the Deck. The options include: + +* `progress_bars` (Boolean, default: false). When set to `true`, long-running operations will show a progress bar on the command line. +* `dpi` (Integer, default: 300). Used in calculations when units are used (e.g. for PDF rendering and unit conversion). +* `hint` (ColorString, default: nil). Text hints are used to show the boundaries of text boxes. Can be enabled/disabled for individual commands, or set globally with the `set` command. This setting is overriden by `set` and individual commands. +* `custom_colors` (Hash of Colors, default: {}). Defines globally-available colors available to the deck that can be specified in commands. + +The following sample demonstrates the config file. + +See the `custom_config` sample found [here](https://github.com/andymeneely/squib/tree/master/samples/) + +{include:file:samples/custom_config.rb} \ No newline at end of file diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index f7bf4b0..9a1e7f3 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -46,7 +46,8 @@ module Squib CONFIG_DEFAULTS = { 'dpi' => 300, 'progress_bar' => false, - 'hint' => nil + 'hint' => nil, + 'custom_colors' => {} } end \ No newline at end of file diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index fcef644..d3009e6 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -52,6 +52,7 @@ module Squib @dpi = dpi @font = Squib::SYSTEM_DEFAULTS[:default_font] @cards = [] + @custom_colors = {} @progress_bar = Squib::Progress.new(false) cards.times{ @cards << Squib::Card.new(self, width, height) } load_config(config) @@ -88,6 +89,7 @@ module Squib @dpi = config['dpi'].to_i @text_hint = config['text_hint'] @progress_bar.enabled = config['progress_bars'] + @custom_colors = config['custom_colors'] end end diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index 767ec42..0f9d433 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -97,11 +97,11 @@ module Squib # :nodoc: # @api private def colorify(opts, nillable=false) - if nillable # for optional color arguments like text hints - opts[:color] = Cairo::Color.parse(opts[:color]) unless opts[:color].nil? - else - opts[:color] = Cairo::Color.parse(opts[:color]) + return opts if nillable && opts[:color].nil? + if @custom_colors.key? opts[:color].to_s + opts[:color] = @custom_colors[opts[:color].to_s] end + opts[:color] = Cairo::Color.parse(opts[:color]) opts end module_function :colorify diff --git a/lib/squib/project_template/config.yml b/lib/squib/project_template/config.yml index 4300efd..33c8bec 100644 --- a/lib/squib/project_template/config.yml +++ b/lib/squib/project_template/config.yml @@ -10,3 +10,7 @@ # Show progress bars on the command line for potentially long-running operations #progress_bars: true + +#Enable some custom colors that can be used in any color +#custom_colors: +# foo: '#abc' \ No newline at end of file diff --git a/samples/custom-config.yml b/samples/custom-config.yml index cb26a29..e593144 100644 --- a/samples/custom-config.yml +++ b/samples/custom-config.yml @@ -1,3 +1,5 @@ dpi: 300 progress_bars: true -text_hint: '#FF0000' \ No newline at end of file +text_hint: '#FF0000' +custom_colors: + foo: '#ccc' \ No newline at end of file diff --git a/samples/custom_config.rb b/samples/custom_config.rb index 010eb52..9a4d828 100644 --- a/samples/custom_config.rb +++ b/samples/custom_config.rb @@ -2,7 +2,10 @@ require 'squib' Squib::Deck.new(config: 'custom-config.yml') do - + + # Custom color defined in our config + background color: :foo + # Hints are turned on in the config file text str: "The Title", x: 0, y: 78, width: 825, font: 'Arial 72', align: :center diff --git a/spec/input_helpers_spec.rb b/spec/input_helpers_spec.rb index 56e3cb0..8ca1344 100644 --- a/spec/input_helpers_spec.rb +++ b/spec/input_helpers_spec.rb @@ -3,7 +3,7 @@ require 'squib/input_helpers' class DummyDeck include Squib::InputHelpers - attr_accessor :layout, :cards + attr_accessor :layout, :cards, :custom_colors end module Squib @@ -20,6 +20,7 @@ describe Squib::InputHelpers do @deck = DummyDeck.new @deck.layout = {'blah' => {x: 25}} @deck.cards = %w(a b) + @deck.custom_colors = {} end context '#layoutify' do @@ -75,6 +76,20 @@ describe Squib::InputHelpers do color = @deck.send(:colorify, {color: '#fff'}, true)[:color] expect(color.to_a).to eq([1.0, 1.0, 1.0, 1.0]) end + + it "raises and error if the color doesn't exist" do + expect{ @deck.send(:colorify, {color: :nonexist}, false) }.to raise_error(ArgumentError, "unknown color name: nonexist") + end + + it "pulls from config's custom colors" do + @deck.custom_colors['foo'] = "#abc" + expect(@deck.send(:colorify, {color: :foo}, false)[:color].to_s).to eq('#AABBCCFF') + end + + it "pulls from config's custom colors even when a string" do + @deck.custom_colors['foo'] = "#abc" + expect(@deck.send(:colorify, {color: 'foo'}, false)[:color].to_s).to eq('#AABBCCFF') + end end end \ No newline at end of file