aboutsummaryrefslogtreecommitdiff
path: root/programs/cwal/apply-theme
blob: ca195d4fecab2e6b15a828ebe9509e0bb0298d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash

# apply-theme - install cwal themes for every supported program
# Copyright (C) 2026 deltegui
#
# 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 <https://www.gnu.org/licenses/>.

set -euo pipefail

script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
components_dir="$script_dir/components"
source "$components_dir/common.sh"
cwal="$script_dir/cwal"
default_theme="$script_dir/themes/default.conf"
templates_dir="$script_dir/templates"
icon_theme_template="$templates_dir/icon-theme.txt"

# Keep this list as the only registry entry needed when adding a component.
components=(
    rofi
    mango
    labwc
    foot
    waybar
    river
    hyprland
    hyprlock
    gtk
    gtk4
    btop
    qt
    yazi
    nnn
    sway
    vim
    vis
    dwm
    st
)

use_base_theme=0
theme=
color_overrides=
color_overrides_source=
context=
override_temporary=

usage()
{
    printf 'Usage: %s [theme.conf]\n' "${0##*/}"
    printf '       %s --base <base-theme.conf> <color-overrides.conf>\n' \
        "${0##*/}"
}

require_readable()
{
    local description=$1
    local path=$2

    if [[ ! -r $path ]]; then
        printf 'apply-theme: %s is not readable: %s\n' "$description" "$path" >&2
        exit 1
    fi
}

cleanup()
{
    if [[ -n ${override_temporary:-} ]]; then
        rm -f -- "$override_temporary"
    fi
    if [[ -n ${context:-} && -d $context ]]; then
        find "$context" -type f -delete
        rmdir -- "$context" 2>/dev/null || true
    fi
}

if (( $# == 1 )) && [[ $1 == '-h' || $1 == '--help' ]]; then
    usage
    exit 0
fi

if (( $# == 0 )); then
    theme=$default_theme
elif (( $# == 1 )); then
    theme=$1
elif (( $# == 3 )) && [[ $1 == '--base' ]]; then
    theme=$2
    color_overrides=$3
    use_base_theme=1
else
    usage >&2
    exit 2
fi

require_readable theme "$theme"
if (( use_base_theme )) && [[ $color_overrides != '-' ]]; then
    require_readable 'color overrides' "$color_overrides"
fi

context=$(mktemp -d "${TMPDIR:-/tmp}/apply-theme.XXXXXX")
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
trap cleanup EXIT

export CWAL_BIN="$cwal"
export CWAL_CONTEXT="$context"
export CWAL_SCRIPT_DIR="$script_dir"
export CWAL_TEMPLATES_DIR="$templates_dir"
export CWAL_THEME="$theme"
export CWAL_OVERRIDES=

available_components=()
for component in "${components[@]}"; do
    component_script="$components_dir/$component.sh"
    if [[ ! -x $component_script ]]; then
        printf 'apply-theme: component script is not executable: %s\n' \
            "$component_script" >&2
        exit 1
    fi
    if "$component_script" detect; then
        available_components+=("$component")
    fi
done

if (( ${#available_components[@]} == 0 )); then
    printf 'apply-theme: no supported components were found\n' >&2
    exit 0
fi

if [[ ! -x $cwal ]]; then
    printf 'apply-theme: cwal is not executable: %s\n' "$cwal" >&2
    printf 'apply-theme: run make -C %s first\n' "$script_dir" >&2
    exit 1
fi

if (( use_base_theme )); then
    if [[ $color_overrides == '-' ]]; then
        override_temporary=$(mktemp "${TMPDIR:-/tmp}/apply-theme-colors.XXXXXX")
        if ! cat > "$override_temporary"; then
            printf 'apply-theme: could not read color overrides from standard input\n' \
                >&2
            exit 1
        fi
        color_overrides=$override_temporary
        color_overrides_source='standard input'
    else
        color_overrides_source=$color_overrides
    fi
    CWAL_OVERRIDES=$color_overrides
    export CWAL_OVERRIDES
fi

icon_theme=
icon_theme_installed=0
for component in "${available_components[@]}"; do
    if [[ $component == gtk || $component == qt ]]; then
        require_readable 'icon theme template' "$icon_theme_template"
        icon_theme=$(component_render_stdout "$icon_theme_template")
        break
    fi
done
if [[ -n $icon_theme ]] && icon_theme_available "$icon_theme"; then
    icon_theme_installed=1
elif [[ -n $icon_theme ]]; then
    printf 'apply-theme: icon theme is not installed, keeping the default: %s\n' \
        "$icon_theme" >&2
fi
export CWAL_ICON_THEME="$icon_theme"
export CWAL_ICON_THEME_INSTALLED="$icon_theme_installed"

run_phase()
{
    local phase=$1
    local component

    for component in "${available_components[@]}"; do
        "$components_dir/$component.sh" "$phase"
    done
}

# Every component is self-contained, but the phases remain global so all
# generated files are ready and validated before any destination is replaced.
run_phase prepare
run_phase generate
run_phase validate
run_phase install
run_phase reload

if (( use_base_theme )); then
    printf 'Theme applied from %s with colors from %s\n' \
        "$theme" "$color_overrides_source"
else
    printf 'Theme applied from %s\n' "$theme"
fi
for component in "${available_components[@]}"; do
    "$components_dir/$component.sh" summary
done

# The EXIT trap removes the shared context and any component temporaries.