Skip to content

Instantly share code, notes, and snippets.

@matthewmcvickar
Last active December 20, 2022 22:59
Show Gist options
  • Save matthewmcvickar/5298965 to your computer and use it in GitHub Desktop.
Save matthewmcvickar/5298965 to your computer and use it in GitHub Desktop.
Get the current phase of the moon and use it to print an emoji moon phase image. OS X only, requires some shell knowledge. (Emoji moon phases will not show up here, but they will when you copy and paste or download the file.)
# Get the current phase of the moon for use as prompt
# Adapted from: http://wan.pengganas.net/entry/calculating-phase-of-moon-in-ruby
function moonphase() {
/usr/bin/env ruby <<-EORUBY
# Convert a date to Julian.
def julian(year, month, day)
a = (14-month)/12
y = year+4800-a
m = (12*a)-3+month
return day + (153*m+2)/5 + (365*y) + y/4 - y/100 + y/400 - 32045
end
Define the phases of the moon.
def phase(year, month, day)
p = (julian(year, month, day) - julian(2000, 1, 6)) % 29.530588853
if p < 1.84566 then return "πŸŒ‘" # new
elsif p < 5.53699 then return "πŸŒ’" # waxing crescent
elsif p < 9.22831 then return "πŸŒ“" # first quarter
elsif p < 12.91963 then return "πŸŒ”" # waxing gibbous
elsif p < 16.61096 then return "πŸŒ•" # full
elsif p < 20.30228 then return "πŸŒ–" # waning gibbous
elsif p < 23.99361 then return "πŸŒ—" # last quarter
elsif p < 27.68493 then return "🌘" # waning crescent
else return "πŸŒ‘" # new
end
end
# Get today's phase.
today = Time.new
print "#{phase(today.year, today.month, today.day)}"
EORUBY
}
# Set the prompt.
PS1="\$(moonphase) "

moon phase prompt

Note the two spaces after the emoji moon; it leaves a decent space between prompt and your command.

This is the simplest possible prompt; just a moon phase. You will probably want to incorporate this into something fancier. My .bash_prompt file has its own line for the current path, Git smarts, and more.

@rupertapplin
Copy link

Oh and to use - run ./moonPhase.sh yyyy mm dd

where moonPhase.sh is the name of your script!

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