Browse Source

make shorthands more intuitive

dev
Andy Meneely 5 years ago
parent
commit
11d550f3df
  1. 9
      docs/shorthands.rst
  2. 55
      lib/squib/args/xywh_shorthands.rb
  3. 3
      lib/squib/version.rb
  4. 7
      samples/units/_shorthands.rb
  5. 14
      spec/args/box_spec.rb
  6. 4
      spec/args/xywh_shorthands_spec.rb

9
docs/shorthands.rst

@ -5,17 +5,20 @@ For the arguments ``x``, ``y``, ``width``, and ``height``, a few convenient shor
* ``middle`` for ``x`` and ``width`` refer to the deck's width / 2 * ``middle`` for ``x`` and ``width`` refer to the deck's width / 2
* ``middle`` for ``y`` and ``height`` refer to the deck's height / 2 * ``middle`` for ``y`` and ``height`` refer to the deck's height / 2
* The word ``center`` behaves the same way
* ``deck`` refers to the deck's width for ``x`` and ``width`` * ``deck`` refers to the deck's width for ``x`` and ``width``
* ``deck`` refers to the deck's height for ``y`` and ``height`` * ``deck`` refers to the deck's height for ``y`` and ``height``
* You can offset from the middle by using +, -, and /, e.g. ``middle + 1in`` * You can offset from the middle by using + or - operators, e.g. ``middle + 1in``
* You can offset from the width or height using, e.g. ``width - 1in`` or ``height - 2mm`` * You can offset from the deck width or height using the + or - operators, e.g. ``deck - 1in`` or ``deck - 2mm``
* Works with the ``cell`` unit as well, e.g. `middle + 1 cell`. See :doc:`units`. * You can offset from the deck width or height using, e.g. ``deck / 3``
* Works with all unit conversion too, e.g. `middle + 1 cell`. See :doc:`units`.
These are all passed as strings. So you will need to quote them in Ruby, or just plain in your layout YAML. These are all passed as strings. So you will need to quote them in Ruby, or just plain in your layout YAML.
Note that the following are NOT supported: Note that the following are NOT supported:
* The `+=` operator when using `extends` in a layout file * The `+=` operator when using `extends` in a layout file
* Complicated formulas. We're not evaluating this as code, we're looking for these specific patterns and applying them. Anything more complicated you'll have to handle with Ruby code.
Samples Samples
------- -------

55
lib/squib/args/xywh_shorthands.rb

@ -3,49 +3,44 @@ require_relative 'unit_conversion'
module Squib module Squib
module Args module Args
module XYWHShorthands module XYWHShorthands
WIDTH_MINUS_REGEX = /^width\s*\-\s*/
HEIGHT_MINUS_REGEX = /^height\s*\-\s*/
WIDTH_DIV_REGEX = /^width\s*\/\s*/
HEIGHT_DIV_REGEX = /^height\s*\/\s*/
MIDDLE_PLUS_REGEX = /^middle\s*\+\s*/
MIDDLE_MINUS_REGEX = /^middle\s*\-\s*/
MIDDLE_ONLY = /^(middle|center)\s*$/
DECK_ONLY = /^deck\s*$/
MIDDLE_MINUS_REGEX = /^(middle|center)\s*\-\s*/
MIDDLE_PLUS_REGEX = /^(middle|center)\s*\+\s*/
DECK_MINUS_REGEX = /^deck\s*\-\s*/
DECK_PLUS_REGEX = /^deck\s*\+\s*/
DECK_DIV_REGEX = /^deck\s*\/\s*/
# dimension is usually either deck_width or deck_height # dimension is usually either deck_width or deck_height
def apply_shorthands(arg, deck, axis: :x) def apply_shorthands(arg, deck, axis: :x)
dimension = (axis == :x) ? deck.width : deck.height dimension = (axis == :x) ? deck.width : deck.height
arg_s = arg.to_s arg_s = arg.to_s
case arg_s case arg_s
when 'middle' when MIDDLE_ONLY
dimension / 2.0 dimension / 2.0
when 'center' when DECK_ONLY
dimension / 2.0
when 'deck'
dimension dimension
when WIDTH_MINUS_REGEX # e.g. width - 1.5in when MIDDLE_MINUS_REGEX # e.g. width: middle - 3
n = arg_s.sub WIDTH_MINUS_REGEX, '' n = arg_s.sub MIDDLE_MINUS_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
deck.width - n
when HEIGHT_MINUS_REGEX # e.g. height - 1.5in
n = arg_s.sub HEIGHT_MINUS_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
deck.height - n
when WIDTH_DIV_REGEX # e.g. width / 3
n = arg_s.sub WIDTH_DIV_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
deck.width / n
when HEIGHT_DIV_REGEX # e.g. height / 3
n = arg_s.sub HEIGHT_DIV_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
deck.height / n dimension / 2.0 - n
when MIDDLE_PLUS_REGEX # e.g. middle + 1.5in when MIDDLE_PLUS_REGEX # e.g. middle + 1.5in
n = arg_s.sub MIDDLE_PLUS_REGEX, '' n = arg_s.sub MIDDLE_PLUS_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px) n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
dimension / 2.0 + n dimension / 2.0 + n
when MIDDLE_MINUS_REGEX # e.g. middle - 1.5in when DECK_MINUS_REGEX # e.g. width: deck - 1.5in
n = arg_s.sub MIDDLE_MINUS_REGEX, '' n = arg_s.sub DECK_MINUS_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px) n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
dimension / 2.0 - n dimension - n
when DECK_PLUS_REGEX # e.g. deck + 1.5in (which is weird but ok)
n = arg_s.sub DECK_PLUS_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
dimension + n
when DECK_DIV_REGEX # e.g. width: deck/3
n = arg_s.sub DECK_DIV_REGEX, ''
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
dimension / n
else else
arg arg
end end

3
lib/squib/version.rb

@ -6,5 +6,6 @@ module Squib
# Most of the time this is in the alpha of the next release. # Most of the time this is in the alpha of the next release.
# e.g. v0.0.5a is on its way to becoming v0.0.5 # e.g. v0.0.5a is on its way to becoming v0.0.5
# #
VERSION = '0.16.0-preview1' VERSION = '0.16.0-preview2a'
end end

7
samples/units/_shorthands.rb

@ -20,14 +20,13 @@ Squib::Deck.new(width: '0.5in', height: '0.25in') do
# We can also do width-, height-, width/, height/ # We can also do width-, height-, width/, height/
rect x: 20, y: 5, stroke_color: :green, rect x: 20, y: 5, stroke_color: :green,
width: 'width - 0.1in', height: 10 width: 'deck - 0.1in', height: 10
rect x: 10, y: 50, width: 10, height: 'height / 3', rect x: 10, y: 50, width: 10, height: 'deck / 3',
stroke_color: :purple stroke_color: :purple
# And middle+/- # And middle+/-
rect x: 'middle + 0.1in', y: 'center - 0.1in',
rect x: 'middle + 0.1in', y: 'middle - 0.1in',
width: '0.1in', height: '0.1in', fill_color: :blue width: '0.1in', height: '0.1in', fill_color: :blue
# Layouts apply this too. # Layouts apply this too.

14
spec/args/box_spec.rb

@ -152,14 +152,14 @@ describe Squib::Args::Box do
expect(box).to have_attributes(width: [61.5], height: [228.0]) expect(box).to have_attributes(width: [61.5], height: [228.0])
end end
it 'listens to height/2' do it 'listens to deck/2' do
args = { width: 'height / 2', height: :deck } args = { width: 'deck / 2', height: :deck }
box = Squib::Args.extract_box args, deck box = Squib::Args.extract_box args, deck
expect(box).to have_attributes(width: [228.0], height: [456]) expect(box).to have_attributes(width: [61.5], height: [456])
end end
it 'listens to width - 0.5in' do it 'listens to deck - 0.5in' do
args = { x: 'width - 0.5in'} args = { x: 'deck - 0.5in'}
box = Squib::Args.extract_box args, deck box = Squib::Args.extract_box args, deck
expect(box).to have_attributes(x: [ 123 - 150 ]) expect(box).to have_attributes(x: [ 123 - 150 ])
end end
@ -172,8 +172,8 @@ describe Squib::Args::Box do
args = { args = {
x: 'middle + 1c', x: 'middle + 1c',
y: 'middle', y: 'middle',
width: 'width - 2c', width: 'deck - 2c',
height: 'height / 3' height: 'deck / 3'
} }
box = Squib::Args.extract_box args, deck box = Squib::Args.extract_box args, deck
expect(box).to have_attributes( expect(box).to have_attributes(

4
spec/args/xywh_shorthands_spec.rb

@ -9,8 +9,8 @@ describe Squib::Args::XYWHShorthands do
args = { args = {
x: 'middle', x: 'middle',
y: 'middle + 1in', y: 'middle + 1in',
width: 'width / 2', width: 'deck / 2',
height: 'height - 1in', height: 'deck - 1in',
} }
box = Squib::Args.extract_box args, deck box = Squib::Args.extract_box args, deck
expect(box).to have_attributes({ expect(box).to have_attributes({

Loading…
Cancel
Save