#!/bin/bash

    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.

    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.

# ptp (Print Tempus Palette) --- Prints the colour palette of the active
# Tempus theme and colourises the value of each point accordingly.  Part
# of my dotfiles: https://gitlab.com/protesilaos/dotfiles.
# 
# The Tempus themes is a standalone project of mine, which is also
# integrated into my custom desktop session.  For more about it, see:
# https://protesilaos.com/tempus-themes.
#
# NOTE ABOUT THE OUTPUT: on terminals that use a dark Tempus theme, the
# colours 0 and 8 will not be visible (maybe colour 8 will be bearely
# visible).  Similarly, colours 7 and 15 will not appear on light
# themes.  This is because col0 is the dark theme's background, with
# col8 functioning as the alternative background in various contexts.
# For light themes we have col15=bg and col7=bgalt.

# This will work if the colours are in place (my dots rely on GNU Stow).
my_colours="$HOME/.local/share/my_colours"
active_theme="$my_colours/active-tempus-theme.sh"
active_theme_content="$my_colours/active-theme-content"

# Check if the content of the active theme is present, else exit.
[ -f "$active_theme" ] || { echo "ERROR: No available Tempus theme."; exit 1; }

# This cleans up the file that contains all the colour values.  It only
# gets the hexadecimal RGB value (aka "HEX") of the 16-colour palette,
# while omitting any other parameters.
get_colours () {
	sed '/color*/!d ; s,.*"\(#.*\)",\1,g' "$active_theme_content" | \
	while IFS=$'\n' read -r line; do
		echo -n "$line "
	done
}

# Declare an empty array, which we then populate with the HEX colour
# values.  We will be iterating over this array to print the table with
# the colours.
tempus_palette=()
for i in $(get_colours); do
	tempus_palette+=( "$i" )
done

# Get the unique name of the active theme.
tempus_slug="$(grep -o '[a-z]*\.sh' "$active_theme")"

# The row with the colour values needs to not exceed the length of the
# current terminal.  So we use conditional logic to print space-aware
# output.
colour_row() {
	local block columns
	columns="$(tput cols)"
	# Unicode code point for full block character.  See wiki entry:
	# https://en.wikipedia.org/wiki/Box-drawing_character
	block='\u2588\u2588'

	if [ "$columns" -gt 90 ]; then
		echo -en "\\033[38;5;${i}m${block} ${tempus_palette[$i]} \\033[0m";
	elif [ "$columns" -gt 80 ]; then
		echo -en "\\033[38;5;${i}m${block} ${tempus_palette[$i]:1} \\033[0m";
	else
		echo -en "\\033[38;5;${i}m${tempus_palette[$i]} \\033[0m";
	fi
}

# Here we use parameter expansion to trim the final three characters.
# No need to run an other command, such as sed or cut.
echo "Tempus ${tempus_slug:0:-3}:"

# Print the 16-colour palette in two rows that represent the standard
# and bright variants.  Colourise each block with its respective colour
# using ANSI escape sequences.
for i in {0..15}; do
	colour_row
	if [ "$i" == 7 ]; then
		# Leave an empty line to draw the two rows properly and continue
		# with the rest of the loop.
		echo "" && continue
	fi
done
# Leave an empty line, otherwise it messes up with the position of the
# subsequent shell prompt.
echo ""
