Last active
May 5, 2022 12:50
-
-
Save isoraqathedh/f11711ddb125fc54c6441f47f4b82f3a to your computer and use it in GitHub Desktop.
Collect your wordle clones into one single image for easy posting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/sbin/perl | |
use strict; | |
use warnings; | |
use Image::Magick; | |
use POSIX "strftime"; | |
binmode STDIN, ":utf8"; | |
binmode STDOUT, ":utf8"; | |
my $today = strftime("%Y-%m-%d", localtime); | |
my @text_files = <"*-$today.txt">; | |
foreach my $filename (@text_files) { | |
my $rows; | |
my $image = Image::Magick->new(); | |
my $final = Image::Magick->new(); | |
$final->Set(font=>"Noto-Sans-CJK-TC", | |
pointsize=>20); | |
open(FH, '<:encoding(UTF-8)', $filename) or continue; | |
while(<FH>) { | |
if (/[0-9]/) { | |
my $title=$_; | |
$title =~ s/\n//; | |
$final->Read("label:$title"); | |
} | |
elsif (/^$/) {} | |
else { | |
foreach my $char (split //, $_) { | |
my $img_filename; | |
next if ($char eq "\n"); | |
if ($char eq "\x{2B1B}") {$img_filename = "black" ;} | |
elsif ($char eq "\x{25A1}") {$img_filename = "white" ;} | |
elsif ($char eq "\x{2B1C}") {$img_filename = "white" ;} | |
elsif ($char eq "\x{1F7E5}") {$img_filename = "red" ;} | |
elsif ($char eq "\x{1F7E6}") {$img_filename = "blue" ;} | |
elsif ($char eq "\x{1F7E7}") {$img_filename = "orange";} | |
elsif ($char eq "\x{1F7E8}") {$img_filename = "yellow";} | |
elsif ($char eq "\x{1F7E9}") {$img_filename = "green" ;} | |
elsif ($char eq "\x{1F7EA}") {$img_filename = "purple";} | |
elsif ($char eq "\x{1F7EB}") {$img_filename = "brown" ;} | |
else {die "char $char not in list."} | |
if ("$img_filename" ne "") { | |
$image->Read("colour-squares/$img_filename.png"); | |
} | |
} | |
$rows += 1; | |
} | |
} | |
close(FH); | |
$filename =~ s/\.[^.]+$//; | |
my $montage = $image->Montage(mode=>'Concatenate', | |
gravity=>'center', | |
tile=>"x$rows", | |
frame=>3, | |
border=>3, | |
font=>'Noto-Sans-CJK-TC', | |
pointsize=>10 | |
); | |
undef $image; | |
push(@$final, @$montage); | |
$final = $final->Smush(stack=>1, gravity=>'center'); | |
my $x = $final->Write("$filename.png"); | |
warn "$x" if "$x"; | |
undef $montage; | |
undef $final; | |
print("$filename.png constructed\n") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
cd "${0:a:h}" | |
today=$(date +%Y-%m-%d) | |
collect=(0 0 0 0 0 0) | |
pst "Detecting already-present results..." | |
[[ -f nerdle-$today.txt ]] && collect[1]=1 | |
[[ -f lukhap-$today.txt ]] && collect[2]=1 | |
[[ -f mjhandle-$today.txt ]] && collect[3]=1 | |
[[ -f zidou-$today.txt ]] && collect[4]=1 | |
[[ -f diffle-$today.txt ]] && collect[5]=1 | |
[[ -f tetsudoru-$today.txt ]] && collect[6]=1 | |
# Collect clipboard scores | |
interrupted=0 | |
trap 'tput cnorm; interrupted=1; pkill clipnotify' INT TERM | |
pst "Collecting scores... (Press C-c to complete)." | |
while [[ $(( ${collect[@]/%/ +} 0)) == ${#collect} ]] ; do | |
clipnotify | |
if [[ $interrupted == 1 ]]; then | |
trap - INT TERM | |
break | |
fi | |
if xclip -sel c -target TARGETS -out | grep -q image/; then | |
[[ ${collect[5]} == 1 ]] && continue | |
# Diffle | |
xclip -sel c -target image/png -out > diffle-$today.png | |
collect[5]=1 | |
echo "Stored diffle-$today.png" | |
elif xclip -sel c -target TARGETS -out | grep -q STRING; then | |
xclip -sel c -target UTF8_STRING -out | case $(head -n 1) in | |
nerdlegame*) | |
[[ ${collect[1]} == 1 ]] && continue | |
out="nerdle-$today.txt" | |
collect[1]=1 | |
;; | |
"六合"*) | |
[[ ${collect[2]} == 1 ]] && continue | |
out="lukhap-$today.txt" | |
collect[2]=1 | |
;; | |
"Mahjong Handle"*) | |
[[ ${collect[3]} == 1 ]] && continue | |
out="mjhandle-$today.txt" | |
collect[3]=1 | |
;; | |
Zidou*) | |
[[ ${collect[4]} == 1 ]] && continue | |
out="zidou-$today.txt" | |
collect[4]=1 | |
;; | |
"#テツドル"*) | |
[[ ${collect[6]} == 1 ]] && continue | |
out="tetsudoru-$today.txt" | |
collect[6]=1 | |
;; | |
*) | |
echo "Not a recognised variant, skipping" | |
continue | |
esac | |
xclip -sel c -target UTF8_STRING -out | | |
sed '/^https:/d;$a\' > $out | |
echo "Stored $out" | |
fi | |
done | |
if [[ $(( ${collect[@]/%/ +} 0)) >= 5 ]]; then | |
pst "Generating final image" | |
perl mkimg.pl # Make the images | |
# Composite them | |
{ | |
# Header | |
echo -- -gravity center \ | |
-smush 30 \ | |
\( -font "Nunito-Light" -pointsize 35 label:"Wordle-like Statistics" \) \ | |
\( -pointsize 20 label:"$today" \) \ | |
\( -size 500x2 xc:black \) | |
# Row 1: mahjong handle | |
echo mjhandle-$today.png | |
# Row 2: Cantonese | |
echo \( lukhap-$today.png zidou-$today.png +smush 30 +append \) | |
# Row 3: Other | |
if [[ -f tetsudoru-$today.png ]]; then | |
echo \( nerdle-$today.png tetsudoru-$today.png +smush 30 +append \) | |
else | |
echo nerdle-$today.png | |
fi | |
# Row 4: Diffle | |
echo diffle-$today.png | |
# Footer | |
echo -- -append png:- | |
} | | |
xargs magick | # Execute | |
tee $today.png | # Save | |
xclip -sel c -target image/png # Dump into the clipboard | |
pst Done. | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment