conquer 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
+ SHA1:
3
+ metadata.gz: 314b6a02effda78924f95d5b22f905c058c7862b
4
+ data.tar.gz: 6048d32534ad57123c93480745198b922a4a2169
5
+ SHA512:
6
+ metadata.gz: 39fc369997f9a65ce1bb30bcb21c7ea7d5b4c9ef57933364d3e4be41744279f9bb23af2910af34dd3cac059b18bbd4bb99ea800f377a0b2da7db91defadf0570
7
+ data.tar.gz: 9c6f7e0f274dd9faa3e190a03398624507140e27db53ab43582cb4371b05b5d4417aa4d6d2f9c8e9e38dc97f44a51256eb1a6989b10e8eb23eb28054e87a6e1c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in conquer.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Conquer
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/conquer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'conquer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install conquer
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/conquer.
36
+
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 "conquer"
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
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/conquer.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'conquer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "conquer"
8
+ spec.version = Conquer::VERSION
9
+ spec.authors = ["Joakim Reinert"]
10
+ spec.email = ["mail@jreinert.com"]
11
+
12
+ spec.summary = "One bar to rule them all"
13
+ spec.description = "Conquer is a DSL for writing concurrent status bars"
14
+ spec.homepage = "https://github.com/jreinert/conquer"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+
25
+ spec.add_dependency 'celluloid', '~> 0.1'
26
+ end
data/example/bar.rb ADDED
@@ -0,0 +1,9 @@
1
+ Conquer.bar do
2
+ every(5.seconds) { Time.now }
3
+ separator
4
+ every(8.seconds) { %w(Hello World).sample }
5
+ separator
6
+ scroll do
7
+ block { `mpc -w current`.chomp }
8
+ end
9
+ end
data/exe/conquer ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort 'Usage: conquer bar.rb' if ARGV.empty?
4
+
5
+ require 'conquer'
6
+ require File.join(ENV['PWD'], ARGV[0])
@@ -0,0 +1,18 @@
1
+ require 'celluloid/current'
2
+
3
+ module Conquer
4
+ class Bar
5
+ include Celluloid
6
+ include Celluloid::Notifications
7
+
8
+ def initialize(topic, io)
9
+ @io = io
10
+ subscribe(topic, :puts)
11
+ end
12
+
13
+ def puts(_, content)
14
+ @io.puts(content)
15
+ @io.flush
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,49 @@
1
+ require 'celluloid/current'
2
+ require 'securerandom'
3
+
4
+ module Conquer
5
+ class Container
6
+ class Worker
7
+ include Celluloid
8
+ include Celluloid::Notifications
9
+
10
+ def initialize(topic, child_topics)
11
+ @topic = topic
12
+ @child_content = {}
13
+
14
+ child_topics.each do |child_topic|
15
+ @child_content[child_topic] = ''
16
+ subscribe(child_topic, :update_child_content)
17
+ end
18
+ end
19
+
20
+ def update_child_content(topic, content)
21
+ @child_content[topic] = content
22
+ publish(@topic, render(@child_content))
23
+ end
24
+
25
+ protected
26
+
27
+ def render(child_content)
28
+ child_content.values.join
29
+ end
30
+ end
31
+
32
+ attr_reader :children
33
+
34
+ def initialize(topic)
35
+ @topic = topic
36
+ @children = {}
37
+ end
38
+
39
+ def register(child_class, *args)
40
+ id = SecureRandom.uuid
41
+ @children[id] = child_class.new(id, *args)
42
+ end
43
+
44
+ def start_worker
45
+ Worker.supervise(args: [@topic, @children.keys])
46
+ @children.values.each(&:start_worker)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ class Numeric
2
+ def hours
3
+ self * 60.minutes
4
+ end
5
+
6
+ def minutes
7
+ self * 60.seconds
8
+ end
9
+
10
+ def seconds
11
+ self
12
+ end
13
+
14
+ def milliseconds
15
+ self.to_f / 1000
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'conquer/segment'
2
+ require 'conquer/scroller'
3
+
4
+ module Conquer
5
+ class DSL
6
+ def initialize(container)
7
+ @container = container
8
+ end
9
+
10
+ def separator(string = ' | ')
11
+ every(100.hours) { string }
12
+ end
13
+
14
+ def every(timespan, &block)
15
+ @container.register(Segment, timespan, block)
16
+ end
17
+
18
+ def block(&block)
19
+ every(0, &block)
20
+ end
21
+
22
+ def scroll(options = {}, &block)
23
+ scroller = @container.register(Scroller, options)
24
+ sub_dsl = DSL.new(scroller)
25
+ sub_dsl.instance_eval(&block)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ require 'conquer/container'
2
+
3
+ module Conquer
4
+ class Scroller < Container
5
+ class Worker < Container::Worker
6
+ def initialize(topic, child_topics, options)
7
+ super(topic, child_topics)
8
+ @options = options
9
+ @scroll_position = 0
10
+
11
+ async.run
12
+ end
13
+
14
+ def update_child_content(*)
15
+ @scroll_position = 0
16
+ super
17
+ end
18
+
19
+ protected
20
+
21
+ def render(child_content)
22
+ @content = super
23
+ visible_content = @content[@scroll_position, @options[:width]]
24
+ visible_content.ljust(@options[:width])
25
+ end
26
+
27
+ private
28
+
29
+ def run
30
+ loop do
31
+ publish(@topic, render(@child_content))
32
+ advance_scroll_position
33
+
34
+ sleep(1.0 / @options[:speed])
35
+ end
36
+ end
37
+
38
+ def advance_scroll_position
39
+ content_size = @content.size
40
+ scroll_position = @scroll_position + @options[:step]
41
+ margin = @options[:width] - @options[:step]
42
+ scroll_position = 0 if scroll_position > (content_size - margin)
43
+
44
+ @scroll_position = scroll_position
45
+ end
46
+ end
47
+
48
+ DEFAULT_OPTIONS = {
49
+ speed: 1,
50
+ width: 20,
51
+ every: 0,
52
+ step: 2
53
+ }.freeze
54
+
55
+ def initialize(topic, options)
56
+ super(topic)
57
+ @options = DEFAULT_OPTIONS.merge(options)
58
+ end
59
+
60
+ def start_worker
61
+ Worker.supervise(args: [@topic, @children.keys, @options])
62
+ @children.values.each(&:start_worker)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,49 @@
1
+ require 'celluloid/current'
2
+ require 'conquer/core_ext/numeric'
3
+
4
+ module Conquer
5
+ class Segment
6
+ class Worker
7
+ include Celluloid
8
+ include Celluloid::Notifications
9
+
10
+ DRIFT = 100.milliseconds.freeze
11
+
12
+ def initialize(topic, interval, block)
13
+ @topic = topic
14
+ @interval = interval
15
+ @block = wrapped_proc(block)
16
+
17
+ async.run
18
+ end
19
+
20
+ protected
21
+
22
+ def run
23
+ loop do
24
+ publish(@topic, @block.call.to_s.chomp)
25
+ sleep @interval + rand(0..DRIFT)
26
+ end
27
+ end
28
+
29
+ def render(child_content)
30
+ child_content.join
31
+ end
32
+
33
+ private
34
+
35
+ def wrapped_proc(block)
36
+ end
37
+ end
38
+
39
+ def initialize(topic, interval, block)
40
+ @topic = topic
41
+ @interval = interval
42
+ @block = block
43
+ end
44
+
45
+ def start_worker
46
+ Worker.supervise(args: [@topic, @interval, @block])
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Conquer
2
+ VERSION = "0.1.0"
3
+ end
data/lib/conquer.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'conquer/version'
2
+ require 'conquer/container'
3
+ require 'conquer/dsl'
4
+ require 'conquer/bar'
5
+
6
+ module Conquer
7
+ MAIN_TOPIC = 'conquer'.freeze
8
+
9
+ def self.bar(io = STDOUT, &block)
10
+ main_container = Container.new(MAIN_TOPIC)
11
+ dsl = DSL.new(main_container)
12
+ dsl.instance_eval(&block)
13
+
14
+ Bar.supervise(args: [MAIN_TOPIC, io])
15
+ main_container.start_worker
16
+
17
+ sleep
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conquer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Reinert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: celluloid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ description: Conquer is a DSL for writing concurrent status bars
70
+ email:
71
+ - mail@jreinert.com
72
+ executables:
73
+ - conquer
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - conquer.gemspec
86
+ - example/bar.rb
87
+ - exe/conquer
88
+ - lib/conquer.rb
89
+ - lib/conquer/bar.rb
90
+ - lib/conquer/container.rb
91
+ - lib/conquer/core_ext/numeric.rb
92
+ - lib/conquer/dsl.rb
93
+ - lib/conquer/scroller.rb
94
+ - lib/conquer/segment.rb
95
+ - lib/conquer/version.rb
96
+ homepage: https://github.com/jreinert/conquer
97
+ licenses: []
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: One bar to rule them all
119
+ test_files: []