Skip to content

Instantly share code, notes, and snippets.

@singularitti
Created December 28, 2023 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singularitti/776b479d9fb9789bb057b72dd7c2b00e to your computer and use it in GitHub Desktop.
Save singularitti/776b479d9fb9789bb057b72dd7c2b00e to your computer and use it in GitHub Desktop.
Print snowing scene in REPL #Julia
using REPL
# See https://discourse.julialang.org/t/let-it-snow/72950/15
function snow(io = stdout)
h, w = displaysize(io)
iob = IOBuffer()
ioc = IOContext(IOContext(iob, io), :displaysize=>(h,w))
print(io, repeat("\n", h), "\e[", h, "A\e[1G") # new lines and move back up
air = ones(Int, w, h)
flakes = [" ", "*", "❄︎", "❅", "❆"]
scsin(t) = ((sin(t) / 2) + 0.5) * (0.1 / 3)
likelihood(t) = scsin(t) + scsin(t * 1.00001) + scsin(t * 0.9999)
try
while true
for x in 1:w, y in h:-1:1
air[x,y] = if y == 1 # new flakes
rand() < likelihood(time()) ? rand(2:length(flakes)) : 1
elseif y == h # accumulate bottom
((rand() < 0.95 && air[x,y] > 1) || (air[x, y-1] > 1 && rand() < 0.2)) ? 2 : 1
elseif all(>(1), air[x, y:end]) # melt pile sometimes
rand() < 0.95 ? 2 : 1
elseif (air[x, y-1] > 1 && all(>(1), air[x, (y+1):end])) # if flake coming and piled up below
rand() < 0.1 ? 2 : 1
else # fall downwards otherwise
air[x, y-1]
end
end
foreach(space -> print(ioc, flakes[space]), air)
for itree in 1:12
print(ioc, "\e[H") # move back to home
if itree <= 9
print(ioc, "\e[$(h-14+itree)B\e[$(19-itree)G")
printstyled(ioc, "x"^(2*itree-1), color=:green)
else # Trunk
print(ioc, "\e[$(h-14+itree)B\e[18G")
printstyled(ioc, "-", color=:reverse)
end
end
print(ioc, "\e[H") # move back to home
@static VERSION > v"1.11.0-0" ? REPL.banner(ioc) : Base.banner(ioc)
printstyled(ioc, "julia> ", color = :green, bold = true)
println(ioc, "snow()")
print(io, String(take!(iob)))
sleep(1/8)
end
catch e
isa(e,InterruptException) || rethrow()
end
nothing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment