harlem_bits 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in harlem_bits.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ harlem_bits (0.0.1)
5
+ colored
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ colored (1.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ harlem_bits!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Diego Salazar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ BinaryHarlemShake
2
+ =================
3
+
4
+ This is a thing now, right? Right guys?
5
+
6
+ For the uninitiated:
7
+ http://www.reddit.com/r/harlemshake
8
+
9
+ To Run:
10
+ =======
11
+ ```bash
12
+ # download first, you need the mp3 file.
13
+ git clone git@github.com:DiegoSalazar/BinaryHarlemShake.git
14
+ cd BinaryHarlemShake
15
+ ruby harlem_bits.rb
16
+ ```
17
+
18
+ Dependencies:
19
+ ============
20
+ * The "colored" gem for colored terminal output.
21
+ * afplay (Mac only).
22
+
23
+ Todos:
24
+ =====
25
+ * Figure out how to work with frame rates and timings to make bits dance nicer.
26
+ * Figure out how to render to the terminal more efficiently. Currently just outputting screen after screen creating a huge scroll history, ncurses maybe?
27
+ * Control-C doesn't exit cleanly, currently must press twice.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/harlem_bits ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'harlem_bits'
4
+ HarlemBits.shake ARGV[0], ARGV[1]
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'harlem_bits/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "harlem_bits"
8
+ gem.version = HarlemBits::VERSION
9
+ gem.authors = ["Diego Salazar"]
10
+ gem.email = ["diego@greyrobot.com"]
11
+ gem.description = %q{It's the Harlem Shake. In Ruby. In the terminal.}
12
+ gem.summary = %q{Text based, binary, harlem shake. Yup.}
13
+ gem.homepage = "https://github.com/DiegoSalazar/BinaryHarlemShake"
14
+
15
+ gem.files = `git ls-files`.split($/) - ["HarlemShake.mp3"]
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.require_paths = ['lib']
18
+ gem.add_dependency 'colored'
19
+ end
@@ -0,0 +1,36 @@
1
+ module HarlemBits
2
+ class Arena
3
+ include CliRendering
4
+
5
+ def initialize(width, height)
6
+ @width, @height, @arena = width.to_i, height.to_i, []
7
+ @height.times do |h|
8
+ @arena << (1..@width).map do |i|
9
+ Bit.new rand_color
10
+ end
11
+ end
12
+ end
13
+
14
+ def center
15
+ @arena[@height/2][@width/2]
16
+ end
17
+
18
+ def clear
19
+ @arena.each { |row| row.each { |bit| bit.off } }
20
+ end
21
+
22
+ def random!
23
+ random.map { |bit| bit.switch }
24
+ end
25
+
26
+ def random
27
+ found = []
28
+ @arena.each do |row|
29
+ row.each do |bit|
30
+ found << bit if rand(@width) < @width/3
31
+ end
32
+ end
33
+ found
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ module HarlemBits
2
+ class Bit
3
+ def initialize(color, on = false)
4
+ @color, @on = color, on
5
+ end
6
+
7
+ def switch
8
+ @on = !@on
9
+ end
10
+
11
+ def off
12
+ @on = false
13
+ end
14
+
15
+ def to_s
16
+ @on ? '1'.send(@color) : '0'
17
+ end
18
+ alias_method :inspect, :to_s
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ module HarlemBits
2
+ module CliRendering
3
+ require 'colored'
4
+ COLORS = Colored.methods - [:extra, :colors, :colorize, :color] - Object.methods
5
+
6
+ def draw
7
+ STDOUT.puts with_enough_breaks_to_page_down
8
+ end
9
+
10
+ private
11
+
12
+ def rand_color
13
+ COLORS[rand(COLORS.size-1)]
14
+ end
15
+
16
+ def with_enough_breaks_to_page_down
17
+ "#{"\n" * 50}#{with_space_below}"
18
+ end
19
+
20
+ def with_space_below
21
+ "#{rows_with_newlines}#{"\n" * 5}"
22
+ end
23
+
24
+ def rows_with_newlines
25
+ @arena.map { |row| with_leading_space row }.join("\n")
26
+ end
27
+
28
+ def with_leading_space(row)
29
+ "#{' '*10}#{row.join(' ')}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module HarlemBits
2
+ class Harlem
3
+ def initialize(width, height)
4
+ @arena = Arena.new width, height
5
+ @alive = true
6
+ end
7
+
8
+ def kill
9
+ @alive = false
10
+ end
11
+
12
+ def start
13
+ start_t = Time.now.to_f
14
+ last_t = Time.now.to_i
15
+ frame = 0
16
+ switched = false
17
+
18
+ while @alive
19
+ cur_t = Time.now.to_f
20
+ delta = cur_t - start_t
21
+
22
+ @arena.clear
23
+ @arena.center.switch if delta % 2.0 == 0 || (delta % 2.0) / 2.0 > 0.3
24
+ @arena.random! if delta >= (DURATION / 2) + 1
25
+ @arena.draw
26
+
27
+ break if delta > DURATION
28
+ sleep 1/FRAME_RATE
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module HarlemBits
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'harlem_bits/bit'
2
+ require 'harlem_bits/cli_rendering'
3
+ require 'harlem_bits/arena'
4
+ require 'harlem_bits/harlem'
5
+ require 'harlem_bits/version'
6
+
7
+ module HarlemBits
8
+ DURATION = 30
9
+ FRAME_RATE = 30.0
10
+ AUDIO_FILE = 'resources/HarlemShake.mp3'
11
+
12
+ def self.shake(width, height)
13
+ t = Thread.new { system "afplay #{AUDIO_FILE}" }
14
+ s = Harlem.new width || 50, height || 25
15
+ s.start
16
+ # rescue Interrupt
17
+ # s.kill
18
+ # t.kill
19
+ # ensure
20
+ # puts 'fin'
21
+ # exit 0
22
+ end
23
+ end
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: harlem_bits
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Diego Salazar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colored
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: It's the Harlem Shake. In Ruby. In the terminal.
31
+ email:
32
+ - diego@greyrobot.com
33
+ executables:
34
+ - harlem_bits
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/harlem_bits
45
+ - harlem_bits.gemspec
46
+ - lib/harlem_bits.rb
47
+ - lib/harlem_bits/arena.rb
48
+ - lib/harlem_bits/bit.rb
49
+ - lib/harlem_bits/cli_rendering.rb
50
+ - lib/harlem_bits/harlem.rb
51
+ - lib/harlem_bits/version.rb
52
+ - pkg/harlem_bits-0.0.1.gem
53
+ - resources/HarlemShake.mp3
54
+ homepage: https://github.com/DiegoSalazar/BinaryHarlemShake
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.24
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Text based, binary, harlem shake. Yup.
78
+ test_files: []