Debian Codegraph @

bin/term_snow

raw symbols SCIP
1 #!/usr/bin/env ruby
2
3 require 'term/ansicolor'
4 require 'tins/go'
5 include Tins::GO
6
7 class SnowFlake
8 include Term::ANSIColor
9 extend Term::ANSIColor
10
11 def initialize(x, y, shape: %w[ ❄ ❅ ❆ • • · · . . ])
12 @x, @y, @shape = x, y, Array(shape).sample
13 @shape.size != 1 and raise ArgumentError, "#@shape needs to be a character"
14 end
15
16 attr_accessor :x
17
18 attr_accessor :y
19
20 attr_accessor :shape
21
22 def to_s
23 move_to(y, x) { white on_black @shape }
24 end
25 end
26
27 opts = go 'n:s:'
28 new_snowflakes = (opts[?n] || 3).to_i
29 sleep_duration = (opts[?s] || 0.2).to_f
30
31 flakes = []
32 cycles = 0
33 wind = 0.5
34
35 loop do
36 print SnowFlake.hide_cursor, SnowFlake.on_black, SnowFlake.clear_screen
37
38 at_exit do
39 print SnowFlake.reset, SnowFlake.clear_screen, SnowFlake.move_home, SnowFlake.show_cursor
40 end
41
42 if cycles % (SnowFlake.terminal_lines / 3) == 0
43 wind, cycles = rand, 0
44 end
45 cycles += 1
46
47 flakes.reject! do |sf|
48 if rand > wind
49 sf.x -= 1
50 if sf.x <= 0
51 sf.x = SnowFlake.terminal_columns
52 end
53 else
54 sf.x += 1
55 if sf.x > SnowFlake.terminal_columns
56 sf.x = 1
57 end
58 end
59 sf.y += 1
60 sf.y > SnowFlake.terminal_lines
61 end
62
63 new_snowflakes.times do
64 flakes << SnowFlake.new(rand(1..SnowFlake.terminal_columns), 1)
65 end
66
67 print(*flakes)
68
69 sleep sleep_duration
70 rescue Interrupt
71 exit
72 end