harlem_bits 0.0.2 → 0.0.3

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 CHANGED
@@ -1,2 +1,4 @@
1
1
  .DS_Store
2
- harlem_bits-*.gem
2
+ harlem_bits-*.gem
3
+ Gemfile.lock
4
+ pkg/
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  BinaryHarlemShake
2
2
  =================
3
3
 
4
+ ![Have you seen this harlem shake thing?](http://i.imgur.com/5CKyDK3.png)
5
+
4
6
  This is a thing now, right? Right guys?
5
7
 
6
8
  For the uninitiated:
data/harlem_bits.gemspec CHANGED
@@ -16,4 +16,5 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.require_paths = ['lib']
18
18
  gem.add_dependency 'colored'
19
+ gem.add_dependency 'os'
19
20
  end
data/harlem_bits.rb ADDED
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "os"
4
+
5
+ class Bit
6
+ def initialize(color, on = false)
7
+ @color, @on = color, on
8
+ end
9
+
10
+ def switch
11
+ @on = !@on
12
+ end
13
+
14
+ def off
15
+ @on = false
16
+ end
17
+
18
+ def to_s
19
+ @on ? '1'.send(@color) : '0'
20
+ end
21
+ alias_method :inspect, :to_s
22
+ end
23
+
24
+ module CliRendering
25
+ require 'colored'
26
+ COLORS = Colored.methods - [:extra, :colors, :colorize, :color] - Object.methods
27
+
28
+ def draw
29
+ puts with_enough_breaks_to_page_down
30
+ end
31
+
32
+ private
33
+
34
+ def rand_color
35
+ COLORS[rand(COLORS.size-1)]
36
+ end
37
+
38
+ def with_enough_breaks_to_page_down
39
+ "#{"\n" * 50}#{with_space_below}"
40
+ end
41
+
42
+ def with_space_below
43
+ "#{rows_with_newlines}#{"\n" * 5}"
44
+ end
45
+
46
+ def rows_with_newlines
47
+ @arena.map { |row| with_leading_space row }.join("\n")
48
+ end
49
+
50
+ def with_leading_space(row)
51
+ "#{' '*10}#{row.join(' ')}"
52
+ end
53
+ end
54
+
55
+ class Arena
56
+ include CliRendering
57
+
58
+ def initialize(width, height)
59
+ @width, @height, @arena = width.to_i, height.to_i, []
60
+ @height.times do |h|
61
+ @arena << (1..@width).map do |i|
62
+ Bit.new rand_color
63
+ end
64
+ end
65
+ end
66
+
67
+ def center
68
+ @arena[@height/2][@width/2]
69
+ end
70
+
71
+ def clear
72
+ @arena.each { |row| row.each { |bit| bit.off } }
73
+ end
74
+
75
+ def random!
76
+ random.map { |bit| bit.switch }
77
+ end
78
+
79
+ def random
80
+ found = []
81
+ @arena.each do |row|
82
+ row.each do |bit|
83
+ found << bit if rand(@width) < @width/3
84
+ end
85
+ end
86
+ found
87
+ end
88
+ end
89
+
90
+ class Harlem
91
+ DURATION = 30
92
+ FRAME_RATE = 30.0
93
+ AUDIO_FILE = 'HarlemShake.mp3'
94
+
95
+ def self.shake(width, height)
96
+ if OS.osx?
97
+ t = Thread.new { system "afplay #{AUDIO_FILE}" }
98
+ elsif OS.linux?
99
+ t = Thread.new { system "mpg123 #{AUDIO_FILE}" }
100
+ else
101
+ #Playing songs from windows command line? Can it be done? Has science gone too far?
102
+ t = Thread.new { puts "Pretend that there's music, pal." }
103
+ end
104
+ s = new width || 50, height || 25
105
+ s.start
106
+ rescue Interrupt
107
+ s.kill
108
+ t.kill
109
+ ensure
110
+ puts 'fin'
111
+ exit 0
112
+ end
113
+
114
+ def initialize(width, height)
115
+ @arena = Arena.new width, height
116
+ @alive = true
117
+ end
118
+
119
+ def kill
120
+ @alive = false
121
+ end
122
+
123
+ def start
124
+ start_t = Time.now.to_f
125
+ last_t = Time.now.to_i
126
+ frame = 0
127
+ switched = false
128
+
129
+ while @alive
130
+ cur_t = Time.now.to_f
131
+ delta = cur_t - start_t
132
+
133
+ @arena.clear
134
+ @arena.center.switch if delta % 2.0 == 0 || (delta % 2.0) / 2.0 > 0.3
135
+ @arena.random! if delta >= (DURATION / 2) + 1
136
+ @arena.draw
137
+
138
+ break if delta > DURATION
139
+ sleep 1/FRAME_RATE
140
+ end
141
+ end
142
+ end
143
+
144
+ Harlem.shake ARGV[0], ARGV[1] if $0 == __FILE__
@@ -1,3 +1,3 @@
1
1
  module HarlemBits
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/harlem_bits.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'os'
1
2
  require 'harlem_bits/bit'
2
3
  require 'harlem_bits/cli_rendering'
3
4
  require 'harlem_bits/arena'
@@ -10,7 +11,14 @@ module HarlemBits
10
11
  AUDIO_FILE = 'resources/HarlemShake.mp3'
11
12
 
12
13
  def self.shake(width, height)
13
- t = Thread.new { system "afplay #{AUDIO_FILE}" }
14
+ if OS.osx?
15
+ t = Thread.new { system "afplay #{AUDIO_FILE}" }
16
+ elsif OS.linux?
17
+ t = Thread.new { system "mpg123 #{AUDIO_FILE}" }
18
+ else
19
+ # Playing songs from windows command line? Can it be done? Has science gone too far?
20
+ t = Thread.new { puts "Pretend that there's music, pal." }
21
+ end
14
22
  s = Harlem.new width || 50, height || 25
15
23
  s.start
16
24
  rescue Interrupt
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harlem_bits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: os
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  description: It's the Harlem Shake. In Ruby. In the terminal.
31
47
  email:
32
48
  - diego@greyrobot.com
@@ -37,19 +53,18 @@ extra_rdoc_files: []
37
53
  files:
38
54
  - .gitignore
39
55
  - Gemfile
40
- - Gemfile.lock
41
56
  - LICENSE.txt
42
57
  - README.md
43
58
  - Rakefile
44
59
  - bin/harlem_bits
45
60
  - harlem_bits.gemspec
61
+ - harlem_bits.rb
46
62
  - lib/harlem_bits.rb
47
63
  - lib/harlem_bits/arena.rb
48
64
  - lib/harlem_bits/bit.rb
49
65
  - lib/harlem_bits/cli_rendering.rb
50
66
  - lib/harlem_bits/harlem.rb
51
67
  - lib/harlem_bits/version.rb
52
- - pkg/harlem_bits-0.0.1.gem
53
68
  - resources/HarlemShake.mp3
54
69
  homepage: https://github.com/DiegoSalazar/BinaryHarlemShake
55
70
  licenses: []
data/Gemfile.lock DELETED
@@ -1,16 +0,0 @@
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!
Binary file