#!/usr/bin/env bash

# omarchy:summary=Display the Omarchy logo in the terminal using green color.

# The wordmark is block art of a fixed width, and a terminal narrower than the
# art wraps every row of it by the overflow, so the rows stop lining up and the
# picture falls apart. logo.txt is 81 columns and the window these flows open in
# is 80, which shattered it by a single column every time.
#
# Draw the widest art that fits with two columns to spare, and say the name in
# words when nothing does. The margin is there because a line of exactly as many
# characters as the terminal has columns wraps on some terminals and not others.
#
# Widths are counted by the shell, which measures in whatever the locale makes of
# the bytes -- the same units the terminal spends drawing them. Somewhere the
# block glyphs do not render, each of them also costs three columns, the count
# says 197 rather than 81, and the art steps aside on its own.
logos=("$OMARCHY_PATH/logo.txt" "$OMARCHY_PATH/logo-compact.txt")

terminal_columns() {
  local columns=${COLUMNS:-}

  [[ $columns =~ ^[0-9]+$ ]] || columns=$(tput cols 2>/dev/null)
  [[ $columns =~ ^[0-9]+$ ]] || columns=80

  printf '%s\n' "$columns"
}

logo_columns() {
  local line width=0

  while IFS= read -r line; do
    (( ${#line} > width )) && width=${#line}
  done <"$1"

  printf '%s\n' "$width"
}

clear
echo -e "\033[32m"

columns=$(terminal_columns)
drawn=0

for logo in "${logos[@]}"; do
  [[ -r $logo ]] || continue
  (( $(logo_columns "$logo") + 2 <= columns )) || continue

  cat "$logo"
  drawn=1
  break
done

(( drawn )) || echo "Omarchy"

echo -e "\033[0m"
echo
