Skip to content

Instantly share code, notes, and snippets.

@gustafe
Created April 1, 2020 09:57
Show Gist options
  • Save gustafe/3696d6c694e9b8bfdda3dd688a6c3b45 to your computer and use it in GitHub Desktop.
Save gustafe/3696d6c694e9b8bfdda3dd688a6c3b45 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -I /home/gustaf/perl5-OLD/lib/perl5/
use strict;
use warnings;
use Astro::MoonPhase;
binmode( STDOUT, ":utf8" );
sub moon_info {
my ($epoch) = @_;
my ( $MoonPhase, $MoonIllum, $MoonAge, $MoonDist,
$MoonAng, $SunDist, $SunAng ) = phase($epoch);
return [ $MoonPhase, $MoonIllum, $MoonAge ];
}
sub phase_name {
my ( $phase, $illum ) = @_;
my $string = '';
if ( $phase < 0.25 ) { $string = 'WaxC' }
elsif ( $phase >= 0.25 and $phase < 0.5 ) { $string = 'WaxG' }
elsif ( $phase >= 0.5 and $phase < 0.75 ) { $string = 'WanG' }
elsif ( $phase >= 0.75 ) { $string = 'WanC' }
if ( $illum <= 0.01 ) { return 'NewM' }
if ( $illum >= 0.99 ) { return 'Full' }
if ( abs( $illum - 0.5 ) <= 0.01 and $string =~ m/Wax/ ) { return "1Q" }
if ( abs( $illum - 0.5 ) <= 0.01 and $string =~ m/Wan/ ) { return "3Q" }
return $string;
}
sub phase_emoji {
# https://gist.github.com/ddrscott/5339084
# phases = πŸŒ‘ πŸŒ’ πŸŒ“ πŸŒ” πŸŒ• πŸŒ– πŸŒ— 🌘
# U+1F311 πŸŒ‘ NEW MOON SYMBOL
# U+1f312 πŸŒ’ WAXING CRESCENT MOON SYMBOL
# U+1f313 πŸŒ“ FIRST QUARTER MOON SYMBOL
# U+1f314 πŸŒ” WAXING GIBBOUS MOON SYMBOL
# U+1f315 πŸŒ• FULL MOON SYMBOL
# U+1f316 πŸŒ– WANING GIBBOUS MOON SYMBOL
# U+1f317 πŸŒ— LAST QUARTER MOON SYMBOL
# U+1f318 🌘 WANING CRESCENT MOON SYMBOL
my ( $phase, $illum ) = @_;
my $string = '';
my $curr_phase;
if ( $phase < 0.25 ) {
$string = "\x{1f312}"; # Waxing crescent - πŸŒ’
$curr_phase = 'Wax';
}
elsif ( $phase >= 0.25 and $phase < 0.5 ) {
$string = "\x{1f314}"; # Waxing gibbous - πŸŒ”
$curr_phase = 'Wax';
}
elsif ( $phase >= 0.5 and $phase < 0.75 ) {
$string = "\x{1f316}"; # Waning gibbous - πŸŒ–
$curr_phase = 'Wan';
}
elsif ( $phase >= 0.75 ) {
$string = "\x{1f318}"; # Waning crescent - 🌘
$curr_phase = 'Wan';
}
if ( $illum <= 0.01 ) {
$string = "\x{1f311}"; # new moon - πŸŒ‘
$curr_phase='Wax';
}
if ( $illum >= 0.99 ) {
$string= "\x{1f315}"; # full moon - πŸŒ•
$curr_phase = 'Wan';
}
if ( abs( $illum - 0.5 ) <= 0.01 and $curr_phase =~ m/Wax/ ) {
$string = "\x{1f313}"; # first quarter - πŸŒ“
}
if ( abs( $illum - 0.5 ) <= 0.01 and $curr_phase =~ m/Wan/ ) {
$string = "\x{1f317}"; # 3rd quarter - πŸŒ—
}
return $string;
}
my $now = shift || time;
my $info = moon_info($now);
#my $name = phase_name($info->[0],$info->[1]);
my $name = phase_emoji( $info->[0], $info->[1] );
my $illum = $info->[1];
my $age = $info->[2];
printf( "%s %.0f%% %.2f\n", $name, $illum * 100, $age );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment