laser-cutter 2.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/pages.yml +44 -0
  3. data/.gitignore +2 -0
  4. data/.openlanes/config.yml +106 -0
  5. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/spec.md +40 -0
  6. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/state.json +337 -0
  7. data/.plans/004.01/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257sloped-box-panels/plan.md +91 -0
  8. data/.plans/decisions.md +11 -0
  9. data/CHANGELOG.md +134 -0
  10. data/CLAUDE.md +18 -6
  11. data/Gemfile +2 -1
  12. data/README.md +223 -50
  13. data/Rakefile +11 -7
  14. data/docs/badges/coverage_badge.svg +2 -2
  15. data/docs/decisions/decisions-on-angled-box.md +93 -0
  16. data/docs/decisions/stack.md +6 -0
  17. data/docs/images/box-3d.avif +0 -0
  18. data/docs/images/box-sloped-3d.avif +0 -0
  19. data/docs/images/box-sloped.avif +0 -0
  20. data/justfile +24 -8
  21. data/laser-cutter.gemspec +1 -1
  22. data/lib/laser/cutter/aggregator.rb +28 -0
  23. data/lib/laser/cutter/box.rb +95 -5
  24. data/lib/laser/cutter/box_factory.rb +15 -0
  25. data/lib/laser/cutter/cli/command.rb +27 -1
  26. data/lib/laser/cutter/cli/config_file.rb +2 -0
  27. data/lib/laser/cutter/cli/examples.rb +3 -0
  28. data/lib/laser/cutter/cli/generate.rb +37 -6
  29. data/lib/laser/cutter/cli/help.rb +4 -0
  30. data/lib/laser/cutter/cli/page_sizes.rb +3 -0
  31. data/lib/laser/cutter/cli/version.rb +2 -0
  32. data/lib/laser/cutter/cli.rb +23 -4
  33. data/lib/laser/cutter/configuration.rb +38 -4
  34. data/lib/laser/cutter/geometry/dimensions.rb +7 -0
  35. data/lib/laser/cutter/geometry/line.rb +42 -0
  36. data/lib/laser/cutter/geometry/loop.rb +138 -0
  37. data/lib/laser/cutter/geometry/point.rb +3 -0
  38. data/lib/laser/cutter/geometry/rect.rb +25 -0
  39. data/lib/laser/cutter/geometry/shape.rb +19 -1
  40. data/lib/laser/cutter/geometry/transform.rb +50 -0
  41. data/lib/laser/cutter/geometry/tuple.rb +47 -11
  42. data/lib/laser/cutter/launcher.rb +14 -1
  43. data/lib/laser/cutter/missing_option.rb +2 -0
  44. data/lib/laser/cutter/notching/angled_edge.rb +67 -0
  45. data/lib/laser/cutter/notching/base.rb +8 -0
  46. data/lib/laser/cutter/notching/edge.rb +44 -4
  47. data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
  48. data/lib/laser/cutter/notching/path_generator.rb +40 -6
  49. data/lib/laser/cutter/notching/shift.rb +4 -0
  50. data/lib/laser/cutter/options.rb +25 -4
  51. data/lib/laser/cutter/page_manager.rb +12 -2
  52. data/lib/laser/cutter/renderer/base.rb +19 -3
  53. data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
  54. data/lib/laser/cutter/renderer/layout_renderer.rb +10 -0
  55. data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
  56. data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
  57. data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
  58. data/lib/laser/cutter/renderer/svg_renderer.rb +9 -1
  59. data/lib/laser/cutter/renderer.rb +2 -0
  60. data/lib/laser/cutter/sloped/box.rb +90 -0
  61. data/lib/laser/cutter/sloped/corners.rb +58 -0
  62. data/lib/laser/cutter/sloped/layout.rb +97 -0
  63. data/lib/laser/cutter/sloped/notcher.rb +53 -0
  64. data/lib/laser/cutter/sloped/panels.rb +121 -0
  65. data/lib/laser/cutter/sloped/placement.rb +36 -0
  66. data/lib/laser/cutter/sloped/profile.rb +176 -0
  67. data/lib/laser/cutter/sloped/request.rb +31 -0
  68. data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
  69. data/lib/laser/cutter/sloped/side_panel.rb +89 -0
  70. data/lib/laser/cutter/types.rb +3 -0
  71. data/lib/laser/cutter/units_converter.rb +5 -0
  72. data/lib/laser/cutter/version.rb +2 -1
  73. data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
  74. data/lib/laser/cutter.rb +2 -0
  75. data/spec/laser/cutter/angled_edge_spec.rb +103 -0
  76. data/spec/laser/cutter/box_factory_spec.rb +19 -0
  77. data/spec/laser/cutter/cli_spec.rb +87 -7
  78. data/spec/laser/cutter/configuration_spec.rb +37 -0
  79. data/spec/laser/cutter/loop_spec.rb +107 -0
  80. data/spec/laser/cutter/options_spec.rb +51 -9
  81. data/spec/laser/cutter/renderer/layout_renderer_spec.rb +18 -0
  82. data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
  83. data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
  84. data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
  85. data/spec/laser/cutter/sloped/box_spec.rb +151 -0
  86. data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
  87. data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
  88. data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
  89. data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
  90. data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
  91. data/spec/laser/cutter/sloped/request_spec.rb +48 -0
  92. data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
  93. data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
  94. data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
  95. data/spec/laser/cutter/transform_spec.rb +53 -0
  96. data/spec/spec_helper.rb +4 -0
  97. data/spec/support/loops.rb +106 -0
  98. data/spec/support/sloped.rb +26 -0
  99. data/spec/support/solid.rb +95 -0
  100. data/spec/wishes/wish-tilted-box.png +0 -0
  101. metadata +55 -9
  102. /data/.plans/{001.00-dry-cli-migration/plan.md → 001.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257dry-cli-migration/plan.md"} +0 -0
  103. /data/.plans/{002.00-lid-options/plan.md → 002.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257lid-options/plan.md"} +0 -0
  104. /data/.plans/{003.00-ruby-api/plan.md → 003.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257ruby-api/plan.md"} +0 -0
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # One of the rectangular panels that wrap around the sloped box between
7
+ # its two side walls: the bottom, the back, a strip or the tilted panel.
8
+ #
9
+ # In its own frame the panel is +width+ along x, from one side wall to
10
+ # the other, and +length+ along y, from its near edge to its far edge.
11
+ #
12
+ # far edge
13
+ # 3 +----------+ 2
14
+ # | |
15
+ # end | | end
16
+ # | |
17
+ # 0 +----------+ 1
18
+ # near edge
19
+ #
20
+ # - A long edge is notched into the panel beyond it, or plain.
21
+ # - An end is notched into the side wall from the near edge as far as
22
+ # +joint+, and from there on covers the edge of the side wall, one
23
+ # thickness out. A strip ends that way, past the corner of the wall,
24
+ # and so does a tilted panel that lifts off, along its whole length.
25
+ # - A corner next to two notched edges may carry the corner cube.
26
+ class RingPanel
27
+ # Which way is out of the panel across its left end, near edge, right
28
+ # end and far edge. Corner n lies between entry n and the one after.
29
+ OUTWARDS = [[-1, 0], [0, -1], [1, 0], [0, 1]].freeze
30
+
31
+ # @param notcher [Notcher]
32
+ # @param width [Numeric] between the side walls
33
+ # @param length [Numeric] from the near edge to the far edge
34
+ # @param near [Boolean, nil] the middle notch of the near edge: true for a tab, false for a hole, nil for a plain edge
35
+ # @param far [Boolean, nil] the same, for the far edge
36
+ # @param joint [Numeric] how far along each end, from the near edge, the notches into the side wall run
37
+ # @param corners [Array<Symbol>] the edges whose two corners this panel carries: :near, :far
38
+ def initialize(notcher:, width:, length:, joint:, near: nil, far: nil, corners: [])
39
+ @notcher = notcher
40
+ @width = width.to_f
41
+ @length = length.to_f
42
+ @joint = joint.to_f
43
+ @near = near
44
+ @far = far
45
+ @corners = corners
46
+ freeze
47
+ end
48
+
49
+ # @return [Geometry::Loop] the outline, counter-clockwise from the near left corner
50
+ def loop
51
+ Geometry::Loop.new(near_edge + corner(1) + right_end + corner(2) + far_edge + corner(3) + left_end + corner(0))
52
+ end
53
+
54
+ private
55
+
56
+ # @return [Float] the thickness of the material
57
+ def thickness = @notcher.thickness
58
+
59
+ # @param x [Float]
60
+ # @param y [Float]
61
+ # @return [Geometry::Point]
62
+ def point(x, y) = Geometry::Point[x, y]
63
+
64
+ # @return [Array<Geometry::Point>] the near edge, left to right; none when it is plain
65
+ def near_edge
66
+ @near.nil? ? [] : @notcher.path(point(0, 0), point(@width, 0), center_out: @near)
67
+ end
68
+
69
+ # @return [Array<Geometry::Point>] the far edge, right to left; none when it is plain
70
+ def far_edge
71
+ @far.nil? ? [] : @notcher.path(point(@width, @length), point(0, @length), center_out: @far)
72
+ end
73
+
74
+ # Up the right end: the notches, then the stretch that covers the wall.
75
+ #
76
+ # @return [Array<Geometry::Point>]
77
+ def right_end
78
+ notched(point(@width, 0), point(@width, @joint)) + covering(@width + thickness, @joint, @length)
79
+ end
80
+
81
+ # Down the left end, the same two stretches the other way round.
82
+ #
83
+ # @return [Array<Geometry::Point>]
84
+ def left_end
85
+ covering(-thickness, @length, @joint) + notched(point(0, @joint), point(0, 0))
86
+ end
87
+
88
+ # @param from [Geometry::Point]
89
+ # @param to [Geometry::Point]
90
+ # @return [Array<Geometry::Point>] the end notched into the side wall; none when the joint has no length
91
+ def notched(from, to)
92
+ @joint.positive? ? @notcher.path(from, to, center_out: true) : []
93
+ end
94
+
95
+ # @param x [Float] one thickness out from the end
96
+ # @param from [Float] where the stretch starts along y
97
+ # @param to [Float] where it ends
98
+ # @return [Array<Geometry::Point>] the stretch that covers the edge of the side wall; none when the joint runs the whole length
99
+ def covering(x, from, to)
100
+ @joint < @length ? [point(x, from), point(x, to)] : []
101
+ end
102
+
103
+ # The square on a corner this panel carries: out along the edge that
104
+ # arrives, round the outer corner, and back along the edge that leaves.
105
+ # The outline then passes the inner corner twice, and Loop drops the
106
+ # spikes that leaves wherever a tab runs into the square.
107
+ #
108
+ # @param index [Integer] 0 to 3, counter-clockwise from the near left corner
109
+ # @return [Array<Geometry::Point>] none when the panel does not carry that corner
110
+ def corner(index)
111
+ return [] unless @corners.include?(index < 2 ? :near : :far)
112
+
113
+ inner = point(index.between?(1, 2) ? @width : 0.0, index < 2 ? 0.0 : @length)
114
+ arriving, leaving = [OUTWARDS[index], OUTWARDS[(index + 1) % 4]].map { |x, y| Vector[x, y] * thickness }
115
+ [inner, inner.plus(arriving), inner.plus(arriving + leaving), inner.plus(leaving), inner]
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ module Sloped
6
+ # A side wall of the sloped box: the profile, notched into every panel
7
+ # that closes it.
8
+ #
9
+ # In its own frame x is the depth from the front and y the height, as
10
+ # in Profile. Its square edges are notched into the bottom, the back and
11
+ # the strips. Along the slope it is notched into the tilted panel, over
12
+ # the span that panel covers, or left plain under a tilted panel that
13
+ # lifts off.
14
+ #
15
+ # Where the tilted panel stops short under a strip, a wedge stays open
16
+ # between its square end and the strip. The wall closes the end of that
17
+ # wedge with a triangle, so nobody sees into the box from the side:
18
+ #
19
+ # strip
20
+ # ----+=========
21
+ # |\ wedge
22
+ # end | \
23
+ # of \ \ slope
24
+ # tilted \
25
+ class SidePanel
26
+ # @param profile [Profile]
27
+ # @param notcher [Notcher]
28
+ # @param lid [Symbol] :full for a tilted panel notched into the wall, :plain for one lying on it
29
+ def initialize(profile:, notcher:, lid:)
30
+ @profile = profile
31
+ @notcher = notcher
32
+ @lid = lid
33
+ freeze
34
+ end
35
+
36
+ # @return [Geometry::Loop] the outline, counter-clockwise from the front of the bottom
37
+ def loop
38
+ Geometry::Loop.new(bottom + back + top + slope + front)
39
+ end
40
+
41
+ private
42
+
43
+ # The corners of the inside outline, see Profile#vertices.
44
+ #
45
+ # @return [Array<Geometry::Point>]
46
+ def corners = @profile.vertices
47
+
48
+ # @param from [Geometry::Point]
49
+ # @param to [Geometry::Point]
50
+ # @return [Array<Geometry::Point>] the run, notched with a hole in the middle
51
+ def notched(from, to)
52
+ @notcher.path(from, to, center_out: false)
53
+ end
54
+
55
+ # @return [Array<Geometry::Point>] the bottom, front to back
56
+ def bottom = notched(corners[0], corners[1])
57
+
58
+ # @return [Array<Geometry::Point>] the back, bottom to top
59
+ def back = notched(corners[1], corners[2])
60
+
61
+ # Along the top strip, then on under its end to the top of the wedge.
62
+ #
63
+ # @return [Array<Geometry::Point>] none without a top strip
64
+ def top
65
+ return [] unless @profile.top_strip?
66
+
67
+ notched(corners[2], corners[3]) << @profile.tilted_outside.p2
68
+ end
69
+
70
+ # Down the slope, over the span the tilted panel covers.
71
+ #
72
+ # @return [Array<Geometry::Point>]
73
+ def slope
74
+ span = @profile.tilted_span
75
+ @lid == :plain ? [span.p2, span.p1] : notched(span.p2, span.p1)
76
+ end
77
+
78
+ # From the top of the wedge under the front strip, down that strip.
79
+ #
80
+ # @return [Array<Geometry::Point>] none without a front strip
81
+ def front
82
+ return [] unless @profile.front_strip?
83
+
84
+ [@profile.tilted_outside.p1] + notched(corners.last, corners.first)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -38,6 +38,9 @@ module Laser
38
38
  # A distance that may be zero, such as the kerf or a margin.
39
39
  Allowance = Coercible::Float.constrained(gteq: 0)
40
40
 
41
+ # The angle of a sloped front above horizontal, in degrees: flat and upright are both out.
42
+ Slope = Coercible::Float.constrained(gt: 0, lt: 90)
43
+
41
44
  # The units every dimension is in.
42
45
  Units = choice(%w[in mm], default: 'in')
43
46
 
@@ -2,11 +2,16 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Converts lengths between millimetres and inches, rounded to five digits.
5
6
  class UnitsConverter
7
+ # @param value [Numeric] a length in millimetres
8
+ # @return [Float] the same length in inches
6
9
  def self.mm2in(value)
7
10
  (0.039370079 * value).round(5)
8
11
  end
9
12
 
13
+ # @param value [Numeric] a length in inches
14
+ # @return [Float] the same length in millimetres
10
15
  def self.in2mm(value)
11
16
  (25.4 * value).round(5)
12
17
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
- VERSION = "2.0.1"
5
+ # The version of the gem.
6
+ VERSION = "3.0.0"
6
7
  end
7
8
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Raised when an option that must be above zero, such as a dimension or
6
+ # the thickness, is zero.
5
7
  class ZeroValueNotAllowed < MissingOption; end
6
8
  end
7
9
  end
data/lib/laser/cutter.rb CHANGED
@@ -18,10 +18,12 @@ require "dry/struct"
18
18
  require "tty-screen"
19
19
  require "zeitwerk"
20
20
 
21
+ # The namespace of the laser-cutter gem.
21
22
  module Laser
22
23
  # Draws the notched panels of a box. A Ruby program needs {Options},
23
24
  # {Cutter.render} and {Cutter.write}; the command line is one caller of many.
24
25
  module Cutter
26
+ # The base class of every error the gem raises.
25
27
  class Error < StandardError; end
26
28
 
27
29
  class << self
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ module Notching
8
+ RSpec.describe AngledEdge do
9
+ let(:thickness) { 0.25 }
10
+ let(:notch) { 0.75 }
11
+ let(:kerf) { 0.0 }
12
+ let(:center_out) { false }
13
+ let(:outward) { :right }
14
+ let(:from) { Geometry::Point[2, 1] }
15
+ # A 5-long run at atan(3/4), about 36.87 degrees.
16
+ let(:to) { Geometry::Point[6, 4] }
17
+ let(:options) { { notch_width: notch, thickness: thickness, kerf: kerf, center_out: center_out } }
18
+
19
+ subject(:edge) { described_class.new(from, to, outward: outward, **options) }
20
+
21
+ let(:vertices) { edge.lines.flat_map { |line| [line.p1, line.p2] } }
22
+ let(:direction) { [(to.x - from.x) / 5.0, (to.y - from.y) / 5.0] }
23
+ # Unit normal to the left of the run.
24
+ let(:left) { [-direction[1], direction[0]] }
25
+
26
+ def along(point) = ((point.x - from.x) * direction[0]) + ((point.y - from.y) * direction[1])
27
+ def across(point) = ((point.x - from.x) * left[0]) + ((point.y - from.y) * left[1])
28
+
29
+ it 'joins its lines end to end' do
30
+ expect(edge.lines.each_cons(2).all? { |a, b| a.p2.eql?(b.p1) }).to be(true)
31
+ end
32
+
33
+ it 'starts and ends on the inside line' do
34
+ expect([edge.lines.first.p1, edge.lines.last.p2]).to eql([from, to])
35
+ end
36
+
37
+ it 'stays between the ends of the run' do
38
+ expect(vertices.map { |v| along(v) }).to all(be_between(-1e-9, 5 + 1e-9))
39
+ end
40
+
41
+ context 'with the material to the right of the run' do
42
+ it 'puts every vertex on the inside line or one thickness to its right' do
43
+ expect(vertices.map { |v| across(v).round(9) }.uniq).to contain_exactly(0.0, -thickness)
44
+ end
45
+ end
46
+
47
+ context 'with the material to the left of the run' do
48
+ let(:outward) { :left }
49
+
50
+ it 'puts every vertex on the inside line or one thickness to its left' do
51
+ expect(vertices.map { |v| across(v).round(9) }.uniq).to contain_exactly(0.0, thickness)
52
+ end
53
+ end
54
+
55
+ it 'draws one notch per stretch along the run' do
56
+ runs = edge.lines.count { |line| across(line.p1).round(9) == across(line.p2).round(9) }
57
+ expect(runs).to eq(edge.notch_count)
58
+ end
59
+
60
+ it 'matches the turned copy of the same side laid flat' do
61
+ flat = described_class.new(Geometry::Point[0, 0], Geometry::Point[5, 0], outward: outward, **options)
62
+ turned = flat.lines.map { |line| edge.transform.apply(line) }
63
+ expect(turned.zip(edge.lines).all? { |a, b| a.p1.eql?(b.p1) && a.p2.eql?(b.p2) }).to be(true)
64
+ end
65
+
66
+ # The tilted panel is a plain rectangle; its end must mate with the
67
+ # sloped wall, so both must agree on the notches.
68
+ describe 'mating with a rectangular panel of the same length' do
69
+ let(:panel) do
70
+ inside = Geometry::Line.new(Geometry::Point[0, 0], Geometry::Point[5, 0])
71
+ outside = Geometry::Line.new(Geometry::Point[-thickness, -thickness], Geometry::Point[5 + thickness, -thickness])
72
+ Edge.new(outside, inside, options.merge(center_out: !center_out))
73
+ end
74
+
75
+ it 'has the same notch count and width' do
76
+ expect([edge.notch_count, edge.notch_width]).to eq([panel.notch_count, panel.notch_width])
77
+ end
78
+
79
+ it 'starts with a hole where the panel starts with a tab' do
80
+ expect(edge.first_notch_out?).to eq(!panel.first_notch_out?)
81
+ end
82
+ end
83
+
84
+ context 'with a kerf' do
85
+ let(:kerf) { 0.01 }
86
+
87
+ it 'moves each end out along the run by half the kerf' do
88
+ ends = [edge.lines.first.p1, edge.lines.last.p2].map { |p| along(p).round(9) }
89
+ expect(ends).to eq([-kerf / 2, 5 + (kerf / 2)])
90
+ end
91
+ end
92
+
93
+ it 'refuses a run with no length' do
94
+ expect { described_class.new(from, from, outward: outward, **options) }.to raise_error(ArgumentError, /needs a length/)
95
+ end
96
+
97
+ it 'refuses an unknown side' do
98
+ expect { described_class.new(from, to, outward: :up, **options) }.to raise_error(ArgumentError, /outward/)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module Laser
6
+ module Cutter
7
+ RSpec.describe BoxFactory do
8
+ let(:settings) { { width: 4, height: 3, depth: 5, thickness: 0.25, units: 'in', file: 'box.pdf' } }
9
+
10
+ it 'builds the box with six panels for settings without a slope' do
11
+ expect(described_class.for(Configuration.new(settings))).to be_a(Box)
12
+ end
13
+
14
+ it 'builds a sloped box for settings with a slope' do
15
+ expect(described_class.for(Configuration.new(settings.merge(slope: 45, front_height: 1)))).to be_a(Sloped::Box)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -10,13 +10,13 @@ RSpec.describe 'laser-cutter', type: :aruba do
10
10
  expect(last_command_started).to have_output(/generate.*page-sizes.*examples.*help/m)
11
11
  end
12
12
 
13
- it 'wraps help at 90 columns or fewer' do
14
- expect(last_command_started.output.lines.map { |line| line.chomp.length }.max).to be <= 90
13
+ it 'wraps help at 100 columns or fewer' do
14
+ expect(last_command_started.output.lines.map { |line| line.chomp.length }.max).to be <= 100
15
15
  end
16
16
  end
17
17
 
18
18
  describe 'the width of help' do
19
- { 200 => 90, 80 => 74, 0 => 74, nil => 74 }.each do |columns, width|
19
+ { 200 => 100, 80 => 74, 0 => 84, nil => 84 }.each do |columns, width|
20
20
  it "is #{width} on a terminal #{columns.inspect} columns wide" do
21
21
  expect(Laser::Cutter::CLI.help_width(columns)).to eq(width)
22
22
  end
@@ -50,7 +50,7 @@ RSpec.describe 'laser-cutter', type: :aruba do
50
50
  %w[version -V --version].each do |argument|
51
51
  it "answers to #{argument}" do
52
52
  run_command_and_stop("laser-cutter #{argument}")
53
- expect(last_command_started).to have_output(Laser::Cutter::VERSION)
53
+ expect(last_command_started.stdout).to eq("#{Laser::Cutter::VERSION}\n")
54
54
  end
55
55
  end
56
56
  end
@@ -74,11 +74,10 @@ RSpec.describe 'laser-cutter', type: :aruba do
74
74
  end
75
75
  end
76
76
 
77
- # dry-cli-autocomplete writes to $stdout rather than the stream Aruba hands
78
- # the command, so this reads the process's stdout instead.
79
77
  describe 'completion' do
80
78
  it 'prints a zsh completion script' do
81
- expect { run_command_and_stop('laser-cutter completion zsh') }.to output(/laser-cutter/).to_stdout
79
+ run_command_and_stop('laser-cutter completion zsh')
80
+ expect(last_command_started).to have_output(/laser-cutter/)
82
81
  end
83
82
  end
84
83
 
@@ -99,7 +98,16 @@ RSpec.describe 'laser-cutter', type: :aruba do
99
98
  expect(info.lines.map { |line| line.chomp.length }.max).to eq(Laser::Cutter::CLI.help_width)
100
99
  end
101
100
 
101
+ it 'draws the info box 100 wide in a terminal 200 columns wide' do
102
+ set_environment_variable('COLUMNS', '200')
103
+ run_command_and_stop("laser-cutter generate #{box} -o box.pdf")
104
+ info = last_command_started.stdout.split('┌─ Success').first
105
+ expect(info.lines.map { |line| line.chomp.length }.max).to eq(100)
106
+ end
107
+
108
+ # Wide enough for any working directory Aruba picks, which 80 columns are not.
102
109
  it 'keeps the path of the file on one line' do
110
+ set_environment_variable('COLUMNS', '200')
103
111
  run_command_and_stop("laser-cutter generate #{box} -o box.pdf")
104
112
  expect(last_command_started.stdout).to include(expand_path('box.pdf'))
105
113
  end
@@ -111,6 +119,7 @@ RSpec.describe 'laser-cutter', type: :aruba do
111
119
  end
112
120
 
113
121
  it 'closes with a success box that gives the path of the file' do
122
+ set_environment_variable('COLUMNS', '200')
114
123
  run_command_and_stop("laser-cutter generate #{box} -f svg -o box.svg")
115
124
  expect(last_command_started.stdout).to match(/─ Info ─.*─ Success ─.*Generated SVG file:.*box\.svg/m)
116
125
  end
@@ -120,6 +129,55 @@ RSpec.describe 'laser-cutter', type: :aruba do
120
129
  expect(read('box.svg').join).to include('<svg', '<line').and include('viewBox="0 0 9.2012 11.5512"')
121
130
  end
122
131
 
132
+ describe 'a sloped box' do
133
+ let(:sloped) { '-b 4x3x5/0.25/0.6 --slope 45 --front-height 1' }
134
+
135
+ it 'writes a PDF' do
136
+ run_command_and_stop("laser-cutter generate #{sloped} -o box.pdf")
137
+ expect(read('box.pdf').first).to start_with('%PDF')
138
+ end
139
+
140
+ it 'writes an SVG with lines along the slope' do
141
+ run_command_and_stop("laser-cutter generate #{sloped} -f svg -o box.svg")
142
+ expect(read('box.svg').join).to match(/<line x1="(?<x1>[\d.]+)" y1="(?<y1>[\d.]+)" x2="(?!\k<x1>")[\d.]+" y2="(?!\k<y1>")[\d.]+"/)
143
+ end
144
+
145
+ it 'names the front height and the slope among the dimensions' do
146
+ run_command_and_stop("laser-cutter generate #{sloped} -o box.pdf")
147
+ expect(last_command_started.stdout).to match(/Depth:\s+5\.0 in.*Front:\s+1\.0 in.*Slope:\s+45\.0°.*Lid:\s+full/m)
148
+ end
149
+
150
+ it 'lays a plain lid on the slope' do
151
+ run_command_and_stop("laser-cutter generate #{sloped} --lid plain -o box.pdf")
152
+ expect(last_command_started.stdout).to match(/Slope:\s+45\.0°.*Lid:\s+plain.*─ Success ─/m)
153
+ end
154
+
155
+ {
156
+ '--slope 45' => /front_height is required with slope, but missing/,
157
+ '--front-height 1' => /front_height is the front of a sloped box, so it needs a slope/,
158
+ '--slope 45 --front-height 1 --lid back' => /lid cannot be "back" with a slope, but must be one of: full, plain/,
159
+ '--slope 10 --front-height 1' => /10\.0° slope from 1\.0 up to 3\.0 needs a depth of 11\.343/,
160
+ '--slope 95 --front-height 1' => /slope must be between 0 and 90 degrees, got 95\.0/
161
+ }.each do |arguments, message|
162
+ it "fails on #{arguments}" do
163
+ set_environment_variable('COLUMNS', '200')
164
+ run_command_and_stop("laser-cutter generate -b 4x3x5/0.25/0.6 #{arguments} -o box.pdf", fail_on_error: false)
165
+ expect(last_command_started).to have_exit_status(1)
166
+ expect(last_command_started.stderr).to match(/─ Error ─/).and match(message)
167
+ end
168
+ end
169
+
170
+ it 'says in the help which lid a slope cannot take' do
171
+ run_command_and_stop('laser-cutter generate --help')
172
+ expect(last_command_started.stdout.gsub(/\s+/, ' ')).to include('into the back wall only (not with --slope)')
173
+ end
174
+
175
+ it 'explains --slope and --front-height in the help' do
176
+ run_command_and_stop('laser-cutter generate --help')
177
+ expect(last_command_started).to have_output(/--slope.*--front-height/m)
178
+ end
179
+ end
180
+
123
181
  it 'draws the box without kerf as well with --inside-box' do
124
182
  run_command_and_stop("laser-cutter generate #{box} --inside-box -o box.pdf")
125
183
  expect(last_command_started).to have_output(%r{Drawing box\.pdf 656/656})
@@ -155,6 +213,19 @@ RSpec.describe 'laser-cutter', type: :aruba do
155
213
  end
156
214
  end
157
215
 
216
+ { '3x3x3' => [3.0, 3.0, 3.0], '9x4x9' => [9.0, 4.0, 9.0] }.each do |size, dimensions|
217
+ it "generates a #{size} inch storage box with a removable lid" do
218
+ run_command_and_stop("laser-cutter generate -u in -b #{size}/0.125 --lid plain -o storage.pdf -W settings.json")
219
+
220
+ expect(last_command_started).to have_exit_status(0)
221
+ expect(read('storage.pdf').first).to start_with('%PDF')
222
+ expect(JSON.parse(read('settings.json').join)).to include(
223
+ 'width' => dimensions[0], 'height' => dimensions[1], 'depth' => dimensions[2],
224
+ 'thickness' => 0.125, 'units' => 'in', 'lid' => 'plain'
225
+ )
226
+ end
227
+ end
228
+
158
229
  it 'keeps the lid a saved configuration asks for' do
159
230
  run_command_and_stop("laser-cutter generate #{box} -L plain -o box.pdf -W settings.json")
160
231
  expect(JSON.parse(read('settings.json').join)).to include('lid' => 'plain')
@@ -182,6 +253,15 @@ RSpec.describe 'laser-cutter', type: :aruba do
182
253
  expect(last_command_started.stderr.lines.map { |line| line.chomp.length }.max).to eq(Laser::Cutter::CLI.help_width)
183
254
  end
184
255
 
256
+ # Aruba's streams are not a tty, like a pipe: boxes must still follow the terminal.
257
+ { '200' => 100, '60' => 54 }.each do |columns, width|
258
+ it "draws the error box #{width} wide in a terminal #{columns} columns wide" do
259
+ set_environment_variable('COLUMNS', columns)
260
+ run_command_and_stop("laser-cutter generate #{box}", fail_on_error: false)
261
+ expect(last_command_started.stderr.lines.map { |line| line.chomp.length }.max).to eq(width)
262
+ end
263
+ end
264
+
185
265
  it 'fails on an unknown format' do
186
266
  run_command_and_stop("laser-cutter generate #{box} -f dxf -o box.dxf", fail_on_error: false)
187
267
  expect(last_command_started).to have_output(/unknown format "dxf", expected one of: pdf, svg/)
@@ -57,6 +57,43 @@ module Laser
57
57
  end
58
58
  end
59
59
 
60
+ describe 'slope' do
61
+ let(:opts) { { 'box' => '4x3x5/0.125', 'file' => '/tmp/a', 'slope' => '45', 'front_height' => '1' } }
62
+
63
+ its(:slope) { is_expected.to eql(45.0) }
64
+ its(:front_height) { is_expected.to eql(1.0) }
65
+
66
+ it 'passes with a front height' do
67
+ expect { config.validate! }.not_to raise_error
68
+ end
69
+
70
+ it 'needs a front height' do
71
+ expect { described_class.new(opts.except('front_height')).validate! }
72
+ .to raise_error(Laser::Cutter::MissingOption, /front_height is required with slope/)
73
+ end
74
+
75
+ it 'rejects a front height without a slope' do
76
+ expect { described_class.new(opts.except('slope')).validate! }.to raise_error(Laser::Cutter::InvalidOption, /needs a slope/)
77
+ end
78
+
79
+ it 'rejects a lid notched into the back wall' do
80
+ expect { described_class.new(opts.merge('lid' => 'back')).validate! }
81
+ .to raise_error(Laser::Cutter::InvalidOption, /lid cannot be "back" with a slope/)
82
+ end
83
+
84
+ it 'rejects numbers that do not make the shape' do
85
+ expect { described_class.new(opts.merge('slope' => '10')).validate! }
86
+ .to raise_error(Laser::Cutter::InvalidOption, /needs a depth/)
87
+ end
88
+
89
+ context 'after change_units(:mm)' do
90
+ subject(:config) { described_class.new(opts).tap { |c| c.change_units(:mm) } }
91
+
92
+ its(:front_height) { is_expected.to eql(25.4) }
93
+ its(:slope) { is_expected.to eql(45.0) }
94
+ end
95
+ end
96
+
60
97
  context 'when notch is omitted' do
61
98
  let(:opts) { { 'box' => '2.0x1.0x2/0.125', 'file' => '/tmp/a' } }
62
99