whirled_peas 0.5.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +31 -0
- data/README.md +748 -137
- data/Rakefile +4 -32
- data/bin/easing +33 -0
- data/bin/reset_cursor +11 -0
- data/bin/screen_test +68 -0
- data/examples/graph.rb +54 -0
- data/examples/intro.rb +52 -0
- data/examples/scrolling.rb +54 -0
- data/lib/whirled_peas.rb +4 -12
- data/lib/whirled_peas/animator.rb +5 -0
- data/lib/whirled_peas/animator/debug_consumer.rb +17 -0
- data/lib/whirled_peas/animator/easing.rb +72 -0
- data/lib/whirled_peas/animator/frame.rb +5 -0
- data/lib/whirled_peas/animator/frameset.rb +33 -0
- data/lib/whirled_peas/animator/producer.rb +35 -0
- data/lib/whirled_peas/animator/renderer_consumer.rb +31 -0
- data/lib/whirled_peas/command.rb +5 -0
- data/lib/whirled_peas/command/base.rb +86 -0
- data/lib/whirled_peas/command/config_command.rb +44 -0
- data/lib/whirled_peas/command/debug.rb +21 -0
- data/lib/whirled_peas/command/fonts.rb +22 -0
- data/lib/whirled_peas/command/frame_command.rb +34 -0
- data/lib/whirled_peas/command/frames.rb +24 -0
- data/lib/whirled_peas/command/help.rb +38 -0
- data/lib/whirled_peas/command/play.rb +108 -0
- data/lib/whirled_peas/command/record.rb +57 -0
- data/lib/whirled_peas/command/still.rb +29 -0
- data/lib/whirled_peas/command_line.rb +22 -220
- data/lib/whirled_peas/config.rb +56 -6
- data/lib/whirled_peas/device.rb +5 -0
- data/lib/whirled_peas/device/null_device.rb +8 -0
- data/lib/whirled_peas/device/output_file.rb +19 -0
- data/lib/whirled_peas/device/screen.rb +26 -0
- data/lib/whirled_peas/graphics.rb +14 -0
- data/lib/whirled_peas/graphics/box_painter.rb +48 -47
- data/lib/whirled_peas/graphics/canvas.rb +22 -11
- data/lib/whirled_peas/graphics/composer.rb +98 -0
- data/lib/whirled_peas/graphics/container_coords.rb +24 -13
- data/lib/whirled_peas/graphics/container_dimensions.rb +37 -9
- data/lib/whirled_peas/graphics/container_painter.rb +283 -36
- data/lib/whirled_peas/graphics/content_dimensions.rb +19 -0
- data/lib/whirled_peas/graphics/content_painter.rb +20 -0
- data/lib/whirled_peas/graphics/debugger.rb +10 -1
- data/lib/whirled_peas/graphics/graph_dimensions.rb +12 -0
- data/lib/whirled_peas/graphics/graph_painter.rb +95 -0
- data/lib/whirled_peas/graphics/grid_painter.rb +25 -15
- data/lib/whirled_peas/graphics/mock_screen.rb +1 -1
- data/lib/whirled_peas/graphics/painter.rb +15 -6
- data/lib/whirled_peas/graphics/renderer.rb +13 -30
- data/lib/whirled_peas/graphics/text_painter.rb +11 -13
- data/lib/whirled_peas/settings/alignment.rb +24 -0
- data/lib/whirled_peas/settings/bg_color.rb +2 -0
- data/lib/whirled_peas/settings/border.rb +16 -16
- data/lib/whirled_peas/settings/color.rb +13 -14
- data/lib/whirled_peas/settings/container_settings.rb +133 -40
- data/lib/whirled_peas/settings/debugger.rb +9 -0
- data/lib/whirled_peas/settings/display_flow.rb +13 -11
- data/lib/whirled_peas/settings/element_settings.rb +8 -3
- data/lib/whirled_peas/settings/graph_settings.rb +21 -0
- data/lib/whirled_peas/settings/grid_settings.rb +15 -0
- data/lib/whirled_peas/settings/scrollbar.rb +15 -0
- data/lib/whirled_peas/settings/sizing.rb +19 -0
- data/lib/whirled_peas/settings/spacing.rb +34 -0
- data/lib/whirled_peas/settings/text_color.rb +2 -0
- data/lib/whirled_peas/settings/vert_alignment.rb +24 -0
- data/lib/whirled_peas/utils/ansi.rb +13 -0
- data/lib/whirled_peas/utils/file_handler.rb +57 -0
- data/lib/whirled_peas/utils/title_font.rb +1 -1
- data/lib/whirled_peas/version.rb +1 -1
- data/screen_test/{rendered/elements → elements}/box.frame +1 -1
- data/screen_test/{rendered/elements → elements}/box.rb +0 -0
- data/screen_test/elements/graph.frame +1 -0
- data/screen_test/elements/graph.rb +12 -0
- data/screen_test/elements/grid.frame +1 -0
- data/screen_test/{rendered/elements → elements}/grid.rb +0 -0
- data/screen_test/elements/screen_overflow_x.frame +1 -0
- data/screen_test/{rendered/elements/screen_overflow.rb → elements/screen_overflow_x.rb} +0 -0
- data/screen_test/elements/screen_overflow_y.frame +1 -0
- data/screen_test/elements/screen_overflow_y.rb +9 -0
- data/screen_test/elements/text.frame +1 -0
- data/screen_test/{rendered/elements → elements}/text.rb +0 -0
- data/screen_test/elements/text_multiline.frame +1 -0
- data/screen_test/{rendered/elements → elements}/text_multiline.rb +0 -0
- data/screen_test/settings/align/box_around.frame +1 -0
- data/screen_test/settings/align/box_around.rb +16 -0
- data/screen_test/settings/align/box_between.frame +1 -0
- data/screen_test/settings/align/box_between.rb +16 -0
- data/screen_test/settings/align/box_center.frame +1 -0
- data/screen_test/{rendered/settings/align/box.rb → settings/align/box_center.rb} +5 -4
- data/screen_test/settings/align/box_default.frame +1 -0
- data/screen_test/{rendered/settings/align/grid.rb → settings/align/box_default.rb} +6 -6
- data/screen_test/settings/align/box_evenly.frame +1 -0
- data/screen_test/settings/align/box_evenly.rb +16 -0
- data/screen_test/settings/align/box_left.frame +1 -0
- data/screen_test/settings/align/box_left.rb +21 -0
- data/screen_test/settings/align/box_right.frame +1 -0
- data/screen_test/settings/align/box_right.rb +21 -0
- data/screen_test/settings/align/children_center.frame +1 -0
- data/screen_test/settings/align/children_center.rb +15 -0
- data/screen_test/settings/align/children_left.frame +1 -0
- data/screen_test/settings/align/children_left.rb +15 -0
- data/screen_test/settings/align/children_right.frame +1 -0
- data/screen_test/settings/align/children_right.rb +15 -0
- data/screen_test/settings/align/grid_center.frame +1 -0
- data/screen_test/settings/align/grid_center.rb +18 -0
- data/screen_test/settings/align/grid_default.frame +1 -0
- data/screen_test/settings/align/grid_default.rb +17 -0
- data/screen_test/settings/align/grid_left.frame +1 -0
- data/screen_test/settings/align/grid_left.rb +18 -0
- data/screen_test/settings/align/grid_right.frame +1 -0
- data/screen_test/settings/align/grid_right.rb +18 -0
- data/screen_test/settings/ansi/bold.frame +1 -0
- data/screen_test/{rendered/settings/width/overflow_box_t2b.rb → settings/ansi/bold.rb} +4 -5
- data/screen_test/{rendered/settings → settings}/ansi/color.frame +1 -1
- data/screen_test/{rendered/settings → settings}/ansi/color.rb +0 -0
- data/screen_test/settings/ansi/underline.frame +1 -0
- data/screen_test/{rendered/settings → settings}/ansi/underline.rb +4 -5
- data/screen_test/{rendered/settings → settings}/border.frame +1 -1
- data/screen_test/{rendered/settings → settings}/border.rb +1 -1
- data/screen_test/settings/flow/box_b2t.frame +1 -0
- data/screen_test/settings/flow/box_b2t.rb +26 -0
- data/screen_test/settings/flow/box_l2r.frame +1 -0
- data/screen_test/settings/flow/box_l2r.rb +26 -0
- data/screen_test/settings/flow/box_r2l.frame +1 -0
- data/screen_test/settings/flow/box_r2l.rb +26 -0
- data/screen_test/settings/flow/box_t2b.frame +1 -0
- data/screen_test/settings/flow/box_t2b.rb +26 -0
- data/screen_test/settings/flow/grid_b2t.frame +1 -0
- data/screen_test/settings/flow/grid_b2t.rb +14 -0
- data/screen_test/settings/flow/grid_l2r.frame +1 -0
- data/screen_test/{rendered/settings/margin.rb → settings/flow/grid_l2r.rb} +4 -4
- data/screen_test/settings/flow/grid_r2l.frame +1 -0
- data/screen_test/settings/flow/grid_r2l.rb +14 -0
- data/screen_test/settings/flow/grid_t2b.frame +1 -0
- data/screen_test/settings/flow/grid_t2b.rb +14 -0
- data/screen_test/settings/height/box.frame +1 -0
- data/screen_test/{rendered/settings → settings}/height/box.rb +0 -0
- data/screen_test/settings/height/box_border_sizing.frame +1 -0
- data/screen_test/settings/height/box_border_sizing.rb +15 -0
- data/screen_test/settings/height/grid.frame +1 -0
- data/screen_test/{rendered/settings → settings}/height/grid.rb +0 -0
- data/screen_test/settings/height/overflow_box.frame +1 -0
- data/screen_test/settings/height/overflow_box.rb +13 -0
- data/screen_test/settings/height/overflow_box_l2r.frame +1 -0
- data/screen_test/settings/height/overflow_box_l2r.rb +17 -0
- data/screen_test/settings/height/overflow_box_t2b.frame +1 -0
- data/screen_test/settings/height/overflow_box_t2b.rb +16 -0
- data/screen_test/settings/height/overflow_grid.frame +1 -0
- data/screen_test/{rendered/settings → settings}/height/overflow_grid.rb +0 -0
- data/screen_test/settings/margin.frame +1 -0
- data/screen_test/settings/margin.rb +16 -0
- data/screen_test/settings/padding.frame +1 -0
- data/screen_test/settings/padding.rb +13 -0
- data/screen_test/settings/position/box_left.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/box_left.rb +2 -2
- data/screen_test/settings/position/box_left_negative.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/box_left_negative.rb +2 -2
- data/screen_test/settings/position/box_top.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/box_top.rb +1 -1
- data/screen_test/settings/position/box_top_negative.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/box_top_negative.rb +2 -2
- data/screen_test/settings/position/grid_left.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/grid_left.rb +1 -1
- data/screen_test/settings/position/grid_left_negative.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/grid_left_negative.rb +1 -1
- data/screen_test/settings/position/grid_top.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/grid_top.rb +1 -1
- data/screen_test/settings/position/grid_top_negative.frame +1 -0
- data/screen_test/{rendered/settings → settings}/position/grid_top_negative.rb +1 -1
- data/screen_test/settings/scroll/horiz_box.frame +1 -0
- data/screen_test/settings/scroll/horiz_box.rb +17 -0
- data/screen_test/settings/scroll/horiz_box_align_center.rb +18 -0
- data/screen_test/settings/scroll/horiz_box_align_right.rb +18 -0
- data/screen_test/settings/scroll/vert_box.frame +1 -0
- data/screen_test/settings/scroll/vert_box.rb +20 -0
- data/screen_test/settings/title_font.frame +1 -0
- data/screen_test/{rendered/settings → settings}/title_font.rb +1 -1
- data/screen_test/settings/valign/box_around.frame +1 -0
- data/screen_test/settings/valign/box_around.rb +17 -0
- data/screen_test/settings/valign/box_between.frame +1 -0
- data/screen_test/settings/valign/box_between.rb +17 -0
- data/screen_test/settings/valign/box_bottom.frame +1 -0
- data/screen_test/settings/valign/box_bottom.rb +17 -0
- data/screen_test/settings/valign/box_default.frame +1 -0
- data/screen_test/settings/valign/box_default.rb +16 -0
- data/screen_test/settings/valign/box_evenly.frame +1 -0
- data/screen_test/settings/valign/box_evenly.rb +17 -0
- data/screen_test/settings/valign/box_middle.frame +1 -0
- data/screen_test/settings/valign/box_middle.rb +17 -0
- data/screen_test/settings/valign/box_top.frame +1 -0
- data/screen_test/settings/valign/box_top.rb +17 -0
- data/screen_test/settings/valign/grid_bottom.frame +1 -0
- data/screen_test/settings/valign/grid_bottom.rb +15 -0
- data/screen_test/settings/valign/grid_default.frame +1 -0
- data/screen_test/settings/valign/grid_default.rb +14 -0
- data/screen_test/settings/valign/grid_middle.frame +1 -0
- data/screen_test/settings/valign/grid_middle.rb +15 -0
- data/screen_test/settings/valign/grid_top.frame +1 -0
- data/screen_test/settings/valign/grid_top.rb +15 -0
- data/screen_test/settings/width/box_border_sizing.frame +1 -0
- data/screen_test/settings/width/box_border_sizing.rb +15 -0
- data/screen_test/settings/width/box_content.frame +1 -0
- data/screen_test/settings/width/box_content.rb +15 -0
- data/screen_test/settings/width/box_default.frame +1 -0
- data/screen_test/{rendered/settings/width/box.rb → settings/width/box_default.rb} +3 -2
- data/screen_test/settings/width/grid.frame +1 -0
- data/screen_test/{rendered/settings → settings}/width/grid.rb +2 -2
- data/screen_test/settings/width/overflow_align_center.frame +1 -0
- data/screen_test/settings/width/overflow_align_center.rb +14 -0
- data/screen_test/settings/width/overflow_align_right.frame +1 -0
- data/screen_test/settings/width/overflow_align_right.rb +14 -0
- data/screen_test/settings/width/overflow_box.frame +1 -0
- data/screen_test/settings/width/overflow_box.rb +13 -0
- data/screen_test/settings/width/overflow_box_l2r.frame +1 -0
- data/screen_test/settings/width/overflow_box_l2r.rb +16 -0
- data/screen_test/settings/width/overflow_box_t2b.frame +1 -0
- data/screen_test/settings/width/overflow_box_t2b.rb +17 -0
- data/screen_test/settings/width/overflow_grid.frame +1 -0
- data/screen_test/{rendered/settings → settings}/width/overflow_grid.rb +0 -0
- data/tools/whirled_peas/tools/screen_tester.rb +285 -0
- metadata +192 -98
- data/bin/debug +0 -35
- data/lib/whirled_peas/debugger.rb +0 -80
- data/lib/whirled_peas/frame.rb +0 -6
- data/lib/whirled_peas/frame/consumer.rb +0 -30
- data/lib/whirled_peas/frame/debug_consumer.rb +0 -30
- data/lib/whirled_peas/frame/event_loop.rb +0 -90
- data/lib/whirled_peas/frame/producer.rb +0 -67
- data/lib/whirled_peas/graphics/screen.rb +0 -65
- data/lib/whirled_peas/graphics/text_dimensions.rb +0 -15
- data/lib/whirled_peas/settings/text_align.rb +0 -19
- data/lib/whirled_peas/template.rb +0 -5
- data/lib/whirled_peas/template/box_element.rb +0 -8
- data/lib/whirled_peas/template/composer.rb +0 -68
- data/lib/whirled_peas/template/container.rb +0 -28
- data/lib/whirled_peas/template/debugger.rb +0 -34
- data/lib/whirled_peas/template/element.rb +0 -13
- data/lib/whirled_peas/template/grid_element.rb +0 -8
- data/lib/whirled_peas/template/text_element.rb +0 -24
- data/screen_test/rendered/elements/grid.frame +0 -1
- data/screen_test/rendered/elements/text.frame +0 -1
- data/screen_test/rendered/elements/text_multiline.frame +0 -1
- data/screen_test/rendered/settings/align/box.frame +0 -1
- data/screen_test/rendered/settings/align/children_center.frame +0 -1
- data/screen_test/rendered/settings/align/children_center.rb +0 -13
- data/screen_test/rendered/settings/align/children_left.frame +0 -1
- data/screen_test/rendered/settings/align/children_left.rb +0 -13
- data/screen_test/rendered/settings/align/children_right.frame +0 -1
- data/screen_test/rendered/settings/align/children_right.rb +0 -13
- data/screen_test/rendered/settings/align/grid.frame +0 -1
- data/screen_test/rendered/settings/ansi/bold.frame +0 -1
- data/screen_test/rendered/settings/ansi/bold.rb +0 -15
- data/screen_test/rendered/settings/ansi/underline.frame +0 -1
- data/screen_test/rendered/settings/flow/l2r.frame +0 -1
- data/screen_test/rendered/settings/flow/l2r.rb +0 -24
- data/screen_test/rendered/settings/flow/t2b.frame +0 -1
- data/screen_test/rendered/settings/flow/t2b.rb +0 -24
- data/screen_test/rendered/settings/height/box.frame +0 -1
- data/screen_test/rendered/settings/height/grid.frame +0 -1
- data/screen_test/rendered/settings/height/overflow_box.frame +0 -1
- data/screen_test/rendered/settings/height/overflow_box.rb +0 -13
- data/screen_test/rendered/settings/height/overflow_box_l2r.frame +0 -1
- data/screen_test/rendered/settings/height/overflow_box_l2r.rb +0 -15
- data/screen_test/rendered/settings/height/overflow_box_t2b.frame +0 -1
- data/screen_test/rendered/settings/height/overflow_box_t2b.rb +0 -14
- data/screen_test/rendered/settings/height/overflow_grid.frame +0 -1
- data/screen_test/rendered/settings/margin.frame +0 -1
- data/screen_test/rendered/settings/padding.frame +0 -1
- data/screen_test/rendered/settings/padding.rb +0 -11
- data/screen_test/rendered/settings/position/box_left.frame +0 -1
- data/screen_test/rendered/settings/position/box_left_negative.frame +0 -1
- data/screen_test/rendered/settings/position/box_top.frame +0 -1
- data/screen_test/rendered/settings/position/box_top_negative.frame +0 -1
- data/screen_test/rendered/settings/position/grid_left.frame +0 -1
- data/screen_test/rendered/settings/position/grid_left_negative.frame +0 -1
- data/screen_test/rendered/settings/position/grid_top.frame +0 -1
- data/screen_test/rendered/settings/position/grid_top_negative.frame +0 -1
- data/screen_test/rendered/settings/title_font.frame +0 -1
- data/screen_test/rendered/settings/width/box.frame +0 -1
- data/screen_test/rendered/settings/width/grid.frame +0 -1
- data/screen_test/rendered/settings/width/overflow_box.frame +0 -1
- data/screen_test/rendered/settings/width/overflow_box.rb +0 -11
- data/screen_test/rendered/settings/width/overflow_box_l2r.frame +0 -1
- data/screen_test/rendered/settings/width/overflow_box_l2r.rb +0 -14
- data/screen_test/rendered/settings/width/overflow_box_t2b.frame +0 -1
- data/screen_test/rendered/settings/width/overflow_grid.frame +0 -1
- data/screen_test/screen_tester.rb +0 -191
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'whirled_peas/utils/ansi'
|
2
|
+
|
3
|
+
require_relative 'content_painter'
|
4
|
+
|
5
|
+
module WhirledPeas
|
6
|
+
module Graphics
|
7
|
+
class GraphPainter < ContentPainter
|
8
|
+
def paint(canvas, left, top, &block)
|
9
|
+
axis_formatting = [*settings.axis_color, *settings.bg_color]
|
10
|
+
plot_formatting = [*settings.color, *settings.bg_color]
|
11
|
+
axes_lines.each.with_index do |axis_line, row_index|
|
12
|
+
canvas.stroke(left, top + row_index, axis_line, axis_formatting, &block)
|
13
|
+
next if row_index >= plot_lines.length
|
14
|
+
canvas.stroke(left + 1, top + row_index, plot_lines[row_index], plot_formatting, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def content_lines
|
21
|
+
axes_lines
|
22
|
+
end
|
23
|
+
|
24
|
+
def plot_lines
|
25
|
+
return @plot_lines if @plot_lines
|
26
|
+
min_y = 1.0 / 0
|
27
|
+
max_y = -1.0 / 0
|
28
|
+
if settings.width
|
29
|
+
interpolated = inner_width.times.map do |i|
|
30
|
+
x = (i * (content.length - 1).to_f / inner_width).floor
|
31
|
+
max_y = content[x] if content[x] > max_y
|
32
|
+
min_y = content[x] if content[x] < min_y
|
33
|
+
content[x]
|
34
|
+
end
|
35
|
+
else
|
36
|
+
interpolated = content
|
37
|
+
content.each do |y|
|
38
|
+
max_y = y if y > max_y
|
39
|
+
min_y = y if y < min_y
|
40
|
+
end
|
41
|
+
end
|
42
|
+
scaled = interpolated.map do |y|
|
43
|
+
(2 * inner_height * (y - min_y).to_f / (max_y - min_y)).floor
|
44
|
+
end
|
45
|
+
@plot_lines = Array.new(inner_height) { '' }
|
46
|
+
scaled.each.with_index do |y, x_index|
|
47
|
+
@plot_lines.each.with_index do |row, row_index|
|
48
|
+
y_index = inner_height - row_index - 1
|
49
|
+
asc, next_y = if scaled.length == 1
|
50
|
+
[true, y]
|
51
|
+
elsif x_index == scaled.length - 1
|
52
|
+
y >= scaled[x_index - 1]
|
53
|
+
[true, y]
|
54
|
+
else
|
55
|
+
scaled[x_index + 1] >= y
|
56
|
+
[true, scaled[x_index + 1]]
|
57
|
+
end
|
58
|
+
if asc
|
59
|
+
top_half = (y...next_y).include?(2 * y_index + 1)
|
60
|
+
bottom_half = (y...next_y).include?(2 * y_index)
|
61
|
+
else
|
62
|
+
top_half = (next_y...y).include?(2 * y_index + 1)
|
63
|
+
bottom_half = (next_y...y).include?(2 * y_index)
|
64
|
+
end
|
65
|
+
row << if top_half && bottom_half
|
66
|
+
'█'
|
67
|
+
elsif top_half
|
68
|
+
'▀'
|
69
|
+
elsif bottom_half
|
70
|
+
'▄'
|
71
|
+
else
|
72
|
+
' '
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
@plot_lines
|
77
|
+
end
|
78
|
+
|
79
|
+
def inner_height
|
80
|
+
settings.height - 1
|
81
|
+
end
|
82
|
+
|
83
|
+
def inner_width
|
84
|
+
settings.width.nil? ? content.length : settings.width - 1
|
85
|
+
end
|
86
|
+
|
87
|
+
def axes_lines
|
88
|
+
return @axes_lines if @axes_lines
|
89
|
+
@axes_lines = inner_height.times.map { '┃' }
|
90
|
+
@axes_lines << '┗' + '━' * inner_width
|
91
|
+
@axes_lines
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -4,33 +4,30 @@ require_relative 'container_painter'
|
|
4
4
|
module WhirledPeas
|
5
5
|
module Graphics
|
6
6
|
class GridPainter < ContainerPainter
|
7
|
-
def paint(canvas, &block)
|
7
|
+
def paint(canvas, left, top, &block)
|
8
8
|
super
|
9
9
|
return unless canvas.writable?
|
10
10
|
each_child.with_index do |child, index|
|
11
|
-
col_index = index
|
12
|
-
|
13
|
-
|
14
|
-
if settings.align_center?
|
15
|
-
content_left += (dimensions.content_width - child.dimensions.outer_width) / 2
|
16
|
-
elsif settings.align_right?
|
17
|
-
content_left += dimensions.content_width - child.dimensions.outer_width
|
18
|
-
end
|
11
|
+
col_index, row_index = grid_cell(index)
|
12
|
+
left_offset, _ = horiz_justify_offset(child.dimensions.outer_width)
|
13
|
+
top_offset, _ = vert_justify_offset(child.dimensions.outer_height)
|
19
14
|
child_canvas = canvas.child(
|
20
|
-
content_left,
|
21
|
-
coords(
|
15
|
+
coords(left, top).content_left(col_index) + left_offset,
|
16
|
+
coords(left, top).content_top(row_index) + top_offset,
|
22
17
|
[dimensions.content_width, child.dimensions.outer_width].min,
|
23
18
|
[dimensions.content_height, child.dimensions.outer_height].min
|
24
19
|
)
|
25
|
-
child.paint(
|
20
|
+
child.paint(
|
21
|
+
child_canvas,
|
22
|
+
coords(left, top).content_left(col_index) + left_offset,
|
23
|
+
coords(left, top).content_top(row_index) + top_offset,
|
24
|
+
&block
|
25
|
+
)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
def dimensions
|
30
30
|
@dimensions ||= begin
|
31
|
-
if settings.num_cols.nil? || settings.num_cols == 0
|
32
|
-
raise SettingsError, "`num_cols` must be set for GridSettings(#{name})"
|
33
|
-
end
|
34
31
|
num_cols = settings.num_cols
|
35
32
|
num_rows = (num_children.to_f / num_cols).ceil
|
36
33
|
content_width = 0
|
@@ -50,6 +47,19 @@ module WhirledPeas
|
|
50
47
|
)
|
51
48
|
end
|
52
49
|
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def grid_cell(index)
|
54
|
+
if settings.horizontal_flow?
|
55
|
+
col_index, row_index = [index % dimensions.num_cols, index / dimensions.num_cols]
|
56
|
+
col_index = dimensions.num_cols - col_index - 1 if settings.reverse_flow?
|
57
|
+
else
|
58
|
+
col_index, row_index = [index / dimensions.num_rows, index % dimensions.num_rows]
|
59
|
+
row_index = dimensions.num_rows - row_index - 1 if settings.reverse_flow?
|
60
|
+
end
|
61
|
+
[col_index, row_index]
|
62
|
+
end
|
53
63
|
end
|
54
64
|
private_constant :BoxPainter
|
55
65
|
end
|
@@ -1,23 +1,32 @@
|
|
1
1
|
module WhirledPeas
|
2
2
|
module Graphics
|
3
|
+
# Abstract base Painter class. Given a canvas and start coordinates (left, top), a painter
|
4
|
+
# is responsible for generating the "strokes" that display the element.
|
3
5
|
class Painter
|
4
6
|
attr_reader :name, :settings
|
5
7
|
|
6
|
-
def initialize(
|
7
|
-
@element = element
|
8
|
+
def initialize(name, settings)
|
8
9
|
@settings = settings
|
9
10
|
@name = name
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
+
# Paint the element onto the canvas by yielding strokes to the block. A stroke is composed
|
14
|
+
# of a left, top, and chars. E.g.
|
15
|
+
#
|
16
|
+
# yield 10, 3, 'Hello World!'
|
17
|
+
#
|
18
|
+
# paints the string "Hello World!" in the 10th column from the left, 3rd row down.
|
19
|
+
def paint(canvas, left, top, &block)
|
13
20
|
end
|
14
21
|
|
22
|
+
# Return a dimension object that provider the `outer_width` and `outer_height` of the
|
23
|
+
# element being painted.
|
15
24
|
def dimensions
|
16
25
|
end
|
17
26
|
|
18
|
-
|
19
|
-
|
20
|
-
|
27
|
+
def inspect
|
28
|
+
"#{self.class.name.split('::').last}(name=#{name.inspect})"
|
29
|
+
end
|
21
30
|
end
|
22
31
|
private_constant :Painter
|
23
32
|
end
|
@@ -1,49 +1,32 @@
|
|
1
|
-
require 'whirled_peas/template/box_element'
|
2
|
-
require 'whirled_peas/template/container'
|
3
|
-
require 'whirled_peas/template/grid_element'
|
4
|
-
require 'whirled_peas/template/text_element'
|
5
|
-
|
6
|
-
require_relative 'box_painter'
|
7
1
|
require_relative 'canvas'
|
8
|
-
require_relative 'grid_painter'
|
9
|
-
require_relative 'text_painter'
|
10
2
|
|
11
3
|
module WhirledPeas
|
12
4
|
module Graphics
|
13
5
|
class Renderer
|
14
|
-
PAINTERS = {
|
15
|
-
Template::BoxElement => BoxPainter,
|
16
|
-
Template::GridElement => GridPainter,
|
17
|
-
Template::TextElement => TextPainter
|
18
|
-
}
|
19
6
|
def initialize(template, width, height)
|
20
7
|
@template = template
|
21
8
|
@width = width
|
22
9
|
@height = height
|
23
10
|
end
|
24
11
|
|
25
|
-
def paint
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
12
|
+
def paint
|
13
|
+
# Modify the template's settings so that it fills the entire screen
|
14
|
+
template.settings.width = width
|
15
|
+
template.settings.height = height
|
16
|
+
template.settings.sizing = :border
|
17
|
+
template.settings.set_margin(left: 0, top: 0, right: 0, bottom: 0)
|
18
|
+
strokes = [Utils::Ansi.cursor_visible(false), Utils::Ansi.cursor_pos, Utils::Ansi.clear_down]
|
19
|
+
template.paint(Canvas.new(0, 0, width, height), 0, 0) do |left, top, fstring|
|
20
|
+
next unless fstring.length > 0
|
21
|
+
strokes << Utils::Ansi.cursor_pos(left: left, top: top)
|
22
|
+
strokes << fstring
|
23
|
+
end
|
24
|
+
strokes.join
|
31
25
|
end
|
32
26
|
|
33
27
|
private
|
34
28
|
|
35
29
|
attr_reader :template, :width, :height
|
36
|
-
|
37
|
-
def build(element)
|
38
|
-
unless PAINTERS.key?(element.class)
|
39
|
-
raise ArgumentError, "Unable to render #{element.class}"
|
40
|
-
end
|
41
|
-
painter = PAINTERS[element.class].new(element, element.settings, element.name)
|
42
|
-
if element.is_a?(Template::Container)
|
43
|
-
element.each_child { |child| painter.add_child(build(child)) }
|
44
|
-
end
|
45
|
-
painter
|
46
|
-
end
|
47
30
|
end
|
48
31
|
end
|
49
32
|
end
|
@@ -1,35 +1,33 @@
|
|
1
1
|
require 'whirled_peas/utils/formatted_string'
|
2
2
|
require 'whirled_peas/utils/title_font'
|
3
3
|
|
4
|
-
require_relative '
|
5
|
-
require_relative '
|
4
|
+
require_relative 'content_dimensions'
|
5
|
+
require_relative 'content_painter'
|
6
6
|
|
7
7
|
module WhirledPeas
|
8
8
|
module Graphics
|
9
|
-
class TextPainter <
|
10
|
-
def paint(canvas, &block)
|
9
|
+
class TextPainter < ContentPainter
|
10
|
+
def paint(canvas, left, top, &block)
|
11
11
|
return unless canvas.writable?
|
12
12
|
formatting = [*settings.color, *settings.bg_color]
|
13
13
|
formatting << Utils::Ansi::BOLD if settings.bold?
|
14
14
|
if settings.underline? && settings.title_font.nil?
|
15
15
|
formatting << Utils::Ansi::UNDERLINE
|
16
16
|
end
|
17
|
-
|
18
|
-
canvas.stroke(
|
17
|
+
content_lines.each.with_index do |line, index|
|
18
|
+
canvas.stroke(left, top + index, line, formatting, &block)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
TextDimensions.new(content)
|
24
|
-
end
|
22
|
+
private
|
25
23
|
|
26
|
-
def
|
27
|
-
if settings.title_font
|
24
|
+
def content_lines
|
25
|
+
@content_lines = if settings.title_font
|
28
26
|
Utils::TitleFont.to_s(
|
29
|
-
|
27
|
+
content, settings.title_font
|
30
28
|
).split("\n")
|
31
29
|
else
|
32
|
-
|
30
|
+
content.split("\n")
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module WhirledPeas
|
2
|
+
module Settings
|
3
|
+
module Alignment
|
4
|
+
LEFT = :left
|
5
|
+
CENTER = :center
|
6
|
+
RIGHT = :right
|
7
|
+
BETWEEN = :between
|
8
|
+
AROUND = :around
|
9
|
+
EVENLY = :evenly
|
10
|
+
|
11
|
+
DEFAULT = LEFT
|
12
|
+
|
13
|
+
VALID = [LEFT, CENTER, RIGHT, BETWEEN, AROUND, EVENLY]
|
14
|
+
private_constant :VALID
|
15
|
+
|
16
|
+
def self.validate!(align)
|
17
|
+
return if align.nil?
|
18
|
+
return align if VALID.include?(align)
|
19
|
+
raise ArgumentError, "Unsupported alignment: #{align.inspect}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
private_constant :Alignment
|
23
|
+
end
|
24
|
+
end
|
@@ -34,20 +34,20 @@ module WhirledPeas
|
|
34
34
|
'╚', '═', '╩', '╝'
|
35
35
|
)
|
36
36
|
|
37
|
+
DEFAULT = BOLD
|
38
|
+
|
37
39
|
def self.validate!(style)
|
38
|
-
return
|
40
|
+
return if style.nil?
|
41
|
+
return style if style.is_a?(Border::Style)
|
39
42
|
if style.is_a?(Symbol)
|
40
|
-
|
41
|
-
|
42
|
-
style = self.const_get(style.upcase)
|
43
|
-
raise ArgumentError, error_message unless style.is_a?(Style)
|
44
|
-
style
|
45
|
-
rescue NameError
|
46
|
-
raise ArgumentError, error_message
|
47
|
-
end
|
48
|
-
else
|
49
|
-
style
|
43
|
+
style_class = self.const_get(style.upcase)
|
44
|
+
return style_class if style_class.is_a?(Border::Style)
|
50
45
|
end
|
46
|
+
error_message = "Unsupported border style: #{style.inspect}"
|
47
|
+
raise ArgumentError, error_message
|
48
|
+
rescue NameError
|
49
|
+
error_message = "Unsupported border style: #{style.inspect}"
|
50
|
+
raise ArgumentError, error_message
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -82,20 +82,20 @@ module WhirledPeas
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def style
|
85
|
-
@style || Styles::
|
85
|
+
@style || Styles::DEFAULT
|
86
86
|
end
|
87
87
|
|
88
88
|
def style=(val)
|
89
89
|
@style = Styles.validate!(val)
|
90
90
|
end
|
91
91
|
|
92
|
-
def color
|
93
|
-
@color || TextColor::WHITE
|
94
|
-
end
|
95
|
-
|
96
92
|
def color=(val)
|
97
93
|
@color = TextColor.validate!(val)
|
98
94
|
end
|
95
|
+
|
96
|
+
def color
|
97
|
+
@color || TextColor::DEFAULT
|
98
|
+
end
|
99
99
|
end
|
100
100
|
end
|
101
101
|
end
|
@@ -13,25 +13,24 @@ module WhirledPeas
|
|
13
13
|
# @return [Color|Symbol] the value passed in if valid, otherwise an ArgumentError
|
14
14
|
# is raised.
|
15
15
|
def self.validate!(color)
|
16
|
-
return
|
16
|
+
return if color.nil?
|
17
|
+
return color if color.is_a?(self)
|
17
18
|
if color.is_a?(Symbol)
|
18
|
-
error_message = "Unsupported #{self.name.split('::').last}: #{color.inspect}"
|
19
19
|
match = color.to_s.match(/^(bright_)?(\w+)$/)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
color
|
20
|
+
color_class = self.const_get(match[2].upcase)
|
21
|
+
if color_class.is_a?(self)
|
22
|
+
if !match[1]
|
23
|
+
return color_class
|
24
|
+
elsif !color_class.bright?
|
25
|
+
return color_class.bright
|
28
26
|
end
|
29
|
-
rescue NameError
|
30
|
-
raise ArgumentError, error_message
|
31
27
|
end
|
32
|
-
else
|
33
|
-
color
|
34
28
|
end
|
29
|
+
error_message = "Unsupported #{self.name.split('::').last}: #{color.inspect}"
|
30
|
+
raise ArgumentError, error_message
|
31
|
+
rescue NameError
|
32
|
+
error_message = "Unsupported #{self.name.split('::').last}: #{color.inspect}"
|
33
|
+
raise ArgumentError, error_message
|
35
34
|
end
|
36
35
|
|
37
36
|
def initialize(code, bright=false)
|