draw 1.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.
- checksums.yaml +7 -0
- data/README.md +3 -0
- data/lib/canvas.rb +101 -0
- data/lib/card.rb +95 -0
- data/lib/dot.rb +37 -0
- data/lib/test_canvas.rb +23 -0
- data/lib/test_card.rb +11 -0
- data/lib/test_dot.rb +21 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2627066879b020d9740eceee7002e70648cbc0ff
|
4
|
+
data.tar.gz: be733a248c9a5314723e9b7655d7e4e89a45b4f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbdb3fbbf6c746a6897d04f5db26b6b82ae9a18379999a08b2bb094a96b39f837dc45c9992d4c037a93278b12babb1e04893e1d4a708d6765eceecef268cbc62
|
7
|
+
data.tar.gz: 45237997bfe9381aac46e947b5ba6805b2369982ae1695711dbd883e63c73f8462b2ace54b5c8137871d144b9ec116d52d21a08668ebc85e02966f6250f70d02
|
data/README.md
ADDED
data/lib/canvas.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
class Canvas
|
2
|
+
require_relative 'dot'
|
3
|
+
require 'cjk_helper'
|
4
|
+
|
5
|
+
attr_accessor :height
|
6
|
+
attr_accessor :width
|
7
|
+
attr_accessor :dots
|
8
|
+
|
9
|
+
def initialize(height:, width:, frame_style:)
|
10
|
+
if !height.is_a?(Fixnum) || !width.is_a?(Fixnum)
|
11
|
+
raise 'FATAL: height and width must given and should be number'
|
12
|
+
end
|
13
|
+
|
14
|
+
@height = height
|
15
|
+
@width = width
|
16
|
+
|
17
|
+
if frame_style == 'dotted'
|
18
|
+
init_canvas_with_style(height: @height, width: @width, symbol:'.')
|
19
|
+
elsif frame_style == 'star'
|
20
|
+
init_canvas_with_style(height: @height, width: @width, symbol:'*')
|
21
|
+
else
|
22
|
+
@dots = Array.new(height) { Array.new(width, nil) }
|
23
|
+
|
24
|
+
(0...@height).each do |j|
|
25
|
+
(0...@width).each do |i|
|
26
|
+
@dots[j][i] = Dot.new(x: i, y: j, symbol:nil)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def init_canvas_with_style(height:, width:, symbol:)
|
33
|
+
@dots = Array.new(height) { Array.new(width, nil) }
|
34
|
+
|
35
|
+
(0...@height).each do |j|
|
36
|
+
(0...@width).each do |i|
|
37
|
+
if i == 0 || i == @width - 1 || j == 0 || j == @height - 1
|
38
|
+
@dots[j][i] = Dot.new(x: i, y: j, symbol:symbol)
|
39
|
+
@dots[j][i].occupied = true
|
40
|
+
else
|
41
|
+
@dots[j][i] = Dot.new(x: i, y: j, symbol:nil)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def draw(y:, x:, symbol:)
|
48
|
+
if !y.is_a?(Fixnum) || !x.is_a?(Fixnum)
|
49
|
+
raise 'FATAL: x and y must given and should be number'
|
50
|
+
end
|
51
|
+
|
52
|
+
if y >= @height
|
53
|
+
raise 'FATAL: y should not be bigger than canvas height'
|
54
|
+
end
|
55
|
+
|
56
|
+
if x >= @width
|
57
|
+
raise 'FATAL: x should not be bigger than canvas width'
|
58
|
+
end
|
59
|
+
|
60
|
+
if @dots[y][x].occupied
|
61
|
+
raise "FATAL: this dot(#{x}, #{y}) is occupied"
|
62
|
+
end
|
63
|
+
|
64
|
+
# ++
|
65
|
+
# CJK character takes two-char width on terminal print
|
66
|
+
# so we need take care of it especailly
|
67
|
+
# ++
|
68
|
+
if CJKHelper.is_cjk(symbol)
|
69
|
+
if x + 1 < @width
|
70
|
+
@dots[y][x].symbol = symbol
|
71
|
+
@dots[y][x + 1].occupied = true
|
72
|
+
@dots[y][x + 1].symbol = nil
|
73
|
+
else
|
74
|
+
@dots[y+1][0].symbol = symbol
|
75
|
+
@dots[y+1][1].occupied = true
|
76
|
+
@dots[y+1][1].symbol = nil
|
77
|
+
end
|
78
|
+
else
|
79
|
+
@dots[y][x].symbol = symbol
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def show
|
84
|
+
canvas = ''
|
85
|
+
(0...@height).each do |i|
|
86
|
+
canvas += "\n"
|
87
|
+
(0...@width).each do |j|
|
88
|
+
if !@dots[i][j].occupied && @dots[i][j].symbol == nil
|
89
|
+
canvas += ' '
|
90
|
+
else
|
91
|
+
canvas += @dots[i][j].symbol.to_s
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
puts canvas
|
96
|
+
end
|
97
|
+
|
98
|
+
def occupied?(x:, y:)
|
99
|
+
@dots[y][x].occupied
|
100
|
+
end
|
101
|
+
end
|
data/lib/card.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require_relative 'canvas'
|
2
|
+
|
3
|
+
class Card
|
4
|
+
require 'cjk_helper'
|
5
|
+
|
6
|
+
attr_reader :canvas
|
7
|
+
|
8
|
+
def initialize(title:, sub_title:, note:, from:)
|
9
|
+
@title = title
|
10
|
+
@sub_title = sub_title
|
11
|
+
@note = note
|
12
|
+
@from = from
|
13
|
+
|
14
|
+
@canvas = Canvas.new(height: 10, width: 62, frame_style:'dotted')
|
15
|
+
|
16
|
+
if @title.length < 60
|
17
|
+
cjk_in_title = CJKHelper.get_cjk(@title)
|
18
|
+
@title_x_start = (60 - @title.length - cjk_in_title.length)/2
|
19
|
+
else
|
20
|
+
raise "title too long"
|
21
|
+
end
|
22
|
+
|
23
|
+
if @sub_title.length < 60
|
24
|
+
cjk_in_sub_title = CJKHelper.get_cjk(@sub_title)
|
25
|
+
@sub_title_x_start = (60 - @sub_title.length - cjk_in_sub_title.length)/2
|
26
|
+
else
|
27
|
+
raise "sub title too long"
|
28
|
+
end
|
29
|
+
|
30
|
+
init_title
|
31
|
+
init_sub_title
|
32
|
+
init_note
|
33
|
+
init_from
|
34
|
+
end
|
35
|
+
|
36
|
+
def init_title
|
37
|
+
y = 1
|
38
|
+
x = @title_x_start
|
39
|
+
@title.chars.each do |c|
|
40
|
+
while @canvas.occupied?(x: x, y: y)
|
41
|
+
x += 1 if x < 60
|
42
|
+
end
|
43
|
+
@canvas.draw(y: y, x: x, symbol: c)
|
44
|
+
x += 1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def init_sub_title
|
49
|
+
y = 2
|
50
|
+
x = @sub_title_x_start
|
51
|
+
@sub_title.chars.each do |c|
|
52
|
+
while @canvas.occupied?(x: x, y: y)
|
53
|
+
x += 1 if x < 60
|
54
|
+
end
|
55
|
+
|
56
|
+
@canvas.draw(y: y, x: x, symbol: c)
|
57
|
+
x += 1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def init_note
|
62
|
+
return if !@note
|
63
|
+
y = 4
|
64
|
+
x = 2
|
65
|
+
@note.chars.each do |c|
|
66
|
+
if x <= 60
|
67
|
+
while @canvas.occupied?(y:y, x:x)
|
68
|
+
x += 1 if x < 60
|
69
|
+
end
|
70
|
+
@canvas.draw(y: y, x: x, symbol: c)
|
71
|
+
else
|
72
|
+
y += 1
|
73
|
+
x = 1
|
74
|
+
end
|
75
|
+
x += 1
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def init_from
|
80
|
+
y = 10 - 2
|
81
|
+
x = 60 - @from.length
|
82
|
+
@from.chars.each do |c|
|
83
|
+
while @canvas.occupied?(x: x, y: y)
|
84
|
+
x += 1 if x < 60
|
85
|
+
end
|
86
|
+
|
87
|
+
@canvas.draw(y: y, x: x, symbol: c)
|
88
|
+
x += 1
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def show
|
93
|
+
@canvas.show
|
94
|
+
end
|
95
|
+
end
|
data/lib/dot.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class Dot
|
2
|
+
attr_accessor :x
|
3
|
+
attr_accessor :y
|
4
|
+
attr_accessor :symbol
|
5
|
+
attr_accessor :occupied
|
6
|
+
|
7
|
+
def initialize(x:, y:, symbol:)
|
8
|
+
if x.is_a?(Fixnum) && y.is_a?(Fixnum)
|
9
|
+
@x = x
|
10
|
+
@y = y
|
11
|
+
@symbol = symbol
|
12
|
+
@occupied = false
|
13
|
+
else
|
14
|
+
raise 'FATAL: x and y coordinates must given and should be number'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def left
|
19
|
+
Dot.new(x: @x - 1, y: @y, symbol:nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
def right
|
23
|
+
Dot.new(x: @x + 1, y: @y, symbol:nil)
|
24
|
+
end
|
25
|
+
|
26
|
+
def top
|
27
|
+
Dot.new(x: @x, y: @y - 1, symbol:nil)
|
28
|
+
end
|
29
|
+
|
30
|
+
def below
|
31
|
+
Dot.new(x: @x, y: @y + 1, symbol:nil)
|
32
|
+
end
|
33
|
+
|
34
|
+
def show
|
35
|
+
puts @symbol
|
36
|
+
end
|
37
|
+
end
|
data/lib/test_canvas.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'canvas'
|
2
|
+
|
3
|
+
describe Canvas do
|
4
|
+
before do
|
5
|
+
@canvas = Canvas.new(height:10, width:60, frame_style:'dotted')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should print out the figure" do
|
9
|
+
@canvas.show
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should draw a dot on 5x5" do
|
13
|
+
@canvas.draw(x:1, y:1, symbol:'你')
|
14
|
+
@canvas.draw(x:3, y:1, symbol:'好')
|
15
|
+
@canvas.draw(x:4, y:2, symbol:'吗')
|
16
|
+
@canvas.draw(x:6, y:3, symbol:'@')
|
17
|
+
@canvas.draw(x:7, y:4, symbol:'@')
|
18
|
+
@canvas.show
|
19
|
+
|
20
|
+
expect(@canvas.occupied?(x:2, y:1)).to eq(true)
|
21
|
+
expect(@canvas.occupied?(x:1, y:1)).to eq(false)
|
22
|
+
end
|
23
|
+
end
|
data/lib/test_card.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative 'card'
|
2
|
+
|
3
|
+
describe Card do
|
4
|
+
before do
|
5
|
+
@card = Card.new(title: "Nice Place Is A Place Where erveryone loves it", sub_title: "nice way", note: "我觉得should have a nice table", from: "@metrue")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should show something" do
|
9
|
+
@card.show
|
10
|
+
end
|
11
|
+
end
|
data/lib/test_dot.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative 'dot'
|
2
|
+
|
3
|
+
describe Dot do
|
4
|
+
before do
|
5
|
+
@dot = Dot.new(x: 5, y: 0, symbol: 'a')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should not create a dot with unFixnum x,y" do
|
9
|
+
expect { @dot = Dot.new(x: 'a', y: 0, symbol: 'a') }.to raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should get the left dot of it" do
|
13
|
+
left_dot = Dot.new(x: 4, y: 0, symbol: nil)
|
14
|
+
expect(@dot.left.x).to eq(left_dot.x)
|
15
|
+
expect(@dot.left.y).to eq(left_dot.y)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should print out it self" do
|
19
|
+
@dot.show
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: draw
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Minghe Huang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: hacker lives in terminal only
|
14
|
+
email: h.mignhe@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- lib/canvas.rb
|
21
|
+
- lib/card.rb
|
22
|
+
- lib/dot.rb
|
23
|
+
- lib/test_canvas.rb
|
24
|
+
- lib/test_card.rb
|
25
|
+
- lib/test_dot.rb
|
26
|
+
homepage: http://minghe.me
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.4.5
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: a lib to help you draw a nice shape on your unix terminal
|
50
|
+
test_files: []
|
51
|
+
has_rdoc:
|