picture_frame 0.0.1
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.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +123 -0
- data/Rakefile +9 -0
- data/lib/picture_frame/canvas.rb +68 -0
- data/lib/picture_frame/frame.rb +42 -0
- data/lib/picture_frame/image.rb +7 -0
- data/lib/picture_frame/predefined.rb +73 -0
- data/lib/picture_frame/raster.rb +84 -0
- data/lib/picture_frame/stencil.rb +43 -0
- data/lib/picture_frame/version.rb +3 -0
- data/lib/picture_frame.rb +32 -0
- data/picture_frame.gemspec +17 -0
- data/test/canvas_test.rb +56 -0
- data/test/default_frame_test.rb +27 -0
- data/test/picture_frame_test.rb +32 -0
- data/test/raster_test.rb +30 -0
- data/test/stencil_test.rb +35 -0
- data/test/test_helper.rb +1 -0
- metadata +71 -0
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*~
|
2
|
+
*.bak
|
3
|
+
*.orig
|
4
|
+
*.gem
|
5
|
+
*.rbc
|
6
|
+
.bundle
|
7
|
+
.config
|
8
|
+
.yardoc
|
9
|
+
.idea
|
10
|
+
.rake_t_cache
|
11
|
+
Gemfile.lock
|
12
|
+
InstalledFiles
|
13
|
+
_yardoc
|
14
|
+
coverage
|
15
|
+
doc/
|
16
|
+
lib/bundler/man
|
17
|
+
pkg
|
18
|
+
rdoc
|
19
|
+
spec/reports
|
20
|
+
test/tmp
|
21
|
+
test/version_tmp
|
22
|
+
tmp
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michael Schuerig
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# PictureFrame
|
2
|
+
|
3
|
+
Draw predefined or custom frames around text.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'picture_frame'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install picture_frame
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
|
22
|
+
> PictureFrame.showcase("Hello, World!")
|
23
|
+
:default
|
24
|
+
+---------------+
|
25
|
+
| Hello, World! |
|
26
|
+
+---------------+
|
27
|
+
|
28
|
+
:stars
|
29
|
+
*****************
|
30
|
+
* Hello, World! *
|
31
|
+
*****************
|
32
|
+
|
33
|
+
:slashes
|
34
|
+
///////////////////
|
35
|
+
// Hello, World! //
|
36
|
+
///////////////////
|
37
|
+
|
38
|
+
:hashes
|
39
|
+
#################
|
40
|
+
# Hello, World! #
|
41
|
+
#################
|
42
|
+
|
43
|
+
:ornate
|
44
|
+
%=---------------=%
|
45
|
+
∥ ∥
|
46
|
+
| Hello, World! |
|
47
|
+
∥ ∥
|
48
|
+
%=---------------=%
|
49
|
+
|
50
|
+
:ephemeral
|
51
|
+
⌜ ⌝
|
52
|
+
Hello, World!
|
53
|
+
⌞ ⌟
|
54
|
+
|
55
|
+
:box
|
56
|
+
+-----------------+
|
57
|
+
/ /|
|
58
|
+
+-----------------+ |
|
59
|
+
| | |
|
60
|
+
| Hello, World! | |
|
61
|
+
| | +
|
62
|
+
| |/
|
63
|
+
+-----------------+
|
64
|
+
|
65
|
+
> PictureFrame.showcase("Hello\nWorld")
|
66
|
+
:default
|
67
|
+
+-------+
|
68
|
+
| Hello |
|
69
|
+
| World |
|
70
|
+
+-------+
|
71
|
+
|
72
|
+
:stars
|
73
|
+
*********
|
74
|
+
* Hello *
|
75
|
+
* World *
|
76
|
+
*********
|
77
|
+
|
78
|
+
:slashes
|
79
|
+
///////////
|
80
|
+
// Hello //
|
81
|
+
// World //
|
82
|
+
///////////
|
83
|
+
|
84
|
+
:hashes
|
85
|
+
#########
|
86
|
+
# Hello #
|
87
|
+
# World #
|
88
|
+
#########
|
89
|
+
|
90
|
+
:ornate
|
91
|
+
%=-------=%
|
92
|
+
∥ ∥
|
93
|
+
| Hello |
|
94
|
+
| World |
|
95
|
+
∥ ∥
|
96
|
+
%=-------=%
|
97
|
+
|
98
|
+
:ephemeral
|
99
|
+
⌜ ⌝
|
100
|
+
Hello
|
101
|
+
World
|
102
|
+
⌞ ⌟
|
103
|
+
|
104
|
+
:box
|
105
|
+
+---------+
|
106
|
+
/ /|
|
107
|
+
+---------+ |
|
108
|
+
| | |
|
109
|
+
| Hello | |
|
110
|
+
| World | |
|
111
|
+
| | +
|
112
|
+
| |/
|
113
|
+
+---------+
|
114
|
+
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
1. Fork it
|
119
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
120
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
121
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
122
|
+
5. Create new Pull Request
|
123
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'picture_frame/raster'
|
2
|
+
|
3
|
+
module PictureFrame
|
4
|
+
class Canvas < Raster
|
5
|
+
def initialize(*dimensions)
|
6
|
+
rows, cols = 0, 0
|
7
|
+
dimensions.each do |r, c|
|
8
|
+
rows += r
|
9
|
+
cols += c
|
10
|
+
end
|
11
|
+
|
12
|
+
raster = (1..rows).inject([]) { |r| r << ' ' * cols }
|
13
|
+
super(raster)
|
14
|
+
end
|
15
|
+
|
16
|
+
def print_at(pos, raster, start = nil, length = nil)
|
17
|
+
case pos
|
18
|
+
when Array
|
19
|
+
overwrite(pos[0], pos[1], raster)
|
20
|
+
|
21
|
+
when :t, :top
|
22
|
+
overwrite(0, make_range(start, length), raster)
|
23
|
+
when :r, :right
|
24
|
+
overwrite(make_range(start, length), width - raster.width, raster)
|
25
|
+
when :b, :bottom
|
26
|
+
overwrite(height - raster.height, make_range(start, length), raster)
|
27
|
+
when :l, :left
|
28
|
+
overwrite(make_range(start, length), 0, raster)
|
29
|
+
|
30
|
+
when :tl, :top_left
|
31
|
+
overwrite(0, 0, raster)
|
32
|
+
when :tr, :top_right
|
33
|
+
overwrite(0, width - raster.width, raster)
|
34
|
+
when :br, :bottom_right
|
35
|
+
overwrite(height - raster.height, width - raster.width, raster)
|
36
|
+
when :bl, :bottom_left
|
37
|
+
overwrite(height - raster.height, 0, raster)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def overwrite(row_pos, col_pos, raster)
|
44
|
+
if row_pos.is_a?(Range)
|
45
|
+
row_pos.each do |r|
|
46
|
+
overwrite(r, col_pos, raster)
|
47
|
+
end
|
48
|
+
elsif col_pos.is_a?(Range)
|
49
|
+
col_pos.each do |c|
|
50
|
+
overwrite(row_pos, c, raster)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
overwrite1(row_pos, col_pos, raster)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def overwrite1(row_pos, col_pos, raster)
|
58
|
+
cols = raster.width
|
59
|
+
raster.each_with_index do |row, r|
|
60
|
+
@raster[row_pos + r][col_pos, cols] = row
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def make_range(start, length)
|
65
|
+
start..(start + length - 1)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'picture_frame/canvas'
|
2
|
+
require 'picture_frame/image'
|
3
|
+
require 'picture_frame/stencil'
|
4
|
+
|
5
|
+
module PictureFrame
|
6
|
+
class Frame
|
7
|
+
def initialize(template_string, options = {})
|
8
|
+
@template = Stencil.new(template_string)
|
9
|
+
@placeholder = options[:placeholder] || '@'
|
10
|
+
end
|
11
|
+
|
12
|
+
def frame(text)
|
13
|
+
image = Image.new(text)
|
14
|
+
|
15
|
+
Canvas.new(@template.bottom_right, image.dimensions).tap do |canvas|
|
16
|
+
pp = placeholder_position
|
17
|
+
return '' if pp.empty?
|
18
|
+
|
19
|
+
r1, c1 = pp
|
20
|
+
rn, cm = image.dimensions
|
21
|
+
|
22
|
+
canvas.print_at(:tl, @template.slice_from(pp, :tl))
|
23
|
+
canvas.print_at(:tr, @template.slice_from(pp, :tr))
|
24
|
+
canvas.print_at(:br, @template.slice_from(pp, :br))
|
25
|
+
canvas.print_at(:bl, @template.slice_from(pp, :bl))
|
26
|
+
|
27
|
+
canvas.print_at(:t, @template.slice_from(pp, :t), c1, cm)
|
28
|
+
canvas.print_at(:b, @template.slice_from(pp, :b), c1, cm)
|
29
|
+
canvas.print_at(:l, @template.slice_from(pp, :l), r1, rn)
|
30
|
+
canvas.print_at(:r, @template.slice_from(pp, :r), r1, rn)
|
31
|
+
|
32
|
+
canvas.print_at(pp, image)
|
33
|
+
end.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def placeholder_position
|
39
|
+
@placeholder_position ||= @template.position(@placeholder)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module PictureFrame
|
4
|
+
module Predefined
|
5
|
+
|
6
|
+
class << self
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
def default
|
10
|
+
TEMPLATES[:default]
|
11
|
+
end
|
12
|
+
|
13
|
+
def named(name)
|
14
|
+
TEMPLATES[name]
|
15
|
+
end
|
16
|
+
|
17
|
+
def random
|
18
|
+
templates = TEMPLATES.values
|
19
|
+
templates[rand(templates.size)]
|
20
|
+
end
|
21
|
+
|
22
|
+
def each(&block)
|
23
|
+
TEMPLATES.keys.each(&block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
TEMPLATES = {
|
28
|
+
:default => %q{
|
29
|
+
+---+
|
30
|
+
| @ |
|
31
|
+
+---+
|
32
|
+
},
|
33
|
+
:stars => %q{
|
34
|
+
*****
|
35
|
+
* @ *
|
36
|
+
*****
|
37
|
+
},
|
38
|
+
:slashes => %q{
|
39
|
+
///////
|
40
|
+
// @ //
|
41
|
+
///////
|
42
|
+
},
|
43
|
+
:hashes => %q{
|
44
|
+
#####
|
45
|
+
# @ #
|
46
|
+
#####
|
47
|
+
},
|
48
|
+
# FIXME there might be a problem with multibyte chars
|
49
|
+
:ornate => %q{
|
50
|
+
%=---=%
|
51
|
+
∥ ∥
|
52
|
+
| @ |
|
53
|
+
∥ ∥
|
54
|
+
%=---=%
|
55
|
+
},
|
56
|
+
:ephemeral => %q{
|
57
|
+
⌜ ⌝
|
58
|
+
@
|
59
|
+
⌞ ⌟
|
60
|
+
},
|
61
|
+
:box => %q{
|
62
|
+
+-----+
|
63
|
+
/ /|
|
64
|
+
+-----+ |
|
65
|
+
| | |
|
66
|
+
| @ | |
|
67
|
+
| | +
|
68
|
+
| |/
|
69
|
+
+-----+
|
70
|
+
}
|
71
|
+
}.freeze
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module PictureFrame
|
2
|
+
class Raster
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(string_or_rows)
|
6
|
+
@raster = string_or_rows.is_a?(String) ? string_or_rows.split("\n") : string_or_rows
|
7
|
+
|
8
|
+
trim_around!
|
9
|
+
pad_short_rows!
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(other)
|
13
|
+
return unless other.is_a?(Raster)
|
14
|
+
@raster == other.raster
|
15
|
+
end
|
16
|
+
|
17
|
+
def dimensions
|
18
|
+
@dimensions ||= [@raster.size, (@raster.map { |r| r.length }.max || 0)]
|
19
|
+
end
|
20
|
+
|
21
|
+
def top_left
|
22
|
+
[0, 0]
|
23
|
+
end
|
24
|
+
|
25
|
+
def bottom_right
|
26
|
+
[dimensions[0] - 1, dimensions[1] - 1]
|
27
|
+
end
|
28
|
+
|
29
|
+
def height
|
30
|
+
dimensions[0]
|
31
|
+
end
|
32
|
+
|
33
|
+
def width
|
34
|
+
dimensions[1]
|
35
|
+
end
|
36
|
+
|
37
|
+
def at(row, col)
|
38
|
+
@raster[row][col]
|
39
|
+
end
|
40
|
+
|
41
|
+
alias [] at
|
42
|
+
|
43
|
+
def slice(top_left, bottom_right)
|
44
|
+
top_bottom = (top_left[0]..bottom_right[0])
|
45
|
+
left_right = (top_left[1]..bottom_right[1])
|
46
|
+
|
47
|
+
array = top_bottom.inject([]) do |rows, ri|
|
48
|
+
rows << @raster[ri].slice(left_right)
|
49
|
+
end
|
50
|
+
|
51
|
+
self.class.new(array)
|
52
|
+
end
|
53
|
+
|
54
|
+
def each(&block)
|
55
|
+
@raster.each(&block)
|
56
|
+
end
|
57
|
+
|
58
|
+
def each_with_index(&block)
|
59
|
+
@raster.each_with_index(&block)
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s
|
63
|
+
@raster.join("\n")
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
attr_reader :raster
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def trim_around!
|
73
|
+
# trim; extract and add side-trimming
|
74
|
+
@raster.shift while @raster.first && @raster.first.empty?
|
75
|
+
@raster.pop while @raster.last && @raster.last.empty?
|
76
|
+
end
|
77
|
+
|
78
|
+
def pad_short_rows!
|
79
|
+
@raster.each do |r|
|
80
|
+
r << ' ' * (width - r.length)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'picture_frame/raster'
|
2
|
+
|
3
|
+
module PictureFrame
|
4
|
+
class Stencil < Raster
|
5
|
+
def initialize(*)
|
6
|
+
super
|
7
|
+
end
|
8
|
+
|
9
|
+
def position(char)
|
10
|
+
@raster.each_with_index do |row, ri|
|
11
|
+
row.chars.with_index do |cell, ci|
|
12
|
+
return ri, ci if cell == char
|
13
|
+
end
|
14
|
+
end
|
15
|
+
[]
|
16
|
+
end
|
17
|
+
|
18
|
+
def slice_from(pos, to)
|
19
|
+
r, c = pos
|
20
|
+
n, m = bottom_right
|
21
|
+
|
22
|
+
case to
|
23
|
+
when :t, :top
|
24
|
+
slice([0, c], [r - 1, c])
|
25
|
+
when :r, :right
|
26
|
+
slice([r, c + 1], [r, m])
|
27
|
+
when :b, :bottom
|
28
|
+
slice([r + 1, c], [n, c])
|
29
|
+
when :l, :left
|
30
|
+
slice([r, 0], [r, c - 1])
|
31
|
+
|
32
|
+
when :tl, :top_left
|
33
|
+
slice([0, 0], [r - 1, c - 1])
|
34
|
+
when :tr, :top_right
|
35
|
+
slice([0, c + 1], [r - 1, m])
|
36
|
+
when :br, :bottom_right
|
37
|
+
slice([r + 1, c + 1], [n, m])
|
38
|
+
when :bl, :bottom_left
|
39
|
+
slice([r + 1, 0], [n, c - 1])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'picture_frame/version'
|
2
|
+
require 'picture_frame/frame'
|
3
|
+
require 'picture_frame/predefined'
|
4
|
+
|
5
|
+
module PictureFrame
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def create(frame_spec = nil)
|
9
|
+
case frame_spec
|
10
|
+
when String
|
11
|
+
template = frame_spec
|
12
|
+
when :random
|
13
|
+
template = Predefined.random
|
14
|
+
when Symbol
|
15
|
+
template = Predefined.named(frame_spec)
|
16
|
+
when nil
|
17
|
+
template = Predefined.default
|
18
|
+
end
|
19
|
+
raise ArgumentError, "No such template: #{frame_spec.inspect}" unless template
|
20
|
+
Frame.new(template)
|
21
|
+
end
|
22
|
+
|
23
|
+
def showcase(text)
|
24
|
+
Predefined.each do |name|
|
25
|
+
puts name.inspect
|
26
|
+
puts create(name).frame(text)
|
27
|
+
puts
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/picture_frame/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Michael Schuerig"]
|
6
|
+
gem.email = ["michael@schuerig.de"]
|
7
|
+
gem.description = %q{Draw pretty frames around text.}
|
8
|
+
gem.summary = %q{Draw predefined or custom frames around text.}
|
9
|
+
gem.homepage = "https://github.com/mschuerig/picture_frame"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "picture_frame"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PictureFrame::VERSION
|
17
|
+
end
|
data/test/canvas_test.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'picture_frame/canvas'
|
3
|
+
|
4
|
+
class CanvasTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@canvas = PictureFrame::Canvas.new([3, 4])
|
7
|
+
|
8
|
+
@corner_stencil = PictureFrame::Stencil.new("12\nab")
|
9
|
+
@top_bottom_stencil = PictureFrame::Stencil.new("1\na")
|
10
|
+
@left_right_stencil = PictureFrame::Stencil.new("12")
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_dimensions
|
14
|
+
assert_equal [3, 4], @canvas.dimensions
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_print_at_tl
|
18
|
+
@canvas.print_at(:tl, @corner_stencil)
|
19
|
+
assert_equal "12 \nab \n ", @canvas.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_print_at_tr
|
23
|
+
@canvas.print_at(:tr, @corner_stencil)
|
24
|
+
assert_equal " 12\n ab\n ", @canvas.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_print_at_br
|
28
|
+
@canvas.print_at(:br, @corner_stencil)
|
29
|
+
assert_equal " \n 12\n ab", @canvas.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_print_at_bl
|
33
|
+
@canvas.print_at(:bl, @corner_stencil)
|
34
|
+
assert_equal " \n12 \nab ", @canvas.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_print_at_t
|
38
|
+
@canvas.print_at(:t, @top_bottom_stencil, 1, 2)
|
39
|
+
assert_equal " 11 \n aa \n ", @canvas.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_print_at_r
|
43
|
+
@canvas.print_at(:r, @left_right_stencil, 1, 1)
|
44
|
+
assert_equal " \n 12\n ", @canvas.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_print_at_b
|
48
|
+
@canvas.print_at(:b, @top_bottom_stencil, 1, 2)
|
49
|
+
assert_equal " \n 11 \n aa ", @canvas.to_s
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_print_at_l
|
53
|
+
@canvas.print_at(:l, @left_right_stencil, 1, 1)
|
54
|
+
assert_equal " \n12 \n ", @canvas.to_s
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'picture_frame'
|
3
|
+
|
4
|
+
class DefaultFrameTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@frame = PictureFrame.create
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_frame
|
10
|
+
expected =<<-FRAMED
|
11
|
+
+---------------+
|
12
|
+
| Hello, World! |
|
13
|
+
+---------------+
|
14
|
+
FRAMED
|
15
|
+
assert_equal expected.strip, @frame.frame('Hello, World!')
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_multiline
|
19
|
+
expected =<<-FRAMED
|
20
|
+
+--------+
|
21
|
+
| Hello |
|
22
|
+
| World! |
|
23
|
+
+--------+
|
24
|
+
FRAMED
|
25
|
+
assert_equal expected.strip, @frame.frame("Hello\nWorld!")
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PictureFrameTest < Test::Unit::TestCase
|
4
|
+
def test_empty_template
|
5
|
+
frame = PictureFrame.create('')
|
6
|
+
assert_equal '', frame.frame('Hello, World!')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_simplistic_template
|
10
|
+
frame = PictureFrame.create('@')
|
11
|
+
assert_equal 'Hello, World!', frame.frame('Hello, World!')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_horizontal_template
|
15
|
+
frame = PictureFrame.create('- @ -')
|
16
|
+
assert_equal '- Hello, World! -', frame.frame('Hello, World!')
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_vertical_template
|
20
|
+
frame = PictureFrame.create %q{
|
21
|
+
^
|
22
|
+
@
|
23
|
+
v
|
24
|
+
}
|
25
|
+
expected = %q{
|
26
|
+
^^^^^^^^^^^^^
|
27
|
+
Hello, World!
|
28
|
+
vvvvvvvvvvvvv
|
29
|
+
}
|
30
|
+
assert_equal expected.strip, frame.frame('Hello, World!')
|
31
|
+
end
|
32
|
+
end
|
data/test/raster_test.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'picture_frame/raster'
|
3
|
+
|
4
|
+
class RasterTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@raster = PictureFrame::Raster.new(
|
7
|
+
"abcde\n" +
|
8
|
+
"12345\n" +
|
9
|
+
"vwxyz\n"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_dimensions
|
14
|
+
assert_equal [3, 5], @raster.dimensions
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_bottom_right
|
18
|
+
assert_equal [2, 4], @raster.bottom_right
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_at
|
22
|
+
assert_equal '4', @raster[1, 3]
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_slice
|
26
|
+
expected = PictureFrame::Raster.new("bc\n23")
|
27
|
+
assert_equal expected, @raster.slice([0, 1], [1, 2])
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'picture_frame/stencil'
|
3
|
+
|
4
|
+
class StencilTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@corner_stencil = PictureFrame::Stencil.new(
|
7
|
+
"abcde\n" +
|
8
|
+
"12@45\n" +
|
9
|
+
"qrstu\n" +
|
10
|
+
"vwxyz\n"
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_position
|
15
|
+
assert_equal [1, 2], @corner_stencil.position('@')
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_slice_from
|
19
|
+
pp = [1, 2]
|
20
|
+
|
21
|
+
assert_slice_equal ['c'], @corner_stencil.slice_from(pp, :t)
|
22
|
+
assert_slice_equal ['45'], @corner_stencil.slice_from(pp, :r)
|
23
|
+
assert_slice_equal ['s', 'x'], @corner_stencil.slice_from(pp, :b)
|
24
|
+
assert_slice_equal ['12'], @corner_stencil.slice_from(pp, :l)
|
25
|
+
|
26
|
+
assert_slice_equal ['de'], @corner_stencil.slice_from(pp, :tr)
|
27
|
+
assert_slice_equal ['tu', 'yz'], @corner_stencil.slice_from(pp, :br)
|
28
|
+
assert_slice_equal ['qr', 'vw'], @corner_stencil.slice_from(pp, :bl)
|
29
|
+
assert_slice_equal ['ab'], @corner_stencil.slice_from(pp, :tl)
|
30
|
+
end
|
31
|
+
|
32
|
+
def assert_slice_equal(expected, actual_slice)
|
33
|
+
assert_equal expected, actual_slice.send(:raster)
|
34
|
+
end
|
35
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'test/unit'
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: picture_frame
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Schuerig
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-07 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Draw pretty frames around text.
|
15
|
+
email:
|
16
|
+
- michael@schuerig.de
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/picture_frame.rb
|
27
|
+
- lib/picture_frame/canvas.rb
|
28
|
+
- lib/picture_frame/frame.rb
|
29
|
+
- lib/picture_frame/image.rb
|
30
|
+
- lib/picture_frame/predefined.rb
|
31
|
+
- lib/picture_frame/raster.rb
|
32
|
+
- lib/picture_frame/stencil.rb
|
33
|
+
- lib/picture_frame/version.rb
|
34
|
+
- picture_frame.gemspec
|
35
|
+
- test/canvas_test.rb
|
36
|
+
- test/default_frame_test.rb
|
37
|
+
- test/picture_frame_test.rb
|
38
|
+
- test/raster_test.rb
|
39
|
+
- test/stencil_test.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
homepage: https://github.com/mschuerig/picture_frame
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.15
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Draw predefined or custom frames around text.
|
65
|
+
test_files:
|
66
|
+
- test/canvas_test.rb
|
67
|
+
- test/default_frame_test.rb
|
68
|
+
- test/picture_frame_test.rb
|
69
|
+
- test/raster_test.rb
|
70
|
+
- test/stencil_test.rb
|
71
|
+
- test/test_helper.rb
|