Files
tmux-themepack/bin/build-theme
Jim Myhrberg ba8616db4c feat: Make all themes easily user-customizable
This is achieved by using custom @-prefixed tmux options which are set
with the `-o` option, meaning, the theme will only set the value if it
is not already set.

This allows users to override any of the options in the theme by simply
setting them before loading the theme.

Additionally all themes are now generated using a custom theme builder,
that allows sharing various parts of themes between them easily.
2019-12-22 10:27:04 +00:00

108 lines
2.2 KiB
Bash
Executable File

#! /usr/bin/env bash
#set -e
shopt -s extglob
[ -n "$BUILD_THEME_DEBUG" ] && set -x
trim() {
local string="$*"
string="${string#"${string%%[![:space:]]*}"}"
string="${string%"${string##*[![:space:]]}"}"
echo -n "$string"
}
find-included() {
local needle source dir found
needle="$1"
source="$2"
dir="$(dirname "$source")"
found=""
if [[ "${needle:0:1}" == "/" ]]; then
echo "$needle"
return 0
fi
while [ -z "$found" ] && [[ ! "$dir" =~ ^(\/|\.|\.\.)$ ]]; do
if [ -f "${dir}/${needle}" ]; then
found="${dir}/${needle}"
elif [ -f "${dir}/${needle}.tmuxsh" ]; then
found="${dir}/${needle}.tmuxsh"
elif [ -f "${dir}/${needle}.tmuxtheme" ]; then
found="${dir}/${needle}.tmuxtheme"
else
dir="$(dirname "$dir")"
fi
done
if [ -z "$found" ]; then
echo "ERROR: Could not find \"$needle\" to include in \"$source\"" 1>&2
return 1
else
# echo "INFO: Found \"$needle\" to include in \"$source\"" 1>&2
echo "$found"
return 0
fi
}
build-theme() {
local source target output file line included err
source="$1"
target="$2"
output=""
err="0"
while IFS= read -r line; do
if [[ "$line" =~ ^#=\ *include\ \"(.+)\".*$ ]]; then
if file="$(find-included "${BASH_REMATCH[1]}" "$source")"; then
if included="$(build-theme "${file}")"; then
output="${output}${included}
"
else
err="1"
fi
else
err="1"
fi
else
output="${output}${line}
"
fi
done < "$source"
if [ "$err" != "0" ]; then
return "$err"
elif [ -z "$target" ]; then
echo "$(trim "$output")"
else
mkdir -p "$(dirname "$target")"
echo "$(trim "$output")" > "$target"
fi
}
help() {
echo "usage: build-theme <source-file> [<target-file>]"
echo ""
echo "Arguments:"
echo " <source-file> - The theme file to build."
echo " <target-file> - Write output to specified file. If not given, print"
echo " output to STDOUT."
}
main() {
local source="$1"
local target="$2"
if [ -z "$source" ]; then
help
exit 1
elif [[ " $* " =~ ^.*\ (-h|--help)\ .*$ ]]; then
help
else
build-theme "$source" "$target"
return "$?"
fi
}
main "$@"
exit "$?"