Sha256: 91cdfe17667b0a5604f53ab10f0d4b7cd55d17eb82d04c520aae3e3049ded295

Contents?: true

Size: 1.35 KB

Versions: 29

Compression:

Stored size: 1.35 KB

Contents

#!/usr/bin/env ruby

require 'term/ansicolor'
require 'tins/go'
include Tins::GO

class SnowFlake
  include Term::ANSIColor
  extend Term::ANSIColor

  def initialize(x, y, shape: %w[ ❄ ❅ ❆ • • · · . . ])
     @x, @y, @shape = x, y, Array(shape).sample
     @shape.size != 1 and raise ArgumentError, "#@shape needs to be a character"
   end

   attr_accessor :x

   attr_accessor :y

   attr_accessor :shape

   def to_s
     move_to(y, x) { white on_black @shape }
   end
end

opts = go 'n:s:'
new_snowflakes  = (opts[?n] || 3).to_i
sleep_duration = (opts[?s] || 0.2).to_f

flakes = []
cycles = 0
wind   = 0.5

loop do
  print SnowFlake.hide_cursor, SnowFlake.on_black, SnowFlake.clear_screen

  at_exit do
    print SnowFlake.reset, SnowFlake.clear_screen, SnowFlake.move_home, SnowFlake.show_cursor
  end

  if cycles % (SnowFlake.terminal_lines / 3) == 0
    wind, cycles = rand, 0
  end
  cycles += 1

  flakes.reject! do |sf|
    if rand > wind
      sf.x -= 1
      if sf.x <= 0
        sf.x = SnowFlake.terminal_columns
      end
    else
      sf.x == 1
      if sf.x > SnowFlake.terminal_columns
        sf.x = 1
      end
    end
    sf.y += 1
    sf.y > SnowFlake.terminal_lines
  end

  new_snowflakes.times do
    flakes << SnowFlake.new(rand(1..SnowFlake.terminal_columns), 1)
  end

  print *flakes

  sleep sleep_duration
rescue Interrupt
  exit
end

Version data entries

29 entries across 20 versions & 2 rubygems

Version Path
tdiary-5.1.3 vendor/bundle/ruby/2.7.0/gems/term-ansicolor-1.7.1/bin/term_snow
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/term-ansicolor-1.7.1/bin/term_snow
tdiary-5.1.1 vendor/bundle/ruby/2.7.0/gems/term-ansicolor-1.7.1/bin/term_snow
tdiary-5.1.0 vendor/bundle/gems/term-ansicolor-1.7.1/bin/term_snow
tdiary-5.0.13 vendor/bundle/gems/term-ansicolor-1.7.1/bin/term_snow
tdiary-5.0.12.1 vendor/bundle/gems/term-ansicolor-1.7.1/bin/term_snow
term-ansicolor-1.7.1 bin/term_snow
tdiary-5.0.11 vendor/bundle/gems/term-ansicolor-1.7.0/bin/term_snow
term-ansicolor-1.7.0 bin/term_snow