circule 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9449a7b91abcb3058201ee66d2c44925ab3cadd958421731a0269de135556e09
4
+ data.tar.gz: f6bc4ede2a8ef059e4cea8e9a02a40203c157b59dd4ba2a4e3e573627b8c1b0b
5
+ SHA512:
6
+ metadata.gz: 8ae5266a7208c0b6dfacc7e6dcacf0f35b04aa57a0c80ce445e5c529d8eb329c4547de66ab56830f511116b9ba27299ed09d5c0e2f861d73248fa1ccb1b7cf6d
7
+ data.tar.gz: 4672cce80a11799eeccc8ef1d29a93fe4d4a144cf4aaeea21a520fe7bb7ff7627b94c5feef3edad7269adfbfa8e087af5bde82c1d5e356485283e155dd4452f6
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-10-29
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in circule.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'minitest', '~> 5.0'
11
+
12
+ gem 'rubocop', '~> 1.7'
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ circule (0.1.0)
5
+ chunky_png
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ chunky_png (1.4.0)
12
+ minitest (5.14.4)
13
+ parallel (1.21.0)
14
+ parser (3.0.2.0)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.0.0)
17
+ rake (13.0.6)
18
+ regexp_parser (2.1.1)
19
+ rexml (3.2.5)
20
+ rubocop (1.22.3)
21
+ parallel (~> 1.10)
22
+ parser (>= 3.0.0.0)
23
+ rainbow (>= 2.2.2, < 4.0)
24
+ regexp_parser (>= 1.8, < 3.0)
25
+ rexml
26
+ rubocop-ast (>= 1.12.0, < 2.0)
27
+ ruby-progressbar (~> 1.7)
28
+ unicode-display_width (>= 1.4.0, < 3.0)
29
+ rubocop-ast (1.12.0)
30
+ parser (>= 3.0.1.1)
31
+ ruby-progressbar (1.11.0)
32
+ unicode-display_width (2.1.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ circule!
39
+ minitest (~> 5.0)
40
+ rake (~> 13.0)
41
+ rubocop (~> 1.7)
42
+
43
+ BUNDLED WITH
44
+ 2.2.27
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Frank J. Cameron
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,50 @@
1
+ # Circule
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you
4
+ need to be able to package up your Ruby library into a gem. Put your
5
+ Ruby code in the file `lib/circule`. To experiment with that code, run
6
+ `bin/console` for an interactive prompt.
7
+
8
+ TODO: Delete this and the text above, and describe your gem
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'circule'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install circule
25
+
26
+ ## Usage
27
+
28
+ TODO: Write usage instructions here
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install
33
+ dependencies. Then, run `rake test` to run the tests. You can also run
34
+ `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake
37
+ install`. To release a new version, update the version number in
38
+ `version.rb`, and then run `bundle exec rake release`, which will create
39
+ a git tag for the version, push git commits and the created tag, and
40
+ push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at
45
+ https://gitlab.com/fjc/circule.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT
50
+ License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'circule'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require 'pry'
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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/circule.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/circule/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'circule'
7
+ spec.version = Circule::VERSION
8
+ spec.authors = ['Frank J. Cameron']
9
+ spec.email = ['fjc@fastmail.net']
10
+
11
+ spec.summary = ''
12
+ spec.homepage = 'https://gitlab.com/fjc/circule'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.4.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'chunky_png'
31
+ end
data/exe/circule ADDED
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'circule'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.banner = ''
9
+
10
+ opts.on('-h HEXSTRING', '--hex', 'Use this hash') do |h|
11
+ options[:hex] = h
12
+ end
13
+
14
+ opts.on('-b INTEGER', '--btc-block', 'Use hash from this bitcoin block') do |b|
15
+ options[:btc_block] = b
16
+ end
17
+
18
+ opts.on('-0 INTEGER', '--bit0', 'Starting bit') do |b|
19
+ options[:bit0] = b
20
+ end
21
+
22
+ opts.on('-s INTEGER', '--step', 'Step bit') do |s|
23
+ options[:step] = s
24
+ end
25
+
26
+ opts.on('-n INTEGER', '--bitn', 'Starting bit') do |b|
27
+ options[:bitn] = b
28
+ end
29
+
30
+ opts.on('-c INTEGER', '--canvas', 'Canvas size') do |c|
31
+ options[:canvas] = c
32
+ end
33
+
34
+ opts.on('-x INTEGER', '--ox', 'Offset x') do |x|
35
+ options[:ox] = x
36
+ end
37
+
38
+ opts.on('-y INTEGER', '--oy', 'Offset y') do |y|
39
+ options[:oy] = y
40
+ end
41
+
42
+ opts.on('-z INTEGER', '--zoom', 'Zoom in GUI') do |z|
43
+ options[:zoom] = z
44
+ end
45
+
46
+ opts.on('-f FILENAME', '--file', 'Use hash from file') do |f|
47
+ options[:file] = f
48
+ end
49
+
50
+ opts.on('-s FILENAME', '--save', 'Save image to file') do |s|
51
+ options[:save] = s
52
+ end
53
+
54
+ opts.on('-g', '--gui', 'Run GUI') do |g|
55
+ options[:gui] = g
56
+ end
57
+
58
+ opts.on('-i', '--interactive', 'Run GUI with pry') do |i|
59
+ options[:interactive] = i
60
+ end
61
+ end.parse!
62
+
63
+ if options[:gui]
64
+ require 'circule/gui'
65
+ gui = Circule::GUI.new
66
+
67
+ gui.hex = options[:hex] if options[:hex]
68
+ gui.bit0 = options[:bit0] if options[:bit0]
69
+ gui.step = options[:step] if options[:step]
70
+ gui.bitn = options[:bitn] if options[:bitn]
71
+ gui.canvas = options[:canvas] if options[:canvas]
72
+ gui.ox = options[:ox] if options[:ox]
73
+ gui.oy = options[:oy] if options[:oy]
74
+ gui.zoom = options[:zoom] if options[:zoom]
75
+
76
+ gui.open options[:file] if options[:file]
77
+
78
+ if options[:save]
79
+ gui.create_gui
80
+ gui.generate_image
81
+ gui.save options[:save]
82
+ end
83
+
84
+ if options[:interactive]
85
+ require 'pry'
86
+ Thread.new do gui.pry ensure `reset` end
87
+ end
88
+
89
+ gui.launch
90
+ else
91
+ case options[:file]
92
+ when nil
93
+ when '-'
94
+ require 'digest'
95
+ options[:hex] = Digest::SHA256.hexdigest STDIN.read
96
+ else
97
+ require 'digest'
98
+ options[:hex] = Digest::SHA256.hexdigest File.read(options[:file])
99
+ end
100
+
101
+ Circule.new(
102
+ **{
103
+ hex: options[:hex],
104
+ btc_block: options[:btc_block],
105
+ bit0: options[:bit0],
106
+ step: options[:step],
107
+ bitn: options[:bitn],
108
+ canvas: options[:canvas],
109
+ ox: options[:ox],
110
+ oy: options[:oy],
111
+ }.compact
112
+ ).tap do |circule|
113
+ case options[:save]
114
+ when nil
115
+ when '-'
116
+ circule.image.write STDOUT
117
+ else
118
+ circule.image.save options[:save]
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,115 @@
1
+ require_relative '../circule'
2
+
3
+ require 'digest'
4
+ require 'tempfile'
5
+
6
+ begin
7
+ require 'glimmer-dsl-tk'
8
+ require 'glimmer/data_binding/observer'
9
+ rescue LoadError
10
+ warn "Could not find gem 'glimmer-dsl-tk' in locally installed gems."
11
+ end
12
+
13
+ class Circule::GUI
14
+ include Glimmer
15
+
16
+ attr_accessor :hex, :bit0, :step, :bitn, :canvas, :ox, :oy, :scale
17
+
18
+ def initialize
19
+ initialize_defaults
20
+ end
21
+
22
+ def initialize_defaults
23
+ @hex = ARGV[0] || SecureRandom.hex(32)
24
+ @bit0 = 0
25
+ @step = 16
26
+ @bitn = 255
27
+ @canvas = 24
28
+ @ox = 0
29
+ @oy = 0
30
+ @scale = 1
31
+ end
32
+
33
+ def observe_image_attribute_changes
34
+ observer = Glimmer::DataBinding::Observer.proc { generate_image }
35
+ observer.observe(self, :hex)
36
+ observer.observe(self, :bit0)
37
+ observer.observe(self, :step)
38
+ observer.observe(self, :bitn)
39
+ observer.observe(self, :canvas)
40
+ observer.observe(self, :ox)
41
+ observer.observe(self, :oy)
42
+ observer.observe(self, :scale)
43
+ end
44
+
45
+ def generate_image
46
+ tfile = Tempfile.new(%w[circule- .png])
47
+
48
+ image = Circule.new(
49
+ hex: @hex,
50
+ bit0: @bit0.to_i,
51
+ step: @step.to_i,
52
+ bitn: @bitn.to_i,
53
+ canvas: @canvas.to_i,
54
+ ox: @ox.to_i,
55
+ oy: @oy.to_i,
56
+ ).image
57
+
58
+ width = image.width * @scale.to_i
59
+ height = image.height * @scale.to_i
60
+ data = image.resize(width, height).save(tfile.path)
61
+
62
+ @image_label.image = tfile.path
63
+ end
64
+
65
+ def create_gui
66
+ @root ||= root {
67
+ title 'Circule GUI'
68
+
69
+ frame {
70
+ entry { text <=> [self, :hex] }
71
+
72
+ label { text 'bit0:' }
73
+ spinbox { from 0; to 255; text <=> [self, :bit0] }
74
+ label { text 'step:' }
75
+ spinbox { from 4; to 64; text <=> [self, :step] }
76
+ label { text 'bitn:' }
77
+ spinbox { from 0; to 255; text <=> [self, :bitn] }
78
+ label { text 'canvas:' }
79
+ spinbox { from 24; to 96; text <=> [self, :canvas] }
80
+ label { text 'ox:' }
81
+ spinbox { from -96; to 96; text <=> [self, :ox] }
82
+ label { text 'oy:' }
83
+ spinbox { from -96; to 96; text <=> [self, :oy] }
84
+ label { text 'scale:' }
85
+ spinbox { from 1; to 24; text <=> [self, :scale] }
86
+
87
+ frame {
88
+ @image_label = label { anchor 'center' }
89
+ }
90
+
91
+ button { text 'Open'; command { open } }
92
+ button { text 'Save'; command { save } }
93
+ button { text 'Exit'; command { exit(0) } }
94
+ }
95
+ }
96
+ end
97
+
98
+ def open(source = nil)
99
+ source ||= send(:get_open_file, parent: root)
100
+ self.hex = Digest::SHA256.hexdigest File.read(source)
101
+ end
102
+
103
+ def save(target = nil)
104
+ source = @image_label.image
105
+ target ||= send(:get_save_file, parent: @root)
106
+ source.write(target)
107
+ end
108
+
109
+ def launch
110
+ observe_image_attribute_changes
111
+ create_gui
112
+ generate_image
113
+ @root.open
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Circule
4
+ VERSION = '0.1.0'
5
+ end
data/lib/circule.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'circule/version'
4
+
5
+ require 'uri'
6
+ require 'net/http'
7
+ require 'chunky_png'
8
+ require 'securerandom'
9
+
10
+ class Circule
11
+ class Error < StandardError; end
12
+
13
+ def initialize(
14
+ hex: nil, btc_block: nil,
15
+ bit0: 0, step: 16, bitn: 255,
16
+ canvas: 24, ox: 0, oy: ox
17
+ ) @_int = hex.to_i(16) if hex
18
+ @btc_block = btc_block
19
+ @bit0 = bit0
20
+ @step = step
21
+ @bitn = bitn
22
+ @canvas = canvas
23
+ @ox = ox
24
+ @oy = oy
25
+ end
26
+
27
+ def int
28
+ @_int ||=
29
+ if @btc_block
30
+ Net::HTTP
31
+ .get_response(
32
+ URI("https://live.blockcypher.com/btc/block/#{@btc_block}/"))
33
+ .header['location']
34
+ .split('/')[-1]
35
+ else
36
+ SecureRandom.hex(32)
37
+ end
38
+ .to_i(16)
39
+ end
40
+
41
+ def slices
42
+ @_slices ||=
43
+ @bit0
44
+ .step(@bitn, @step)
45
+ .map { |n| int[n, 16] }
46
+ end
47
+
48
+ def circles
49
+ @_circles ||=
50
+ slices
51
+ .map { |s| [ s[0, 5], s[5, 5], s[10, 6] ] }
52
+ end
53
+
54
+ def image
55
+ @_image ||=
56
+ ChunkyPNG::Image
57
+ .new(@canvas, @canvas)
58
+ .tap do |img|
59
+ @canvas.times do |a|
60
+ @canvas.times do |b|
61
+ img[a, b] = ChunkyPNG::Color.html_color(
62
+ ChunkyPNG::Color::PREDEFINED_COLORS.keys[
63
+ circles
64
+ .select { |x, y, r| (a+@ox-x)**2 + (b+@oy-y)**2 < r**2 }
65
+ .size
66
+ ]
67
+ )
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: circule
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frank J. Cameron
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chunky_png
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
+ description:
28
+ email:
29
+ - fjc@fastmail.net
30
+ executables:
31
+ - circule
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - circule.gemspec
45
+ - exe/circule
46
+ - lib/circule.rb
47
+ - lib/circule/gui.rb
48
+ - lib/circule/version.rb
49
+ homepage: https://gitlab.com/fjc/circule
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://gitlab.com/fjc/circule
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.4.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: ''
73
+ test_files: []