Skip to content

Instantly share code, notes, and snippets.

@matthewmcvickar
Last active December 20, 2022 22:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.

@matthewmcvickar
Copy link
Author

@ablwr
Copy link

ablwr commented Nov 1, 2015

Line 14 of the above script is not commented out, which will result in an error when copy-pasting into a .bash_prompt or elsewhere in the shell. Otherwise lookin' good and thanks! 🌝

@rupertapplin
Copy link

Hi, This one should work for you - it's changed a little - now printing the name of the phase rather than the icon, used a bit of bc to do the math and also running it as a ksh script (not sure if that is important!)

!/bin/ksh

Convert a date to Julian.

function convert_to_julian {

let year=$1
let month=$2
let day=$3

let a=$year/100
let b=$a/4
let c=2-$a+$b
let e=365.25_($year+4716)
let e=$(echo "$e/1" |bc)
let f=30.6001_($month+1)
let f=$(echo "$f/1" |bc)
let converted_date=$c+$day+$e+$f-1524.5

}

Define the phases of the moon.

function get_moon_phase {
let year=$1
let month=$2
let day=$3
convert_to_julian $year $month $day
today=$converted_date

convert_to_julian 2000 1 6
converter=$converted_date

echo today is $today
echo converter is $converter

let phase_number=$today-$converter
let phase_number=$(echo "$phase_number%29.530588853" |bc)

phase_number=$(($(($today - $converter))/29))

echo the phase number is $phase_number

if [ $phase_number -lt "1.84566" ]; then phase_icon="new"
elif [ $phase_number -lt "5.53699" ]; then phase_icon="waxing crescent"
elif [ $phase_number -lt "9.22831" ]; then phase_icon="first quarter"
elif [ $phase_number -lt "12.91963" ]; then phase_icon="waxing gibbous"
elif [ $phase_number -lt "16.61096" ]; then phase_icon="full"
elif [ $phase_number -lt "20.30228" ]; then phase_icon="waning gibbous"
elif [ $phase_number -lt "23.99361" ]; then phase_icon="last quarter"
elif [ $phase_number -lt "27.68493" ]; then phase_icon="waning crescent"
else phase_icon="new"
fi
}

now the main line

let year=$1
let month=$2
let day=$3

get_moon_phase $year $month $day
echo $phase_icon

@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