Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={};puts "\033[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"};$stdout.flush;sleep 0.1}'


Saving that alias as I love things done in ruby, really nice. If anyone else was curious about a breakdown on some of the parts.

ruby -e:

This command tells the system to execute the following Ruby code.

C=`stty size`.scan(/\d+/)[1].to_i This part gets the size of the terminal window. stty size returns the dimensions of the terminal (rows and columns) .scan(/\d+/) extracts the numbers from this output, and[1] gets the second number, which represents the number of columns and then to_i converts this number to an integer. The result is stored in the variable C.

S=["2743".to_i(16)].pack("U*") This line creates the snowflake character. "2743".to_i(16) converts the hexadecimal number 2743 (which represents the Unicode code point for a snowflake) into an integer, and .pack("U*") converts this integer into a UTF-8 character.

a={} Initializes an empty hash a. This hash will keep track of the position of each snowflake.

puts "\033[2J" This ANSI escape code clears the terminal screen.

loop{...} An infinite loop to keep the animation going.

Inside the loop.

a[rand(C)]=0 Randomly places a new snowflake in the top row at a random column.

a.each{|x,o|;...} Iterates over each snowflake.

Inside the iteration.

a[x]+=1 Moves the snowflake down one row. print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H" This part uses ANSI escape codes to move the cursor and draw the snowflakes. It moves the cursor to the old position of the snowflake, prints a space (to erase the old snowflake), then moves to the new position and prints the snowflake character. $stdout.flush Flushes the standard output buffer, ensuring that all output is displayed immediately. sleep 0.1 Pauses the loop for a short time (0.1 seconds) to control the speed of the animation.


Any reason for "S=["2743".to_i(16)].pack("U*")" instead of S="\u2743"?


This is amazing.


awesome!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: