Class: NyanCat::NyanCat
- Inherits:
-
Object
- Object
- NyanCat::NyanCat
- Defined in:
- lib/nyancat.rb
Instance Method Summary collapse
-
#initialize(io, options) ⇒ NyanCat
constructor
A new instance of NyanCat.
- #reset ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(io, options) ⇒ NyanCat
Returns a new instance of NyanCat.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nyancat.rb', line 45 def initialize(io, ) @running = false @io = io @term_width = [:width] || 80 @term_height = [:height] || 24 @mute = [:mute] || false @hide_time = [:hide_time] || false flavour = [:flavour] || 'original' frames = YAML.load_file(File.("../nyancat/#{flavour}/frames.yml", __FILE__)) palette = YAML.load_file(File.("../nyancat/#{flavour}/palette.yml", __FILE__)) @audio = File.("../nyancat/#{flavour}/audio.mp3", __FILE__) # Calculate the width in terms of the output char term_width = @term_width / OUTPUT_CHAR.length term_height = @term_height min_row = 0 max_row = frames[0].length min_col = 0 max_col = frames[0][0].length min_row = (max_row - term_height) / 2 if max_row > term_height max_row = min_row + term_height if max_row > term_height min_col = (max_col - term_width) / 2 if max_col > term_width max_col = min_col + term_width if max_col > term_width # Calculate the final animation width @anim_width = (max_col - min_col) * OUTPUT_CHAR.length # Precompute frames @frames = frames.map do |frame| frame[min_row...max_row].map do |line| line.chars.to_a[min_col...max_col].map do |c| "\033[48;5;%dm%s" % [palette[c], OUTPUT_CHAR] end.join + "\033[m\n" end end end |
Instance Method Details
#reset ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/nyancat.rb', line 131 def reset() begin @io.puts("\033[0m\033c") rescue Errno::EPIPE => e # We failed to reset the TERM, IO stream is gone end end |
#run ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/nyancat.rb', line 88 def run() @running = true begin # Initialise term @io.printf("\033[H\033[2J\033[?25l") # Get start time start_time = Time.now() # Play audio audio_t = Thread.new { IO.popen("mpg123 -loop 0 -q #{@audio} > /dev/null 2>&1") } unless @mute || nil while @running @frames.each do |frame| # Print the next frame @io.puts frame # Print the time so far unless @hide_time time = "You have nyaned for %0.0f seconds!" % [Time.now() - start_time] time = time.center(@anim_width) @io.printf("\033[1;37;17m%s", time) end # Reset the frame and sleep @io.printf("\033[H") sleep(0.09) end end ensure # Ensure the audio thread is killed, if it exists audio_t.kill unless audio_t.nil? stop reset end end |
#stop ⇒ Object
127 128 129 |
# File 'lib/nyancat.rb', line 127 def stop() @running = false end |