Skip to content

Instantly share code, notes, and snippets.

@sontek
Last active August 9, 2024 13:14
Show Gist options
  • Save sontek/1505483 to your computer and use it in GitHub Desktop.
Save sontek/1505483 to your computer and use it in GitHub Desktop.
Make your terminal snow
#!/bin/bash
LINES=$(tput lines)
COLUMNS=$(tput cols)
declare -A snowflakes
declare -A lastflakes
clear
function move_flake() {
i="$1"
if [ "${snowflakes[$i]}" = "" ] || [ "${snowflakes[$i]}" = "$LINES" ]; then
snowflakes[$i]=0
else
if [ "${lastflakes[$i]}" != "" ]; then
printf "\033[%s;%sH \033[1;1H " ${lastflakes[$i]} $i
fi
fi
printf "\033[%s;%sH\u274$[($RANDOM%6)+3]\033[1;1H" ${snowflakes[$i]} $i
lastflakes[$i]=${snowflakes[$i]}
snowflakes[$i]=$((${snowflakes[$i]}+1))
}
while :
do
i=$(($RANDOM % $COLUMNS))
move_flake $i
for x in "${!lastflakes[@]}"
do
move_flake "$x"
done
sleep 0.1
done
@sontek
Copy link
Author

sontek commented Dec 21, 2011

If your terminal font supports it you can replace * at line 22 with ❄ to make it more festive

@amstan
Copy link

amstan commented Dec 21, 2011

Can you make it appear on top of something that's already running?

@x37v
Copy link

x37v commented Dec 21, 2011

awesome!

@allaun
Copy link

allaun commented Dec 21, 2011

I named the script as snowjob, and this is the output?
snowjob: 11: Syntax error: "(" unexpected

@supercheetah
Copy link

Like it!

@sontek
Copy link
Author

sontek commented Dec 21, 2011

@allaun What shell are you using? You can replace function move_flake() with function move_flake (removing the parens) and it'll work for you

@EmbeddedLinuxGuy
Copy link

Nice. Also you can rotate different flake glyphs if you replace * with \u274"$[($RANDOM%6)+3]"

@sontek
Copy link
Author

sontek commented Dec 22, 2011

@EmbeddedLinuxGuy That is awesome, thanks!

@skihero
Copy link

skihero commented Dec 22, 2011

cool!

@blottis
Copy link

blottis commented Nov 19, 2015

Thanks for the script it is very cool, but could you provides us with some additional info about how to make it more functional ... for example how to make the snow appear on a functional terminal screen as @amstan stated in his comment. I am new with bash scripting and want to use your code to decorate a script I am creating but I can not deduce where to put my code... thanks

@ChrisCoe
Copy link

@amstan @blottis
I've also been trying to get the snow effect as a background on my running terminal. I don't think this is possible without some major refactoring in the bash script as the print out to stdout would collide with normal terminal function use.

In case you are like me and found this post years later, many modern terminals now have built in functionality for effects. I've been using Windows Terminal to get the desired snow effect I want.

image

@sontek
Copy link
Author

sontek commented Jan 31, 2021

Yeah, the only way to get the effect during usage is built-in features of the terminal like shaders and stuff like what you did for the windows terminal. This is a generic / cross platform way of getting effects that doesn't depend on the shell at all

@heyarviind
Copy link

snowflakes are not coming for me

Screenshot 2023-12-15 at 5 58 26 PM

@alaz-aura
Copy link

Screenshot 2023-12-15 at 11 32 41 pm

hmmm unclear what the expected effect is meant to be 🤔

@sandippingle
Copy link

Same issue

@kevindurb
Copy link

Yea new revision doesnt work for me either but old revision works great

@sontek
Copy link
Author

sontek commented Dec 15, 2023

Wow, interesting that others are finding this today. I've done a lot of work on this over the years and its currently packaged over here:

https://github.com/sontek/snowmachine

@kevindurb
Copy link

I think people are finding it through a python implementation that got to hackernews front page https://news.ycombinator.com/item?id=38652339

@kevindurb
Copy link

Oh thats also yours hahaha @sontek

@TheCycoONE
Copy link

For people on MacOS that the new version isn't working on, it's due to the bash version shipping with macOS being too old to support unicode escape.

If you have a newer version of bash installed in homebrew you can change the bang at the top e.g. #!/opt/homebrew/bin/bash (or use the author's python version)

@pwhitdog
Copy link

snowflakes are not coming for me
Screenshot 2023-12-15 at 5 58 26 PM

You cut too much in your snowflake paste. Change line 22 to printf "\033[%s;%sH❄\033[1;1H" ${snowflakes[$i]} $i

@cosmicqbit
Copy link

This is what I get on MacOS iTerm2. Is this supposed to be it! I was expecting SNOW flakes xD.

image

@cosmicqbit
Copy link

With ❄

image

@alaz-aura
Copy link

Screenshot 2023-12-17 at 9 49 22 pm

Thanks the updates everyone, snow has been fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment