nyan 0.0.3 → 0.0.4

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
@@ -3,3 +3,4 @@
3
3
  .rvmrc
4
4
  Gemfile.lock
5
5
  pkg/*
6
+ tmp/*
data/README.md CHANGED
@@ -5,6 +5,8 @@ Your favourite aging meme is reheated as an RSpec custom formatter.
5
5
 
6
6
  For entertainment purposes only.
7
7
 
8
+ ![NYAN?](https://github.com/kapoq/nyan/raw/master/docs/nyan_animated.gif)
9
+
8
10
  FAQ
9
11
  ---
10
12
 
@@ -35,6 +37,7 @@ Probably need a new graphics card or something.
35
37
  Requirements
36
38
  ------------
37
39
 
40
+ * Your terminal window should have a **black background** and be at least 180 columns x 50 rows.
38
41
  * RSpec 2 (tested against RSpec 2.6)
39
42
 
40
43
  Install
@@ -51,14 +54,15 @@ Alternatively install directly:
51
54
  Usage
52
55
  -----
53
56
 
57
+ Your terminal window should have a **black background** and be at
58
+ least 180 columns x 50 rows.
59
+
54
60
  $ rspec spec -f Nyan::Formatter
55
61
 
56
62
  Or if you want the joy of Nyan without running specs, use the binary:
57
63
 
58
64
  $ nyan
59
65
 
60
- Your terminal should be at least 180 columns x 50 rows.
61
-
62
66
  Development
63
67
  -----------
64
68
 
Binary file
@@ -2,6 +2,7 @@ require "nyan/version"
2
2
 
3
3
  require "nyan/colour"
4
4
  require "nyan/shape"
5
+ require "nyan/buffer"
5
6
  require "nyan/sprite"
6
7
  require "nyan/stage"
7
8
 
@@ -11,6 +12,7 @@ module Nyan
11
12
  BLOCK = "\xE2\x96\x88"
12
13
  SPACE = "\x20"
13
14
  NEWLINE = "\n"
15
+ NOTHING = ""
14
16
 
15
17
  require "nyan/sprites/rainbow"
16
18
  require "nyan/sprites/cat_head"
@@ -0,0 +1,18 @@
1
+ module Nyan
2
+ class Buffer < Array
3
+ def initialize(height, width, default)
4
+ @height = height
5
+ @width = width
6
+ @default = default
7
+ super(height) do
8
+ Array.new(width) do
9
+ default
10
+ end
11
+ end
12
+ end
13
+
14
+ def to_s
15
+ join
16
+ end
17
+ end
18
+ end
@@ -2,26 +2,36 @@ require 'ansi'
2
2
  include ANSI::Code
3
3
 
4
4
  module Nyan
5
- module Colour
5
+ module Colour
6
+ TWINKLY = "X"
7
+
6
8
  MAP = {
7
9
  "#" => "#{bright}#{black}",
8
10
  "b" => "#{dark}#{black}",
9
11
  "R" => "#{bright}#{magenta}",
10
12
  "p" => "#{dark}#{magenta}",
11
- "2" => "#{bright}#{yellow}",
12
13
  "`" => "#{dark}#{yellow}",
13
14
  "o" => "#{bright}#{white}",
14
15
  "_" => "#{bright}#{white}",
15
16
  "." => "#{dark}#{white}",
16
17
  "1" => "#{bright}#{red}",
18
+ "2" => "#{bright}#{yellow}",
17
19
  "3" => "#{bright}#{green}",
18
20
  "4" => "#{bright}#{blue}",
19
- "5" => "#{bright}#{magenta}"
21
+ "5" => "#{bright}#{magenta}",
20
22
  }
21
23
 
24
+ def self.twinkly?(char)
25
+ TWINKLY == char
26
+ end
27
+
22
28
  def self.colourize(char)
23
- escape_codes = MAP.fetch(char, reset)
24
- "#{escape_codes}#{Nyan::BLOCK}"
29
+ escape_codes = if twinkly?(char)
30
+ rand > 0.5 ? "#{bright}#{white}" : "#{bright}#{black}"
31
+ else
32
+ MAP.fetch(char, "")
33
+ end
34
+ "#{escape_codes}#{BLOCK}"
25
35
  end
26
36
  end
27
37
  end
@@ -1,10 +1,10 @@
1
1
  module Nyan
2
2
  class Flight
3
- def initialize
4
- @stage = Stage.new
5
-
3
+ def initialize(options = {})
4
+ @framerate = options.fetch(:framerate, 0.09)
5
+
6
+ @stage = Stage.new(options.fetch(:stage, {}))
6
7
  @rainbow = Sprite.new(:x => 0, :y => 17, :z => 1, :frames => Nyan::Rainbow.to_frames)
7
-
8
8
  @cat_head = Sprite.new(:x => Nyan::Rainbow::TOTAL_LENGTH - 2,
9
9
  :y => 17,
10
10
  :z => 100,
@@ -19,7 +19,6 @@ module Nyan
19
19
  when [:offset, :initial] then @x -= 2
20
20
  end
21
21
  end)
22
-
23
22
  @pop_tart = Sprite.new(:x => @cat_head.x - 20,
24
23
  :y => @cat_head.y - 2,
25
24
  :z => @cat_head.z - 1,
@@ -34,7 +33,7 @@ module Nyan
34
33
  def next!
35
34
  maybe_add_a_twinkly_star
36
35
  @stage.play!
37
- while Time.now - @tick_time < 0.09; end
36
+ while Time.now - @tick_time < @framerate; end
38
37
  @tick_time = Time.now
39
38
  end
40
39
 
@@ -75,7 +75,7 @@ X.........X
75
75
  EOF
76
76
 
77
77
  def self.to_frames
78
- @frames ||= SHAPE_LIST.gsub(/\./, SPACE).gsub(/X/, "@").split(/--./m)
78
+ @frames ||= SHAPE_LIST.gsub(/\./, SPACE).split(/--./m)
79
79
  end
80
80
  end
81
81
  end
@@ -1,9 +1,6 @@
1
1
  module Nyan
2
2
  class Stage
3
3
  include Shape
4
-
5
- SPACE = "\x20"
6
- NOTHING = ""
7
4
 
8
5
  attr_reader :output, :height, :width
9
6
 
@@ -35,7 +32,7 @@ module Nyan
35
32
 
36
33
  def render
37
34
  clear_buffer
38
- sprites.sort.each { |s| write_to_buffer(s) }
35
+ sprites.sort.reverse.each { |s| write_to_buffer(s) }
39
36
  print_buffer
40
37
  end
41
38
 
@@ -70,40 +67,46 @@ module Nyan
70
67
  private
71
68
 
72
69
  def clear_buffer
73
- @buffer = Array.new(height) do
74
- Array.new(width) do
75
- { :char => SPACE, :colourized_char => SPACE }
76
- end
77
- end
70
+ @buffer = Buffer.new(height, width, SPACE)
78
71
  end
79
72
 
80
73
  def write_to_buffer(sprite)
81
- sprite.lines.each_with_index do |line, i|
82
- line_y = sprite.y + i
83
- previous_colourized_char = nil
84
- if line_y.between?(top, bottom)
85
- line.chomp.chars.each_with_index do |char, j|
86
- char_x = sprite.x + j
87
- if char_x.between?(left, right) && char != Nyan::SPACE
88
- colourized_char = Colour.colourize(char)
89
- colourized_char = Nyan::BLOCK if colourized_char == previous_colourized_char
90
- @buffer[line_y][char_x] = { :char => char, :colourized_char => colourized_char }
91
- previous_colourized_char = colourized_char
92
- end
74
+ sprite.lines.each_with_index do |row, i|
75
+ @last_char_with_colour = nil
76
+ y = sprite.y + i
77
+ next unless y.between?(top, bottom)
78
+ row.chomp.chars.each_with_index do |char, j|
79
+ x = sprite.x + j
80
+ next unless x.between?(left, right)
81
+ if char_writeable?(char) && buffer_writeable_at_point?(x, y)
82
+ @buffer[y][x] = coloured? && new_colour_or_continue(char) || char
83
+ else
84
+ @last_char_with_colour = nil
93
85
  end
94
86
  end
95
87
  end
96
88
  end
97
89
 
98
- def print_buffer
99
- key = coloured? ? :colourized_char : :char
100
- @buffer.each_with_index do |row, i|
101
- output.print move(i, 0)
102
- output.print clear_line
103
- output.print reset
104
- output.print row.map { |h| h[key] }.join
105
- output.print "\n" unless i == bottom
90
+ def char_writeable?(char)
91
+ SPACE != char
92
+ end
93
+
94
+ def buffer_writeable_at_point?(x, y)
95
+ SPACE == @buffer[y][x]
96
+ end
97
+
98
+ def new_colour_or_continue(char)
99
+ coloured_char = Colour.colourize(char)
100
+ if coloured_char == @last_char_with_colour
101
+ BLOCK
102
+ else
103
+ @last_char_with_colour = coloured_char
106
104
  end
107
105
  end
106
+
107
+ def print_buffer
108
+ output.print move(top, left)
109
+ output.print @buffer.to_s
110
+ end
108
111
  end
109
112
  end
@@ -1,3 +1,3 @@
1
1
  module Nyan
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.description = "Your favourite aging meme is reheated as an RSpec custom formatter. For entertainment purposes only. WARNING: enterprise-ready"
13
13
 
14
14
  s.add_dependency "ansi"
15
- s.add_dependency "rspec"
15
+ s.add_dependency "rspec", "~> 2"
16
16
 
17
17
  s.add_development_dependency "guard-rspec"
18
18
  s.add_development_dependency "rake"
@@ -2,14 +2,39 @@ require 'spec_helper'
2
2
 
3
3
  describe Nyan::Colour do
4
4
  describe ".colourize" do
5
- it "looks up the char in the map" do
6
- Nyan::Colour::MAP.should_receive(:fetch).with("X", anything)
7
- Nyan::Colour.colourize("X")
5
+ context "when the char is not twinkly" do
6
+ before do
7
+ Nyan::Colour.stub(:twinkly? => false)
8
+ end
9
+
10
+ it "looks up the char in the map" do
11
+ Nyan::Colour::MAP.should_receive(:fetch).with("X", anything)
12
+ Nyan::Colour.colourize("X")
13
+ end
14
+
15
+ it "returns the escape code for the colour followed by a block char" do
16
+ Nyan::Colour::MAP.stub(:fetch => "__ESC__")
17
+ Nyan::Colour.colourize("X").should eql "__ESC__#{Nyan::BLOCK}"
18
+ end
8
19
  end
9
-
10
- it "returns the escape code for the colour followed by a block char" do
11
- Nyan::Colour::MAP.stub(:fetch => "__ESC__")
12
- Nyan::Colour.colourize("X").should eql "__ESC__#{Nyan::BLOCK}"
20
+
21
+ context "when the char is not twinkly" do
22
+ before do
23
+ Nyan::Colour.stub(:twinkly? => true)
24
+ end
25
+
26
+ it "does not look up the char in the map" do
27
+ Nyan::Colour::MAP.should_not_receive(:fetch)
28
+ Nyan::Colour.colourize("X")
29
+ end
30
+
31
+ it "returns a different escape code depending on the value of a random number" do
32
+ Nyan::Colour.should_receive(:rand).and_return(0)
33
+ r1 = Nyan::Colour.colourize("X")
34
+ Nyan::Colour.should_receive(:rand).and_return(1)
35
+ r2 = Nyan::Colour.colourize("X")
36
+ r1.should_not eq r2
37
+ end
13
38
  end
14
39
  end
15
40
  end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ nyan_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(nyan_dir) unless $LOAD_PATH.include?(nyan_dir)
7
+ require "nyan"
8
+
9
+ require 'benchmark'
10
+ require 'stringio'
11
+
12
+ s = StringIO.new
13
+ real_stdout = $stdout
14
+ $stdout = s
15
+
16
+ colour_flight = Nyan::Flight.new(:framerate => 0, :stage => { :output => real_stdout })
17
+ bw_flight = Nyan::Flight.new(:framerate => 0, :stage => { :output => real_stdout, :coloured => false })
18
+
19
+ COUNT = 50
20
+ Benchmark.bmbm do |b|
21
+ b.report("Colour:") {
22
+ COUNT.times { colour_flight.next! }
23
+ }
24
+ b.report("B&W:") {
25
+ COUNT.times { bw_flight.next! }
26
+ }
27
+ end
28
+
29
+ $stdout = real_stdout
30
+ print cls
31
+ puts s.string
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-08 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2011-10-21 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: ansi
17
- requirement: &10787560 !ruby/object:Gem::Requirement
16
+ requirement: &12071560 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,21 +21,21 @@ dependencies:
22
21
  version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *10787560
24
+ version_requirements: *12071560
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: rspec
28
- requirement: &10787140 !ruby/object:Gem::Requirement
27
+ requirement: &12071020 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
- - - ! '>='
30
+ - - ~>
32
31
  - !ruby/object:Gem::Version
33
- version: '0'
32
+ version: '2'
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *10787140
35
+ version_requirements: *12071020
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: guard-rspec
39
- requirement: &10786720 !ruby/object:Gem::Requirement
38
+ requirement: &12070600 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: '0'
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *10786720
46
+ version_requirements: *12070600
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: rake
50
- requirement: &10835020 !ruby/object:Gem::Requirement
49
+ requirement: &12070120 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ! '>='
@@ -55,10 +54,10 @@ dependencies:
55
54
  version: '0'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *10835020
57
+ version_requirements: *12070120
59
58
  - !ruby/object:Gem::Dependency
60
59
  name: rb-inotify
61
- requirement: &10834560 !ruby/object:Gem::Requirement
60
+ requirement: &12069540 !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements:
64
63
  - - ! '>='
@@ -66,10 +65,10 @@ dependencies:
66
65
  version: '0'
67
66
  type: :development
68
67
  prerelease: false
69
- version_requirements: *10834560
68
+ version_requirements: *12069540
70
69
  - !ruby/object:Gem::Dependency
71
70
  name: libnotify
72
- requirement: &10834140 !ruby/object:Gem::Requirement
71
+ requirement: &12068840 !ruby/object:Gem::Requirement
73
72
  none: false
74
73
  requirements:
75
74
  - - ! '>='
@@ -77,7 +76,7 @@ dependencies:
77
76
  version: '0'
78
77
  type: :development
79
78
  prerelease: false
80
- version_requirements: *10834140
79
+ version_requirements: *12068840
81
80
  description: ! 'Your favourite aging meme is reheated as an RSpec custom formatter.
82
81
  For entertainment purposes only. WARNING: enterprise-ready'
83
82
  email:
@@ -94,7 +93,9 @@ files:
94
93
  - README.md
95
94
  - Rakefile
96
95
  - bin/nyan
96
+ - docs/nyan_animated.gif
97
97
  - lib/nyan.rb
98
+ - lib/nyan/buffer.rb
98
99
  - lib/nyan/colour.rb
99
100
  - lib/nyan/flight.rb
100
101
  - lib/nyan/formatter.rb
@@ -114,7 +115,7 @@ files:
114
115
  - spec/lib/sprite_spec.rb
115
116
  - spec/lib/stage_spec.rb
116
117
  - spec/spec_helper.rb
117
- has_rdoc: true
118
+ - test/benchmarking.rb
118
119
  homepage: https://github.com/kapoq/nyan
119
120
  licenses: []
120
121
  post_install_message:
@@ -135,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
136
  version: '0'
136
137
  requirements: []
137
138
  rubyforge_project:
138
- rubygems_version: 1.6.2
139
+ rubygems_version: 1.8.10
139
140
  signing_key:
140
141
  specification_version: 3
141
142
  summary: Nyan Cat flies across your terminal while your RSpec specs run!