Sha256: 390b6fa25e675b700e9030e3cc7230ce0d562f584e1848efa989cde4ca791bd2

Contents?: true

Size: 1.35 KB

Versions: 9

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

9 entries across 9 versions & 1 rubygems

Version Path
term-ansicolor-1.11.2 bin/term_snow
term-ansicolor-1.11.1 bin/term_snow
term-ansicolor-1.11.0 bin/term_snow
term-ansicolor-1.10.4 bin/term_snow
term-ansicolor-1.10.3 bin/term_snow
term-ansicolor-1.10.2 bin/term_snow
term-ansicolor-1.10.1 bin/term_snow
term-ansicolor-1.10.0 bin/term_snow
term-ansicolor-1.9.0 bin/term_snow