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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5d343b9c662b9a2613d77e82f2cd905103e78416715e6c26044f5605169a847
4
- data.tar.gz: 1feb22194cfa0a56d7ef10d6e01b270976cfba79e89711a52049e1b8460766d0
3
+ metadata.gz: 033fa3247733631e6e4132b06ab0a7265c6f9f04a89cba1beda888c8af4d5de8
4
+ data.tar.gz: b16b6ed27e7a240346c6f0eaa4f3a9af2ac96d4444e02bba799a448e696c4c2d
5
5
  SHA512:
6
- metadata.gz: 052d7fac4a035d0257c196ed36b637dec5ff03e39dcde2dc60096a32b34cfe0bb82fa6fab79c14f036f5670b817ce0eb56d19997bd25f9ec00821f5fbaedbd27
7
- data.tar.gz: 7821d72cfa812494e3a098f0ad14453cf3b610ea53dee76d74a6a4c254e300ef45fa74937f9fe898a65d4199048500c8cfc34d47cc7df02301d4e9569d025b0a
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.1.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.2.7)
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.0)
17
+ rspec-core (3.8.1)
18
18
  rspec-support (~> 3.8.0)
19
- rspec-expectations (3.8.2)
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.0)
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.0)
25
+ rspec-support (3.8.2)
26
26
 
27
27
  PLATFORMS
28
28
  ruby
29
29
 
30
30
  DEPENDENCIES
31
- bundler (~> 1.16)
31
+ bundler
32
32
  rake (~> 10.0)
33
33
  rspec (~> 3.0)
34
34
  term_canvas!
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # TermCanvas
2
2
  Wrapper library for `curses` of ruby.
3
- You can handle colors easily.
3
+ You can easily draw text on your terminal and handle colors easily.
4
4
 
5
5
  ## Installation
6
6
 
@@ -1,78 +1,80 @@
1
1
  require 'singleton'
2
2
  require 'curses'
3
- class BaseScreen
4
- include Singleton
3
+ module TermCanvas
4
+ class BaseScreen
5
+ include Singleton
5
6
 
6
- def initialize
7
- Curses.init_screen
8
- Curses::noecho
9
- Curses.curs_set(0)
10
- Curses.stdscr.nodelay = 1
11
- Curses.start_color
12
- Curses.use_default_colors
13
- @color_struct = Struct.new(:id, :r, :g, :b)
14
- @color_pair_struct = Struct.new(:id, :fc_id, :bc_id)
15
- @color_pairs = [@color_pair_struct.new(0, 1, 0)]
16
- @colors = [@color_struct.new(0, 0, 0, 0)]
17
- @color_id_offset = 16
18
- end
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
- # @param foreground_color [Hash]
21
- # :r Red element of color of text.
22
- # :g Green element of color of text.
23
- # :b Blue element of color of text.
24
- # @param background_color [Hash]
25
- # :r Red element of color of background.
26
- # :g Green element of color of background.
27
- # :b Blue element of color of background.
28
- def find_or_create_color_pair(foreground_color: nil, background_color:)
29
- response_color_pair = nil
30
- fc_id = find_or_create_color(
31
- @color_struct.new(*foreground_color&.values_at(*@color_struct.members))
32
- ).id
33
- bc_id = find_or_create_color(
34
- @color_struct.new(*background_color&.values_at(*@color_struct.members))
35
- ).id
36
- @color_pairs.each do |cp|
37
- if cp.fc_id == fc_id && cp.bc_id == bc_id
38
- response_color_pair = cp
39
- break
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
- private
46
+ private
46
47
 
47
- def find_or_create_color(color)
48
- return @colors.find { |c| c.id == 0 } if color.r.nil?
49
- response_color = nil
50
- @colors.each do |_color|
51
- if same_color?(color, _color)
52
- response_color = _color
53
- break
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
- def create_color(r:, g:, b:)
60
- new_color_id = @colors.count + @color_id_offset
61
- new_color = @color_struct.new(new_color_id, r, g, b)
62
- Curses.init_color(*new_color)
63
- @colors << new_color
64
- return new_color
65
- end
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
- def create_color_pair(fc_id, bc_id)
68
- new_color_pair_id = @color_pairs.count
69
- new_color_pair = @color_pair_struct.new(new_color_pair_id, fc_id, bc_id)
70
- Curses.init_pair(*new_color_pair)
71
- @color_pairs << new_color_pair
72
- return new_color_pair
73
- end
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
- def same_color?(color1, color2)
76
- return color1.r == color2.r && color1.g == color2.g && color1.b == color2.b
77
- end
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
@@ -1,8 +1,10 @@
1
- class Object::TermCanvas
2
- attr_reader :index
1
+ module TermCanvas
2
+ class Object
3
+ attr_reader :index
3
4
 
4
- # @param index [Integer]
5
- def set_index(index)
6
- @object_index = index
5
+ # @param index [Integer]
6
+ def set_index(index)
7
+ @object_index = index
8
+ end
7
9
  end
8
10
  end
@@ -1,35 +1,37 @@
1
- class Rect < Object::TermCanvas
2
- attr_reader :background_color
1
+ module TermCanvas
2
+ class Rect < TermCanvas::Object
3
+ attr_reader :background_color
3
4
 
4
- # @param x [Integer] Horizontal position of the object.
5
- # @param y [Integer] Vertical position of the object.
6
- # @param background_color [Hash] :r Red element of color of background.
7
- # @param background_color [Hash] :g Green element of color of background.
8
- # @param background_color [Hash] :b Blue element of color of background.
9
- def initialize(x:, y:, width:, height:, background_color:)
10
- @x = x
11
- @y = y
12
- @width = width
13
- @height = height
14
- @background_color = background_color
15
- end
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
- def foreground_color
18
- nil
19
- end
18
+ def foreground_color
19
+ nil
20
+ end
20
21
 
21
- # @param win [Curses::Window] Window to draw
22
- def draw(win)
23
- color_pair_id = BaseScreen.instance.find_or_create_color_pair(
24
- background_color: @background_color,
25
- ).id
26
- color_pair = Curses.color_pair(color_pair_id)
27
- win.setpos(@y, @x)
28
- win.attron(color_pair)
29
- @height.times do
30
- win.addstr(" " * @width)
31
- win.setpos(win.cury + 1, @x)
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
@@ -1,35 +1,37 @@
1
- class Text < Object::TermCanvas
2
- attr_reader :foreground_color, :background_color
1
+ module TermCanvas
2
+ class Text < Object::TermCanvas
3
+ attr_reader :foreground_color, :background_color
3
4
 
4
- # @param x [Integer] Horizontal position of the object.
5
- # @param y [Integer] Vertical position of the object.
6
- # @param body [String] Text body.
7
- # @param foreground_color [Hash]
8
- # :r Red element of color of text.
9
- # :g Green element of color of text.
10
- # :b Blue element of color of text.
11
- # @param background_color [Hash]
12
- # :r Red element of color of background.
13
- # :g Green element of color of background.
14
- # :b Blue element of color of background.
15
- def initialize(x:, y:, body:, foreground_color:, background_color:)
16
- @x = x
17
- @y = y
18
- @body = body
19
- @foreground_color = foreground_color
20
- @background_color = background_color
21
- end
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
- # @param win [Curses::Window] Window to draw
24
- def draw(win)
25
- color_pair_id = BaseScreen.instance.find_or_create_color_pair(
26
- foreground_color: @foreground_color,
27
- background_color: @background_color,
28
- ).id
29
- color_pair = Curses.color_pair(color_pair_id)
30
- win.setpos(@y, @x)
31
- win.attron(color_pair)
32
- win.addstr(@body)
33
- win.attroff(color_pair)
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
@@ -1,3 +1,3 @@
1
1
  module TermCanvas
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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", "~> 1.16"
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.1.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 00:00:00.000000000 Z
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: '1.16'
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: '1.16'
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