#!/usr/bin/env bash
# base16-preview.sh - Preview base16 themes as terminal color swatches

nix build nixpkgs#base16-schemes

THEMES_DIR="${1:-result/share/themes}"

for theme in "$THEMES_DIR"/*.yaml; do
  name=$(basename "$theme" .yaml)
  echo -e "\n\033[1m$name\033[0m"

  # Extract base00-base0F hex values
  colors=()
  while IFS=': ' read -r key value; do
    case "$key" in
      base0[0-9A-Fa-f]|base1[0-5]) colors+=("${value//\"/}") ;;
    esac
  done < "$theme"

  # Print swatches
  for hex in "${colors[@]}"; do
    hex="${hex#\#}"
    r=$((16#${hex:0:2}))
    g=$((16#${hex:2:2}))
    b=$((16#${hex:4:2}))
    printf "\033[48;2;%d;%d;%dm    \033[0m" "$r" "$g" "$b"
  done
  echo
done
