#!/usr/bin/env zsh
# ── splash.zsh : rotating planet (left) BESIDE fastfetch system info (right) ──
# fastfetch info is captured once with its column-align codes (\e[NG) shifted
# right by the planet width, then the planet animates in the left columns using
# absolute row positioning so it never overwrites the info.
# Usage: splash.zsh [seconds] (0/empty = until keypress)
emulate -L zsh
zmodload zsh/mathfunc 2>/dev/null || return 1
zmodload zsh/datetime 2>/dev/null
local timeout="${1:-0}"
local -i W=40 H=21
local -i OFF=$((W+3)) # info starts here
local -F cx=$((W/2.0)) cy=$((H/2.0)) Rx=8.5 Ry=7.5 tilt=1.05 Rring=12.5
local -F lx=-0.50 ly=-0.55 lz=0.67
local -a grad=(
"49;116;143" "49;116;143" "156;207;216" "156;207;216" "196;167;231"
"196;167;231" "235;188;186" "235;188;186" "235;111;146" "235;111;146"
"246;193;119" "246;193;119" "235;188;186" "156;207;216" "49;116;143"
)
local -a ramp=(' ' '.' ':' '-' '+' 'o' 'O' '0' '8' '@')
# ── capture fastfetch info (no logo), shift column-align codes right by OFF ──
local -a info
if (( $+commands[fastfetch] )); then
info=("${(@f)$(OFF=$OFF fastfetch --logo none --pipe false 2>/dev/null \
| perl -pe 's/\e\[2J//g; s/\e\[\?25[lh]//g; s/\e\[\d*;\d*[Hf]//g; s/\e\[(\d+)G/"\e[".($ENV{OFF}+$1)."G"/ge' )}")
fi
# ── precompute static sphere geometry ──
local -a s_on s_illum s_lon s_grad
local -i x y i
local -F nx ny nz d2 illum
for (( y=0; y<H; y++ )); do for (( x=0; x<W; x++ )); do
i=$((y*W+x))
nx=$(( (x-cx)/Rx )); ny=$(( (y-cy)/Ry )); d2=$(( nx*nx+ny*ny ))
if (( d2 <= 1.0 )); then
nz=$(( sqrt(1.0-d2) )); illum=$(( nx*lx+ny*ly+nz*lz ))
(( illum<0.10 )) && illum=0.10; (( illum>1.0 )) && illum=1.0
s_on[i+1]=1; s_illum[i+1]=$illum; s_lon[i+1]=$(( atan(nx,nz) )); s_grad[i+1]=$(( y*${#grad}/H+1 ))
else s_on[i+1]=0; fi
done; done
printf '\e[2J\e[?25l'
trap 'printf "\e[?25h\e[0m\n"' EXIT INT TERM
# paint the info column once (rows fixed; planet won't touch col > W)
local -i li
for (( li=1; li<=${#info}; li++ )); do printf '\e[%d;%dH%s' $li $((OFF+1)) "${info[li]}"; done
local start=$EPOCHSECONDS
local -F rot=0.0 morb=0.0 t rx rz sx sy depth
local -a buf bcol
local key
while :; do
buf=(); bcol=(); for (( i=1; i<=W*H; i++ )); do buf[i]=' '; bcol[i]=''; done
# back ring
for (( t=0; t<6.2832; t+=0.05 )); do
rx=$(( Rring*cos(t+rot*0.5) )); rz=$(( Rring*sin(t+rot*0.5) ))
sx=$(( cx+rx )); sy=$(( cy+rz*sin(tilt)*Ry/Rx )); depth=$(( rz*cos(tilt) ))
(( depth>=0 )) && continue
local -i ix=$(( int(rint(sx)) )) iy=$(( int(rint(sy)) ))
(( ix<0||ix>=W||iy<0||iy>=H )) && continue
i=$((iy*W+ix+1)); buf[i]='·'; bcol[i]="120;230;200"
done
# planet
for (( i=1; i<=W*H; i++ )); do
(( s_on[i] )) || continue
local -F il=$s_illum[i] band=$(( sin((s_lon[i]+rot)*5.0) ))
local -F ic=$(( il + (band>0.35?0.16:(band<-0.35?-0.12:0.0)) ))
(( ic<0.10 )) && ic=0.10; (( ic>1.0 )) && ic=1.0
local -i ri=$(( int(ic*(${#ramp}-1))+1 )); (( ri>${#ramp} )) && ri=${#ramp}
buf[i]=$ramp[ri]
local rgb=${grad[s_grad[i]]}; local -i r=${rgb%%;*} g=${${rgb#*;}%%;*} b=${rgb##*;}
local -F sh=$(( 0.32+0.68*ic )); bcol[i]="$(( int(r*sh) ));$(( int(g*sh) ));$(( int(b*sh) ))"
done
# front ring
for (( t=0; t<6.2832; t+=0.05 )); do
rx=$(( Rring*cos(t+rot*0.5) )); rz=$(( Rring*sin(t+rot*0.5) ))
sx=$(( cx+rx )); sy=$(( cy+rz*sin(tilt)*Ry/Rx )); depth=$(( rz*cos(tilt) ))
(( depth<0 )) && continue
local -i ix=$(( int(rint(sx)) )) iy=$(( int(rint(sy)) ))
(( ix<0||ix>=W||iy<0||iy>=H )) && continue
i=$((iy*W+ix+1)); buf[i]='•'; bcol[i]="255;120;150"
done
# moon
local -F mx=$(( cx+(Rring+2.5)*cos(morb) )) my=$(( cy+(Rring+2.5)*sin(morb)*sin(tilt)*Ry/Rx ))
local -i mix=$(( int(rint(mx)) )) miy=$(( int(rint(my)) ))
(( mix>=0&&mix<W&&miy>=0&&miy<H )) && { i=$((miy*W+mix+1)); buf[i]='●'; bcol[i]="255;236;160"; }
# paint planet rows with absolute positioning (left W cols only)
local line lastcol
for (( y=0; y<H; y++ )); do
line=''; lastcol=''
for (( x=0; x<W; x++ )); do
i=$((y*W+x+1))
if [[ -n $bcol[i] ]]; then
[[ $bcol[i] != $lastcol ]] && { line+=$'\e[38;2;'"$bcol[i]"'m'; lastcol=$bcol[i]; }
line+=$buf[i]
else [[ -n $lastcol ]] && { line+=$'\e[0m'; lastcol=''; }; line+=' '; fi
done
printf '\e[%d;1H%s\e[0m' $((y+1)) "$line"
done
rot=$(( rot+0.13 )); morb=$(( morb+0.10 ))
if read -t 0.06 -k key 2>/dev/null; then break; fi
(( timeout>0 )) && (( EPOCHSECONDS-start >= timeout )) && break
done
local -i below=$(( H>${#info}?H:${#info} ))
printf '\e[%d;1H\e[?25h\e[0m\n' $((below+1))