From 11d550f3df56499d772bb57c6680a238be6f01f8 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 24 Nov 2020 12:16:42 -0500 Subject: [PATCH] make shorthands more intuitive --- docs/shorthands.rst | 9 +++-- lib/squib/args/xywh_shorthands.rb | 55 ++++++++++++++----------------- lib/squib/version.rb | 3 +- samples/units/_shorthands.rb | 7 ++-- spec/args/box_spec.rb | 14 ++++---- spec/args/xywh_shorthands_spec.rb | 4 +-- 6 files changed, 45 insertions(+), 47 deletions(-) diff --git a/docs/shorthands.rst b/docs/shorthands.rst index 7bea3106..7a634efb 100644 --- a/docs/shorthands.rst +++ b/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 ``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 height for ``y`` and ``height`` -* You can offset from the middle by using +, -, and /, e.g. ``middle + 1in`` -* You can offset from the width or height using, e.g. ``width - 1in`` or ``height - 2mm`` -* Works with the ``cell`` unit as well, e.g. `middle + 1 cell`. See :doc:`units`. +* You can offset from the middle by using + or - operators, e.g. ``middle + 1in`` +* You can offset from the deck width or height using the + or - operators, e.g. ``deck - 1in`` or ``deck - 2mm`` +* 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. Note that the following are NOT supported: * 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 ------- diff --git a/lib/squib/args/xywh_shorthands.rb b/lib/squib/args/xywh_shorthands.rb index 15707a6b..017a10c8 100644 --- a/lib/squib/args/xywh_shorthands.rb +++ b/lib/squib/args/xywh_shorthands.rb @@ -3,49 +3,44 @@ module Squib module Args 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 def apply_shorthands(arg, deck, axis: :x) dimension = (axis == :x) ? deck.width : deck.height arg_s = arg.to_s case arg_s - when 'middle' + when MIDDLE_ONLY dimension / 2.0 - when 'center' - dimension / 2.0 - when 'deck' + when DECK_ONLY dimension - when WIDTH_MINUS_REGEX # e.g. width - 1.5in - n = arg_s.sub WIDTH_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, '' + when MIDDLE_MINUS_REGEX # e.g. width: middle - 3 + n = arg_s.sub MIDDLE_MINUS_REGEX, '' 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 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 - when MIDDLE_MINUS_REGEX # e.g. middle - 1.5in - n = arg_s.sub MIDDLE_MINUS_REGEX, '' - n = UnitConversion.parse(n, deck.dpi, deck.cell_px) - dimension / 2.0 - n + when DECK_MINUS_REGEX # e.g. width: deck - 1.5in + n = arg_s.sub DECK_MINUS_REGEX, '' + n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f + 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 arg end diff --git a/lib/squib/version.rb b/lib/squib/version.rb index 2afa08b8..c9c6a649 100644 --- a/lib/squib/version.rb +++ b/lib/squib/version.rb @@ -6,5 +6,6 @@ module Squib # 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 # - VERSION = '0.16.0-preview1' + VERSION = '0.16.0-preview2a' end + diff --git a/samples/units/_shorthands.rb b/samples/units/_shorthands.rb index 9d24e0f2..2d946c3f 100644 --- a/samples/units/_shorthands.rb +++ b/samples/units/_shorthands.rb @@ -20,14 +20,13 @@ # We can also do width-, height-, width/, height/ 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 # And middle+/- - - rect x: 'middle + 0.1in', y: 'middle - 0.1in', + rect x: 'middle + 0.1in', y: 'center - 0.1in', width: '0.1in', height: '0.1in', fill_color: :blue # Layouts apply this too. diff --git a/spec/args/box_spec.rb b/spec/args/box_spec.rb index dd5adf91..5804e4e6 100644 --- a/spec/args/box_spec.rb +++ b/spec/args/box_spec.rb @@ -152,14 +152,14 @@ expect(box).to have_attributes(width: [61.5], height: [228.0]) end - it 'listens to height/2' do - args = { width: 'height / 2', height: :deck } + it 'listens to deck/2' do + args = { width: 'deck / 2', height: :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 - it 'listens to width - 0.5in' do - args = { x: 'width - 0.5in'} + it 'listens to deck - 0.5in' do + args = { x: 'deck - 0.5in'} box = Squib::Args.extract_box args, deck expect(box).to have_attributes(x: [ 123 - 150 ]) end @@ -172,8 +172,8 @@ args = { x: 'middle + 1c', y: 'middle', - width: 'width - 2c', - height: 'height / 3' + width: 'deck - 2c', + height: 'deck / 3' } box = Squib::Args.extract_box args, deck expect(box).to have_attributes( diff --git a/spec/args/xywh_shorthands_spec.rb b/spec/args/xywh_shorthands_spec.rb index 7493ec37..ddfa18c9 100644 --- a/spec/args/xywh_shorthands_spec.rb +++ b/spec/args/xywh_shorthands_spec.rb @@ -9,8 +9,8 @@ args = { x: 'middle', y: 'middle + 1in', - width: 'width / 2', - height: 'height - 1in', + width: 'deck / 2', + height: 'deck - 1in', } box = Squib::Args.extract_box args, deck expect(box).to have_attributes({