Skip to content

Commit 0025aa2

Browse files
authored
Merge pull request #39 from nanobowers/block
Block
2 parents d81bc88 + d99b917 commit 0025aa2

10 files changed

Lines changed: 101 additions & 1 deletion

File tree

example/ex_canvases.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/env ruby
2+
$LOAD_PATH << "#{__dir__}/../lib"
3+
require "unicode_plot"
4+
5+
# example of line plots using different canvases
6+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Default Lineplot").render
7+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Ascii Lineplot", canvas: :ascii).render
8+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Dot Lineplot", canvas: :dot).render
9+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Block Lineplot", canvas: :block).render

lib/unicode_plot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require 'unicode_plot/lookup_canvas'
1414
require 'unicode_plot/ascii_canvas'
1515
require 'unicode_plot/dot_canvas'
16+
require 'unicode_plot/block_canvas'
1617

1718
require 'unicode_plot/plot'
1819
require 'unicode_plot/grid_plot'

lib/unicode_plot/block_canvas.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module UnicodePlot
2+
# The `BlockCanvas` is also Unicode-based.
3+
# It has half the resolution of the `BrailleCanvas`.
4+
# In contrast to BrailleCanvas, the pixels don't
5+
# have visible spacing between them.
6+
# This canvas effectively turns every character
7+
# into 4 pixels that can individually be manipulated
8+
# using binary operations.
9+
class BlockCanvas < LookupCanvas
10+
X_PIXEL_PER_CHAR = 2
11+
Y_PIXEL_PER_CHAR = 2
12+
13+
def initialize(width, height, fill_char=0, **kw)
14+
super(width, height,
15+
X_PIXEL_PER_CHAR,
16+
Y_PIXEL_PER_CHAR,
17+
fill_char,
18+
**kw)
19+
end
20+
21+
BLOCK_SIGNS = [
22+
[0b1000, 0b0010].freeze,
23+
[0b0100, 0b0001].freeze
24+
].freeze
25+
26+
BLOCK_DECODE = [
27+
-' ', -'▗', -'▖', -'▄',
28+
-'▝', -'▐', -'▞', -'▟',
29+
-'▘', -'▚', -'▌', -'▙',
30+
-'▀', -'▜', -'▛', -'█'
31+
].freeze
32+
33+
def lookup_encode(x,y) ; BLOCK_SIGNS[x][y] ; end
34+
def lookup_decode(x) ; BLOCK_DECODE[x] ; end
35+
end
36+
end

lib/unicode_plot/canvas.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def self.create(canvas_type, width, height, **kw)
1212
DensityCanvas.new(width, height, **kw)
1313
when :dot
1414
DotCanvas.new(width, height, **kw)
15+
when :block
16+
BlockCanvas.new(width, height, **kw)
1517
else
1618
raise ArgumentError, "unknown canvas type: #{canvas_type}"
1719
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
▛▄  ▘         ▘   ▖  ▝   ▗  ▝      ▘▗▙▞▀
2+
▌ ▀▄               ▖        ▗   ▗▄▞▀▘  
3+
▌▘  ▀▄   ▝ ▐ ▌      ▄    ▘  ▗▄▞▀▘   ▘  
4+
▌   ▀▀▀█▀▛▀▀▀▀▀▀▀▀▀▀▄▄▄▄▄▄▟█▙▄▄▄▙▄▄▄▝▘ 
5+
▌▗ ▝    ▀▙          ▗▄▞▀▘   ▖   ▖▝  ▘  
6+
▌   ▖    ▖▀▄   ▝▗▄▞▛▘ ▗       ▖   ▖▘▝  
7+
▌      ▗    ▜▄▞▀▘   ▘      ▝     ▗     ▗
8+
▌ ▘ ▖   ▗▄▞▀▘ ▀▄    ▖        ▗▗        
9+
▌  ▄▗▄▞▀▘      ▘▀▄   ▝▗         ▗▝    ▝
10+
▙▙▞▀▀  ▖          ▀▄▖  ▀▘▗        ▖ ▖  
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
▛▄ ▘ ▘ ▖ ▝ ▗ ▝ ▘▗▙▞▀
2+
▌ ▀▄ ▖ ▗ ▗▄▞▀▘
3+
▌▘ ▀▄ ▝ ▐ ▌ ▄ ▘ ▗▄▞▀▘ ▘
4+
▌ ▀▀▀█▀▛▀▀▀▀▀▀▀▀▀▀▄▄▄▄▄▄▟█▙▄▄▄▙▄▄▄▝▘
5+
▌▗ ▝ ▀▙ ▗▄▞▀▘ ▖ ▖▝ ▘
6+
▌ ▖ ▖▀▄ ▝▗▄▞▛▘ ▗ ▖ ▖▘▝
7+
▌ ▗ ▜▄▞▀▘ ▘ ▝ ▗ ▗
8+
▌ ▘ ▖ ▗▄▞▀▘ ▀▄ ▖ ▗▗
9+
▌ ▄▗▄▞▀▘ ▘▀▄ ▝▗ ▗▝ ▝
10+
▙▙▞▀▀ ▖ ▀▄▖ ▀▘▗ ▖ ▖
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
▌▘  ▀▄   ▝ ▐ ▌      ▄    ▘  ▗▄▞▀▘   ▘  
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
┌────────────────────────────────────────┐
2+
│▛▄  ▘         ▘   ▖  ▝   ▗  ▝      ▘▗▙▞▀│
3+
│▌ ▀▄               ▖        ▗   ▗▄▞▀▘   │
4+
│▌▘  ▀▄   ▝ ▐ ▌      ▄    ▘  ▗▄▞▀▘   ▘   │
5+
│▌   ▀▀▀█▀▛▀▀▀▀▀▀▀▀▀▀▄▄▄▄▄▄▟█▙▄▄▄▙▄▄▄▝▘  │
6+
│▌▗ ▝    ▀▙          ▗▄▞▀▘   ▖   ▖▝  ▘   │
7+
│▌   ▖    ▖▀▄   ▝▗▄▞▛▘ ▗       ▖   ▖▘▝   │
8+
│▌      ▗    ▜▄▞▀▘   ▘      ▝     ▗     ▗│
9+
│▌ ▘ ▖   ▗▄▞▀▘ ▀▄    ▖        ▗▗         │
10+
│▌  ▄▗▄▞▀▘      ▘▀▄   ▝▗         ▗▝    ▝ │
11+
│▙▙▞▀▀  ▖          ▀▄▖  ▀▘▗        ▖ ▖   │
12+
└────────────────────────────────────────┘
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
┌────────────────────────────────────────┐
2+
│▛▄ ▘ ▘ ▖ ▝ ▗ ▝ ▘▗▙▞▀│
3+
│▌ ▀▄ ▖ ▗ ▗▄▞▀▘ │
4+
│▌▘ ▀▄ ▝ ▐ ▌ ▄ ▘ ▗▄▞▀▘ ▘ │
5+
│▌ ▀▀▀█▀▛▀▀▀▀▀▀▀▀▀▀▄▄▄▄▄▄▟█▙▄▄▄▙▄▄▄▝▘ │
6+
│▌▗ ▝ ▀▙ ▗▄▞▀▘ ▖ ▖▝ ▘ │
7+
│▌ ▖ ▖▀▄ ▝▗▄▞▛▘ ▗ ▖ ▖▘▝ │
8+
│▌ ▗ ▜▄▞▀▘ ▘ ▝ ▗ ▗│
9+
│▌ ▘ ▖ ▗▄▞▀▘ ▀▄ ▖ ▗▗ │
10+
│▌ ▄▗▄▞▀▘ ▘▀▄ ▝▗ ▗▝ ▝ │
11+
│▙▙▞▀▀ ▖ ▀▄▖ ▀▘▗ ▖ ▖ │
12+
└────────────────────────────────────────┘

test/test-canvas.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module CanvasTestCases
66
ascii: UnicodePlot::AsciiCanvas,
77
braille: UnicodePlot::BrailleCanvas,
88
density: UnicodePlot::DensityCanvas,
9-
dot: UnicodePlot::DotCanvas
9+
dot: UnicodePlot::DotCanvas,
10+
block: UnicodePlot::BlockCanvas
1011
}.freeze
1112

1213
def self.included(mod)
@@ -136,3 +137,9 @@ class DotCanvasTest < Test::Unit::TestCase
136137

137138
include CanvasTestCases
138139
end
140+
141+
class BlockCanvasTest < Test::Unit::TestCase
142+
CANVAS_NAME = :block
143+
144+
include CanvasTestCases
145+
end

0 commit comments

Comments
 (0)