#!/data/data/com.termux/files/usr/bin/bash
# crypto: prices and the last 24 hours, from CoinGecko.
# Pick coins by their CoinGecko id, and the money to show them in.
# termux.zanoski.com/widgets
COINS="bitcoin ethereum solana"
MONEY="usd"
e=$'\e'; R="${e}[0m"
ids=$(echo $COINS | tr ' ' ,)
j=$(curl -s -m 8 "https://api.coingecko.com/api/v3/simple/price?ids=$ids&vs_currencies=$MONEY&include_24hr_change=true")
[ -z "$j" ] && { echo "no network"; exit; }
for c in $COINS; do
  o=$(echo "$j" | grep -o "\"$c\":{[^}]*}")
  p=$(echo "$o" | grep -o "\"$MONEY\":[0-9.e+-]*" | cut -d: -f2)
  ch=$(echo "$o" | grep -o "\"${MONEY}_24h_change\":[0-9.e+-]*" | cut -d: -f2)
  [ -z "$p" ] && { printf "${e}[90m%-9s no price$R\n" "$c"; continue; }
  awk -v c="$c" -v p="$p" -v ch="${ch:-0}" -v m="$MONEY" -v e="$e" 'BEGIN{
    col = ch >= 0 ? "32" : "31"; sign = ch >= 0 ? "▲" : "▼"; if (ch < 0) ch = -ch
    printf "%s[1m%-9s%s[0m %12s %s  %s[%sm%s %.1f%%%s[0m\n", e, c, e, sprintf("%\047.2f", p), toupper(m), e, col, sign, ch, e }'
done
