dotfiles/.local/bin/i3-config-builder

72 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-05-27 21:07:07 +02:00
#!/bin/bash
2022-05-29 01:44:01 +02:00
#
# Title: i3-config-builder
# Version: 0.1.0
# Author: dabruh
# GitLab: https://gitlab.com/dabruh
# GitHub: https://github.com/dabruh
#
# Builds an i3/i3status configuration file by merging
# multiple defined configurations in an orderly manner.
#
# The files you wish to include MUST exist under ~/.config/i3(status)/layers.
# The files MUST start with one of the defined "PATTERNS" below. However, they
# WILL NOT be read unless explicitly mentioned in a .layer file located in the
# same directory.
#
# Two .layer files are read during execution:
# - default.layer
# - $HOSTNAME.layer: This file doesn't have to exist, but will allow you to
# load a special set of files depending on the hostname of the machine which
# executes this script.
#
# The contents of the .layer files are read first, and then sorted.
#
HOSTNAME="$(hostname | cut -d. -f1)"
PATTERNS=(
"config.vars"
"config.pre-main"
"config.main"
"config.post-main"
)
2022-05-29 00:31:14 +02:00
2022-05-29 01:44:01 +02:00
function i3setup() {
local file dir layers config includes path component="$1"
2022-05-29 00:31:14 +02:00
dir="$HOME/.config/$component"
2022-05-29 01:44:01 +02:00
layers="$dir/layers"
config="$dir/config"
2022-05-27 21:07:07 +02:00
2022-05-29 00:31:14 +02:00
echo "Building $component config... 🔨"
2022-05-29 01:44:01 +02:00
rm "$config" 2>/dev/null
includes=$(cat "$layers/default.layer" "$layers/$HOSTNAME.layer" 2>/dev/null | sort -u)
2022-05-27 21:07:07 +02:00
2022-05-29 01:44:01 +02:00
for pattern in "${PATTERNS[@]}"; do
for file in $includes; do
! [[ "$file" =~ "$pattern"* ]] && continue
path="$layers/$file"
2022-05-27 21:07:07 +02:00
2022-05-29 01:44:01 +02:00
if ! [ -f "$path" ] && ! [ -L "$path" ]; then
echo "ERROR: Invalid path: $path"
continue
fi
2022-05-27 21:07:07 +02:00
2022-05-29 01:44:01 +02:00
echo "WRITE : '$file'."
2022-05-27 21:07:07 +02:00
{
echo "# >>>$file"
2022-05-29 01:44:01 +02:00
cat "$path"
2022-05-27 21:07:07 +02:00
echo "# <<<$file"
echo
2022-05-29 01:44:01 +02:00
} >>"$config"
done
2022-05-27 21:07:07 +02:00
done
echo
}
2022-05-29 00:31:14 +02:00
i3setup i3
i3setup i3status
2022-05-29 01:44:01 +02:00
echo "Restarting i3... 🔄"
i3 restart