Browse Source

Custom colors, closing #1

dev
Andy Meneely 12 years ago
parent
commit
6c31320411
  1. 15
      API.md
  2. 3
      lib/squib/constants.rb
  3. 2
      lib/squib/deck.rb
  4. 8
      lib/squib/input_helpers.rb
  5. 4
      lib/squib/project_template/config.yml
  6. 4
      samples/custom-config.yml
  7. 5
      samples/custom_config.rb
  8. 17
      spec/input_helpers_spec.rb

15
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}

3
lib/squib/constants.rb

@ -46,7 +46,8 @@ module Squib
CONFIG_DEFAULTS = {
'dpi' => 300,
'progress_bar' => false,
'hint' => nil
'hint' => nil,
'custom_colors' => {}
}
end

2
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

8
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

4
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'

4
samples/custom-config.yml

@ -1,3 +1,5 @@
dpi: 300
progress_bars: true
text_hint: '#FF0000'
text_hint: '#FF0000'
custom_colors:
foo: '#ccc'

5
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

17
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
Loading…
Cancel
Save