term_canvas 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +7 -7
- data/README.md +1 -1
- data/lib/term_canvas/base_screen.rb +66 -64
- data/lib/term_canvas/object.rb +7 -5
- data/lib/term_canvas/rect.rb +31 -29
- data/lib/term_canvas/text.rb +33 -31
- data/lib/term_canvas/version.rb +1 -1
- data/lib/term_canvas.rb +3 -3
- data/term_canvas.gemspec +1 -1
- metadata +9 -9
- /data/{examples → _examples}/curses.rb +0 -0
- /data/{examples → _examples}/simplified_chart.rb +0 -0
- /data/{examples → _examples}/test.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 033fa3247733631e6e4132b06ab0a7265c6f9f04a89cba1beda888c8af4d5de8
|
4
|
+
data.tar.gz: b16b6ed27e7a240346c6f0eaa4f3a9af2ac96d4444e02bba799a448e696c4c2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69c4ef931f14020aacffc18351cfd7a0d57350ef0f00eaff8af2c606a08da6924c87085f5bfd37cb3ef7b3f46c692f894b6c4709f7cf5609c9c8daa9ab1746dc
|
7
|
+
data.tar.gz: a6308cdf09d6584c303ba1020a214826da0503629ed7ce97d3d242fdff567d380ca0b75efd7cbf6c69942f718fab2146d8d68b15e4ffcac7278b37c959ec3c73
|
data/Gemfile.lock
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
term_canvas (0.
|
4
|
+
term_canvas (0.2.0)
|
5
5
|
curses
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
curses (1.
|
10
|
+
curses (1.3.1)
|
11
11
|
diff-lcs (1.3)
|
12
12
|
rake (10.5.0)
|
13
13
|
rspec (3.8.0)
|
14
14
|
rspec-core (~> 3.8.0)
|
15
15
|
rspec-expectations (~> 3.8.0)
|
16
16
|
rspec-mocks (~> 3.8.0)
|
17
|
-
rspec-core (3.8.
|
17
|
+
rspec-core (3.8.1)
|
18
18
|
rspec-support (~> 3.8.0)
|
19
|
-
rspec-expectations (3.8.
|
19
|
+
rspec-expectations (3.8.4)
|
20
20
|
diff-lcs (>= 1.2.0, < 2.0)
|
21
21
|
rspec-support (~> 3.8.0)
|
22
|
-
rspec-mocks (3.8.
|
22
|
+
rspec-mocks (3.8.1)
|
23
23
|
diff-lcs (>= 1.2.0, < 2.0)
|
24
24
|
rspec-support (~> 3.8.0)
|
25
|
-
rspec-support (3.8.
|
25
|
+
rspec-support (3.8.2)
|
26
26
|
|
27
27
|
PLATFORMS
|
28
28
|
ruby
|
29
29
|
|
30
30
|
DEPENDENCIES
|
31
|
-
bundler
|
31
|
+
bundler
|
32
32
|
rake (~> 10.0)
|
33
33
|
rspec (~> 3.0)
|
34
34
|
term_canvas!
|
data/README.md
CHANGED
@@ -1,78 +1,80 @@
|
|
1
1
|
require 'singleton'
|
2
2
|
require 'curses'
|
3
|
-
|
4
|
-
|
3
|
+
module TermCanvas
|
4
|
+
class BaseScreen
|
5
|
+
include Singleton
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
7
|
+
def initialize
|
8
|
+
Curses.init_screen
|
9
|
+
Curses::noecho
|
10
|
+
Curses.curs_set(0)
|
11
|
+
Curses.stdscr.nodelay = 1
|
12
|
+
Curses.start_color
|
13
|
+
Curses.use_default_colors
|
14
|
+
@color_struct = Struct.new(:id, :r, :g, :b)
|
15
|
+
@color_pair_struct = Struct.new(:id, :fc_id, :bc_id)
|
16
|
+
@color_pairs = [@color_pair_struct.new(0, 1, 0)]
|
17
|
+
@colors = [@color_struct.new(0, 0, 0, 0)]
|
18
|
+
@color_id_offset = 16
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
21
|
+
# @param foreground_color [Hash]
|
22
|
+
# :r Red element of color of text.
|
23
|
+
# :g Green element of color of text.
|
24
|
+
# :b Blue element of color of text.
|
25
|
+
# @param background_color [Hash]
|
26
|
+
# :r Red element of color of background.
|
27
|
+
# :g Green element of color of background.
|
28
|
+
# :b Blue element of color of background.
|
29
|
+
def find_or_create_color_pair(foreground_color: nil, background_color:)
|
30
|
+
response_color_pair = nil
|
31
|
+
fc_id = find_or_create_color(
|
32
|
+
@color_struct.new(*foreground_color&.values_at(*@color_struct.members))
|
33
|
+
).id
|
34
|
+
bc_id = find_or_create_color(
|
35
|
+
@color_struct.new(*background_color&.values_at(*@color_struct.members))
|
36
|
+
).id
|
37
|
+
@color_pairs.each do |cp|
|
38
|
+
if cp.fc_id == fc_id && cp.bc_id == bc_id
|
39
|
+
response_color_pair = cp
|
40
|
+
break
|
41
|
+
end
|
40
42
|
end
|
43
|
+
return response_color_pair || create_color_pair(fc_id, bc_id)
|
41
44
|
end
|
42
|
-
return response_color_pair || create_color_pair(fc_id, bc_id)
|
43
|
-
end
|
44
45
|
|
45
|
-
|
46
|
+
private
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
48
|
+
def find_or_create_color(color)
|
49
|
+
return @colors.find { |c| c.id == 0 } if color.r.nil?
|
50
|
+
response_color = nil
|
51
|
+
@colors.each do |_color|
|
52
|
+
if same_color?(color, _color)
|
53
|
+
response_color = _color
|
54
|
+
break
|
55
|
+
end
|
54
56
|
end
|
57
|
+
return response_color || create_color(r: color.r, g: color.g, b: color.b)
|
55
58
|
end
|
56
|
-
return response_color || create_color(r: color.r, g: color.g, b: color.b)
|
57
|
-
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
60
|
+
def create_color(r:, g:, b:)
|
61
|
+
new_color_id = @colors.count + @color_id_offset
|
62
|
+
new_color = @color_struct.new(new_color_id, r, g, b)
|
63
|
+
Curses.init_color(*new_color)
|
64
|
+
@colors << new_color
|
65
|
+
return new_color
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
68
|
+
def create_color_pair(fc_id, bc_id)
|
69
|
+
new_color_pair_id = @color_pairs.count
|
70
|
+
new_color_pair = @color_pair_struct.new(new_color_pair_id, fc_id, bc_id)
|
71
|
+
Curses.init_pair(*new_color_pair)
|
72
|
+
@color_pairs << new_color_pair
|
73
|
+
return new_color_pair
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
76
|
+
def same_color?(color1, color2)
|
77
|
+
return color1.r == color2.r && color1.g == color2.g && color1.b == color2.b
|
78
|
+
end
|
79
|
+
end
|
78
80
|
end
|
data/lib/term_canvas/object.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module TermCanvas
|
2
|
+
class Object
|
3
|
+
attr_reader :index
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
# @param index [Integer]
|
6
|
+
def set_index(index)
|
7
|
+
@object_index = index
|
8
|
+
end
|
7
9
|
end
|
8
10
|
end
|
data/lib/term_canvas/rect.rb
CHANGED
@@ -1,35 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module TermCanvas
|
2
|
+
class Rect < TermCanvas::Object
|
3
|
+
attr_reader :background_color
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
# @param x [Integer] Horizontal position of the object.
|
6
|
+
# @param y [Integer] Vertical position of the object.
|
7
|
+
# @param background_color [Hash] :r Red element of color of background.
|
8
|
+
# @param background_color [Hash] :g Green element of color of background.
|
9
|
+
# @param background_color [Hash] :b Blue element of color of background.
|
10
|
+
def initialize(x:, y:, width:, height:, background_color:)
|
11
|
+
@x = x
|
12
|
+
@y = y
|
13
|
+
@width = width
|
14
|
+
@height = height
|
15
|
+
@background_color = background_color
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
def foreground_color
|
19
|
+
nil
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
# @param win [Curses::Window] Window to draw
|
23
|
+
def draw(win)
|
24
|
+
color_pair_id = TermCanvas::BaseScreen.instance.find_or_create_color_pair(
|
25
|
+
background_color: @background_color,
|
26
|
+
).id
|
27
|
+
color_pair = Curses.color_pair(color_pair_id)
|
28
|
+
win.setpos(@y, @x)
|
29
|
+
win.attron(color_pair)
|
30
|
+
@height.times do
|
31
|
+
win.addstr(" " * @width)
|
32
|
+
win.setpos(win.cury + 1, @x)
|
33
|
+
end
|
34
|
+
win.attroff(color_pair)
|
32
35
|
end
|
33
|
-
win.attroff(color_pair)
|
34
36
|
end
|
35
37
|
end
|
data/lib/term_canvas/text.rb
CHANGED
@@ -1,35 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module TermCanvas
|
2
|
+
class Text < Object::TermCanvas
|
3
|
+
attr_reader :foreground_color, :background_color
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
5
|
+
# @param x [Integer] Horizontal position of the object.
|
6
|
+
# @param y [Integer] Vertical position of the object.
|
7
|
+
# @param body [String] Text body.
|
8
|
+
# @param foreground_color [Hash]
|
9
|
+
# :r Red element of color of text.
|
10
|
+
# :g Green element of color of text.
|
11
|
+
# :b Blue element of color of text.
|
12
|
+
# @param background_color [Hash]
|
13
|
+
# :r Red element of color of background.
|
14
|
+
# :g Green element of color of background.
|
15
|
+
# :b Blue element of color of background.
|
16
|
+
def initialize(x:, y:, body:, foreground_color:, background_color:)
|
17
|
+
@x = x
|
18
|
+
@y = y
|
19
|
+
@body = body
|
20
|
+
@foreground_color = foreground_color
|
21
|
+
@background_color = background_color
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
24
|
+
# @param win [Curses::Window] Window to draw
|
25
|
+
def draw(win)
|
26
|
+
color_pair_id = TermCanvas::BaseScreen.instance.find_or_create_color_pair(
|
27
|
+
foreground_color: @foreground_color,
|
28
|
+
background_color: @background_color,
|
29
|
+
).id
|
30
|
+
color_pair = Curses.color_pair(color_pair_id)
|
31
|
+
win.setpos(@y, @x)
|
32
|
+
win.attron(color_pair)
|
33
|
+
win.addstr(@body)
|
34
|
+
win.attroff(color_pair)
|
35
|
+
end
|
34
36
|
end
|
35
37
|
end
|
data/lib/term_canvas/version.rb
CHANGED
data/lib/term_canvas.rb
CHANGED
@@ -11,7 +11,7 @@ class TermCanvas
|
|
11
11
|
# @param w [Integer] Width of the window.
|
12
12
|
# @param h [Integer] Height of the window.
|
13
13
|
def initialize(x:, y:, w:, h:)
|
14
|
-
BaseScreen.instance
|
14
|
+
TermCanvas::BaseScreen.instance
|
15
15
|
@win = Curses::Window.new(h, w, y, x)
|
16
16
|
@x = x
|
17
17
|
@y = y
|
@@ -55,7 +55,7 @@ class TermCanvas
|
|
55
55
|
# Add text object to the window but not display.
|
56
56
|
# @param object [Text] Text instance
|
57
57
|
def text(object)
|
58
|
-
raise 'The argument must be Text' if !(Text === object)
|
58
|
+
raise 'The argument must be Text' if !(TermCanvas::Text === object)
|
59
59
|
object.set_index(@object_index)
|
60
60
|
@objects << object
|
61
61
|
@object_index += 1
|
@@ -63,7 +63,7 @@ class TermCanvas
|
|
63
63
|
|
64
64
|
# Add rect object to the window but not display.
|
65
65
|
def rect(object)
|
66
|
-
raise 'The argument must be Rect' if !(Rect === object)
|
66
|
+
raise 'The argument must be Rect' if !(TermCanvas::Rect === object)
|
67
67
|
object.set_index(@object_index)
|
68
68
|
@objects << object
|
69
69
|
@object_index += 1
|
data/term_canvas.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
|
23
23
|
spec.add_dependency "curses"
|
24
24
|
|
25
|
-
spec.add_development_dependency "bundler"
|
25
|
+
spec.add_development_dependency "bundler"
|
26
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
27
27
|
spec.add_development_dependency "rspec", "~> 3.0"
|
28
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: term_canvas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kthatoto
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: curses
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,12 +81,12 @@ files:
|
|
81
81
|
- LICENSE.txt
|
82
82
|
- README.md
|
83
83
|
- Rakefile
|
84
|
+
- _examples/curses.rb
|
85
|
+
- _examples/simplified_chart.rb
|
86
|
+
- _examples/test.rb
|
84
87
|
- bin/console
|
85
88
|
- bin/setup
|
86
89
|
- bin/update
|
87
|
-
- examples/curses.rb
|
88
|
-
- examples/simplified_chart.rb
|
89
|
-
- examples/test.rb
|
90
90
|
- lib/term_canvas.rb
|
91
91
|
- lib/term_canvas/base_screen.rb
|
92
92
|
- lib/term_canvas/object.rb
|
File without changes
|
File without changes
|
File without changes
|