barrb 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1ba3603bc99698616d742d18e43a6b83cd98793
4
+ data.tar.gz: f820952c56ca90546ef380c4d19326e3d3640eac
5
+ SHA512:
6
+ metadata.gz: 94fa82841523cc86752475928d338c2426692a60a41bac30eae7d8d4e64d9efc7458f029f3e4b3e4658eec0db0431ffee18b664219b75236606b65f9768c40d8
7
+ data.tar.gz: 83dadeb8d32caba8fda23824984f86b087575dd54bef78351621da585c27010be7bad5d2b108c28bf05febfe86a8ef21afdda3a02459bc74fd9186dc836a743f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in barrb.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 jreinert
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Barrb
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/barrb`. 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 'barrb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install barrb
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 `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` to 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
+ 1. Fork it ( https://github.com/[my-github-username]/barrb/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'barrb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'barrb'
8
+ spec.version = Barrb::VERSION
9
+ spec.authors = ['Joakim Reinert']
10
+ spec.email = ['mail+barrb@jreinert.com']
11
+
12
+ spec.summary = 'A DSL for writing status bars'
13
+ spec.description = 'To be used with dzen2 and such'
14
+ spec.homepage = 'https://github.com/jreinert/barrb'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ fail 'RubyGems >=2.0 is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(/^(test|spec|features)\//)
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.9'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_dependency 'activesupport', '4.2'
35
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "barrb"
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
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'barrb'
4
+
5
+ Barrb::Writer.new('dzen2 -ta r') do
6
+ segment interval: 1.minute do
7
+ Time.now.strftime('%H:%M')
8
+ end
9
+
10
+ insert ' | '
11
+
12
+ segment interval: 5.seconds do
13
+ Time.now.strftime('%H:%M:%S')
14
+ end
15
+
16
+ insert ' | '
17
+
18
+ scroll_segment interval: :once, width: 20 do
19
+ 'A very long string that needs some scrolling'
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require 'barrb/version'
2
+ require 'barrb/writer'
3
+
4
+ module Barrb
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'barrb/segment'
2
+
3
+ module Barrb
4
+ class ScrollSegment < Segment
5
+ protected
6
+
7
+ def process_loop
8
+ started = Time.now
9
+ output = loop_output
10
+ @scroll_position = 0 if output != @last_output
11
+ while @options[:interval] == :once ||
12
+ Time.now - started < @options[:interval]
13
+ scroll(output)
14
+ end
15
+ @options[:interval]
16
+ end
17
+
18
+ def scroll(output)
19
+ padded_output = output + ' ' * @options[:scroll_gap]
20
+ notify_change(scrolled_view(padded_output))
21
+ if output.length > @options[:width]
22
+ new_position = @scroll_position + @options[:scroll_step]
23
+ @scroll_position = new_position % padded_output.length
24
+ end
25
+ sleep(0.5)#@options[:scroll_speed])
26
+ end
27
+
28
+ def scrolled_view(output)
29
+ end_index = @scroll_position + @options[:width] - 1
30
+ (output + output)[@scroll_position..end_index]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,70 @@
1
+ module Barrb
2
+ class Segment
3
+ attr_writer :on_change
4
+
5
+ def initialize(options, &block)
6
+ @options = options
7
+ output_loop(&block) if block_given?
8
+ end
9
+
10
+ def on_change(&block)
11
+ @on_change = block
12
+ end
13
+
14
+ def output_loop(&block)
15
+ @output_loop = block
16
+ end
17
+
18
+ def start
19
+ @loop_thread = Thread.new do
20
+ catch(:stop) { run }
21
+ end
22
+ end
23
+
24
+ def stop
25
+ @should_stop = true
26
+ end
27
+
28
+ def join
29
+ @loop_thread.join
30
+ end
31
+
32
+ protected
33
+
34
+ def should_stop?
35
+ @should_stop
36
+ end
37
+
38
+ def loop_output
39
+ @output_loop.call
40
+ end
41
+
42
+ def notify_change(output)
43
+ @on_change.call(output)
44
+ @last_output = output
45
+ end
46
+
47
+ def time
48
+ started = Time.now
49
+ yield
50
+ Time.now - started
51
+ end
52
+
53
+ def process_loop
54
+ time do
55
+ output = loop_output
56
+ notify_change(output) if @last_output != output
57
+ end
58
+ end
59
+
60
+ def run
61
+ loop do
62
+ break if should_stop?
63
+ execution_time = time { process_loop }
64
+ break if @options[:interval] == :once
65
+ next if execution_time >= @options[:interval]
66
+ sleep(@options[:interval] - execution_time)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Barrb
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,70 @@
1
+ require 'barrb/segment'
2
+ require 'barrb/scroll_segment'
3
+ require 'active_support/core_ext/numeric/time'
4
+ require 'active_support/core_ext/hash/reverse_merge'
5
+
6
+ module Barrb
7
+ class Writer
8
+ attr_accessor :default_interval
9
+
10
+ def initialize(command, &block)
11
+ @segments = []
12
+ @output = []
13
+ @mutex = Mutex.new
14
+ instance_eval(&block)
15
+ @pipe = IO.popen(command, 'w+')
16
+ @segments.each(&:start)
17
+ @segments.each(&:join)
18
+ end
19
+
20
+ def segment(options = {}, &block)
21
+ register_segment(
22
+ Segment.new(options.reverse_merge(default_segment_options), &block)
23
+ )
24
+ end
25
+
26
+ def scroll_segment(options = {}, &block)
27
+ register_segment(
28
+ ScrollSegment.new(
29
+ options.reverse_merge(default_scroll_segment_options), &block
30
+ )
31
+ )
32
+ end
33
+
34
+ def insert(string)
35
+ segment(interval: :once) { string }
36
+ end
37
+
38
+ protected
39
+
40
+ def register_segment(segment)
41
+ index = @segments.size
42
+ segment.on_change do |output|
43
+ update_output(index, output)
44
+ end
45
+ @segments << segment
46
+ end
47
+
48
+ def update_output(segment_index, output)
49
+ @mutex.synchronize do
50
+ @output[segment_index] = output
51
+ @pipe.puts @output.join
52
+ end
53
+ end
54
+
55
+ def default_segment_options
56
+ {
57
+ interval: default_interval || 1
58
+ }
59
+ end
60
+
61
+ def default_scroll_segment_options
62
+ default_segment_options.merge(
63
+ scroll_speed: 1,
64
+ scroll_step: 1,
65
+ width: 40,
66
+ scroll_gap: 4
67
+ )
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barrb
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: 2015-04-24 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ description: To be used with dzen2 and such
70
+ email:
71
+ - mail+barrb@jreinert.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.md
81
+ - README.md
82
+ - Rakefile
83
+ - barrb.gemspec
84
+ - bin/console
85
+ - bin/sample.rb
86
+ - bin/setup
87
+ - lib/barrb.rb
88
+ - lib/barrb/scroll_segment.rb
89
+ - lib/barrb/segment.rb
90
+ - lib/barrb/version.rb
91
+ - lib/barrb/writer.rb
92
+ homepage: https://github.com/jreinert/barrb
93
+ licenses: []
94
+ metadata:
95
+ allowed_push_host: https://rubygems.org
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A DSL for writing status bars
116
+ test_files: []