term_canvas 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b5d343b9c662b9a2613d77e82f2cd905103e78416715e6c26044f5605169a847
4
+ data.tar.gz: 1feb22194cfa0a56d7ef10d6e01b270976cfba79e89711a52049e1b8460766d0
5
+ SHA512:
6
+ metadata.gz: 052d7fac4a035d0257c196ed36b637dec5ff03e39dcde2dc60096a32b34cfe0bb82fa6fab79c14f036f5670b817ce0eb56d19997bd25f9ec00821f5fbaedbd27
7
+ data.tar.gz: 7821d72cfa812494e3a098f0ad14453cf3b610ea53dee76d74a6a4c254e300ef45fa74937f9fe898a65d4199048500c8cfc34d47cc7df02301d4e9569d025b0a
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /vendor/bundle
14
+
15
+ log*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.0
7
+ before_install: gem install bundler -v 1.16.4
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ term_canvas (0.1.0)
5
+ curses
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ curses (1.2.7)
11
+ diff-lcs (1.3)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ term_canvas!
35
+
36
+ BUNDLED WITH
37
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 kthatoto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # TermCanvas
2
+ Wrapper library for `curses` of ruby.
3
+ You can handle colors easily.
4
+
5
+ ## Installation
6
+
7
+ $ gem install term_canvas
8
+
9
+ ## Usage
10
+
11
+ ## Development
12
+
13
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
14
+
15
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
16
+
17
+ ## Contributing
18
+
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/term_canvas.
20
+
21
+ ## License
22
+
23
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "term_canvas"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/update ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ gem specific_install -l 'kthatoto/term_canvas'
@@ -0,0 +1,23 @@
1
+ require 'curses'
2
+
3
+ Curses.init_screen
4
+ Curses::noecho
5
+ Curses.curs_set(0)
6
+ Curses.start_color
7
+ Curses.use_default_colors
8
+
9
+ win = Curses::Window.new(10, 10, 10, 10)
10
+
11
+ color_struct = Struct.new(:id, :r, :g, :b)
12
+ id = 1
13
+ color = color_struct.new(id, 300, 900, 300)
14
+ Curses.init_color(*color)
15
+ Curses.init_pair(id, 0, id)
16
+ pair = Curses.color_pair(id)
17
+ win.attron(pair)
18
+ win.addstr("123")
19
+ win.attroff(pair)
20
+ win.refresh
21
+ sleep 3
22
+
23
+ Curses.close_screen
@@ -0,0 +1,34 @@
1
+ require 'curses'
2
+ Curses.init_screen
3
+ Curses.stdscr.nodelay = 1
4
+ Curses::noecho
5
+ Curses.curs_set(0)
6
+ Curses.start_color
7
+ Curses.use_default_colors
8
+ 255.times do |i|
9
+ Curses.init_pair(i, 0, i)
10
+ end
11
+ win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
12
+ loop do
13
+ key = Curses.getch
14
+ case key
15
+ when ?q
16
+ break
17
+ end
18
+ l = 30
19
+ 255.times do |i|
20
+ win.setpos(i / l, (i % l) * 3)
21
+ win.attron(Curses.color_pair(i))
22
+ win.addstr(format("%3d", i))
23
+ win.attroff(Curses.color_pair(i))
24
+ end
25
+ 255.times do |i|
26
+ win.setpos(i / l + 10, (i % l) * 3)
27
+ win.attron(Curses.color_pair(i + 256))
28
+ win.addstr(format("%3d", i))
29
+ win.attroff(Curses.color_pair(i + 256))
30
+ end
31
+ win.refresh
32
+ sleep 0.5
33
+ end
34
+ Curses.close_screen
data/examples/test.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'term_canvas'
2
+
3
+ field = TermCanvas.new(x: 0, y: 0, w: 50, h: 30)
4
+
5
+ text_pos = {y: 0, x: 0}
6
+ loop do
7
+ key = TermCanvas.gets
8
+ case key
9
+ when ?k
10
+ text_pos[:y] -= 1
11
+ when ?j
12
+ text_pos[:y] += 1
13
+ when ?h
14
+ text_pos[:x] -= 2
15
+ when ?l
16
+ text_pos[:x] += 2
17
+ when ?q
18
+ break
19
+ end
20
+
21
+ field.clear
22
+ field.rect(
23
+ Rect.new(
24
+ x: 0, y: 0, width: 10, height: 10, background_color: {r: 200, b: 200, g: 800}
25
+ )
26
+ )
27
+ field.text(
28
+ Text.new(
29
+ x: text_pos[:x], y: text_pos[:y], body: "test",
30
+ background_color: {r: 800, g: 800, b: 800}, foreground_color: {r: 200, g: 200, b: 200}
31
+ )
32
+ )
33
+ field.update
34
+ sleep 0.05
35
+ end
36
+ TermCanvas.close
@@ -0,0 +1,78 @@
1
+ require 'singleton'
2
+ require 'curses'
3
+ class BaseScreen
4
+ include Singleton
5
+
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
19
+
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
40
+ end
41
+ end
42
+ return response_color_pair || create_color_pair(fc_id, bc_id)
43
+ end
44
+
45
+ private
46
+
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
54
+ end
55
+ end
56
+ return response_color || create_color(r: color.r, g: color.g, b: color.b)
57
+ end
58
+
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
66
+
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
74
+
75
+ def same_color?(color1, color2)
76
+ return color1.r == color2.r && color1.g == color2.g && color1.b == color2.b
77
+ end
78
+ end
@@ -0,0 +1,8 @@
1
+ class Object::TermCanvas
2
+ attr_reader :index
3
+
4
+ # @param index [Integer]
5
+ def set_index(index)
6
+ @object_index = index
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ class Rect < Object::TermCanvas
2
+ attr_reader :background_color
3
+
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
16
+
17
+ def foreground_color
18
+ nil
19
+ end
20
+
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)
32
+ end
33
+ win.attroff(color_pair)
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ class Text < Object::TermCanvas
2
+ attr_reader :foreground_color, :background_color
3
+
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
22
+
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)
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module TermCanvas
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,89 @@
1
+ require 'curses'
2
+ require 'term_canvas/base_screen'
3
+ require 'term_canvas/object'
4
+ require 'term_canvas/text'
5
+ require 'term_canvas/rect'
6
+ class TermCanvas
7
+ attr_accessor :width, :height
8
+ # Create a convenient window.
9
+ # @param x [Integer] Horizontal position of the window. From left to right.
10
+ # @param y [Integer] Vertical position of the window. From top to bottom.
11
+ # @param w [Integer] Width of the window.
12
+ # @param h [Integer] Height of the window.
13
+ def initialize(x:, y:, w:, h:)
14
+ BaseScreen.instance
15
+ @win = Curses::Window.new(h, w, y, x)
16
+ @x = x
17
+ @y = y
18
+ @width = w
19
+ @height = h
20
+ @objects = []
21
+ @object_index = 0
22
+ end
23
+
24
+ class << self
25
+ # Get key input.
26
+ # @return [String] Inputted key.
27
+ # @return [nil]
28
+ def gets
29
+ Curses.getch
30
+ end
31
+
32
+ # Close.
33
+ def close
34
+ Curses.close_screen
35
+ end
36
+ end
37
+
38
+ # Clear objects and remove from the window.
39
+ def clear
40
+ @win.clear
41
+ @objects = []
42
+ @object_index = 0
43
+ end
44
+
45
+ # @return [Integer] Horizontal center of the window.
46
+ def centerx
47
+ @win.width / 2 + @win.width % 2
48
+ end
49
+
50
+ # @return [Integer] Vertical center of the window.
51
+ def centery
52
+ @win.height / 2 + @win.height % 2
53
+ end
54
+
55
+ # Add text object to the window but not display.
56
+ # @param object [Text] Text instance
57
+ def text(object)
58
+ raise 'The argument must be Text' if !(Text === object)
59
+ object.set_index(@object_index)
60
+ @objects << object
61
+ @object_index += 1
62
+ end
63
+
64
+ # Add rect object to the window but not display.
65
+ def rect(object)
66
+ raise 'The argument must be Rect' if !(Rect === object)
67
+ object.set_index(@object_index)
68
+ @objects << object
69
+ @object_index += 1
70
+ end
71
+
72
+ # Display objects that are added into this instance.
73
+ def update
74
+ draw
75
+ @win.refresh
76
+ end
77
+
78
+ def close
79
+ @win.close
80
+ end
81
+
82
+ private
83
+
84
+ def draw
85
+ @objects.each do |object|
86
+ object.draw(@win)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "term_canvas/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "term_canvas"
7
+ spec.version = TermCanvas::VERSION
8
+ spec.authors = ["kthatoto"]
9
+ spec.email = ["kthatoto@gmail.com"]
10
+
11
+ spec.summary = %q{Wrapper library for curses of ruby}
12
+ spec.description = %q{Wrapper library for curses of ruby. You can handle colors easily.}
13
+ spec.homepage = "https://github.com/kthatoto/term_canvas"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "curses"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: term_canvas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kthatoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Wrapper library for curses of ruby. You can handle colors easily.
70
+ email:
71
+ - kthatoto@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - bin/update
87
+ - examples/curses.rb
88
+ - examples/simplified_chart.rb
89
+ - examples/test.rb
90
+ - lib/term_canvas.rb
91
+ - lib/term_canvas/base_screen.rb
92
+ - lib/term_canvas/object.rb
93
+ - lib/term_canvas/rect.rb
94
+ - lib/term_canvas/text.rb
95
+ - lib/term_canvas/version.rb
96
+ - term_canvas.gemspec
97
+ homepage: https://github.com/kthatoto/term_canvas
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.7.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Wrapper library for curses of ruby
121
+ test_files: []