-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-manager
More file actions
executable file
·223 lines (196 loc) · 6.29 KB
/
Copy paththeme-manager
File metadata and controls
executable file
·223 lines (196 loc) · 6.29 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/sh
# functions
printUsage() {
echo "Usage:
theme-manager <OPTION> ...
Options:
-c --create <name> <imagePath> Create theme
-d --delete <name> Delete theme
-h --help Show this help message
-l --list List themes
-s --set <name> (<apply>) Set theme, optionally apply wallpaper [1]
-u --update <name> <imagePath> Update theme"
}
# error codes
# 0 - success
# 1 - missing argument(s)
# 2 - wrong argument(s)
# 3 - missing dependecy
# 4 - wrong configuration file
# 5 - internal error
# Prints an given error message and returns with the given exit code
# $1 - exit code
# $2 - error message
printErr() {
echo "Error: $2" >&2
echo "Use -h or --help to display help" >&2
exit "$1"
}
printNoThemeFoundError() {
printErr 3 "No theme with name '$1' exists!
Use the '--list' option to get all available themes."
}
printTooFewArgumentsError() {
echo "too few arguments for option '$1'" >&2
echo >&2
printUsage >&2
exit 1
}
# Prints an given warning message and returns with the given exit code
# $1 - warning message
# [$2 - exit code]
printWarning() {
echo "$1" >&3
[ -n "$2" ] && exit "$2"
}
managerPath="$HOME/.config/theme-manager"
[ -d "$managerPath/themes" ] || mkdir -p "$managerPath/themes"
createDefaultCss() {
[ -d "$1" ] || printErr 5 "Failed to create default css file, which internally uses the themes colors!"
echo "Creating default css..."
{ echo "@import './colors/colors-gtk.css';"
echo ""
echo "@define-color backgroundColor @color0;"
echo "@define-color detailColor @color1;"
echo "@define-color hoverColor @color2;"
echo "@define-color borderColor @color3;"
echo "@define-color textColor @color4;"
echo "@define-color transparent rgba(0, 0, 0, 0);"
} > "$1/colors.css"
}
# Checks if a theme exists or not. Returns 0 if it does, 1 if it doesn't
checkIfThemeExists() {
themeExists=false
for file in "$managerPath/themes/"*; do
if [ "$(basename "$file")" = "$1" ]; then
themeExists=true
fi
done
if [ "$themeExists" = "true" ]; then
return 0
else
return 1
fi
}
createTheme() {
[ -r "$2" ] || printErr 1 "Specified image '$2' does not exist or is not readable!"
if [ "$1" = "active" ] || [ "$1" = "auto" ]; then
printErr 2 "The name '$1' is reserved!"
fi
if checkIfThemeExists "$1"; then
printErr 3 "Theme with name '$1' already exists!
Please choose a other name.
Use the '--list' option to get all available themes."
fi
mkdir -p "$managerPath/themes/$1/colors/"
theme-generator -i "$2" -o "$managerPath/themes/$1/" -f pghtr
success=$?
if [ "$success" = "0" ]; then
createDefaultCss "$managerPath/themes/$1/"
echo "Successfully created theme '$1'"
else
rm -r "$managerPath/themes/$1/"
printWarning "Failed to create theme '$1'"
fi
}
updateTheme() {
[ -r "$2" ] || printErr 1 "Specified image '$2' does not exist or is not readable!"
if [ "$1" = "active" ] || [ "$1" = "auto" ]; then
printErr 2 "The name '$1' is reserved! It cannot be updated."
fi
checkIfThemeExists "$1" || printNoThemeFoundError "$1"
printf "Are you sure you want to update the theme '%s'? [y/N]: " "$1"
read -r sure
case "$sure" in
[yY][eE][sS]|[yY]) ;;
*) printWarning "Aborting..." 0 ;;
esac
theme-generator -i "$2" -o "$managerPath/themes/$1/" -f pghtr
success=$?
if [ "$success" = "0" ]; then
echo "Successfully updated theme '$1'"
fi
}
deleteTheme() {
[ "$1" = "active" ] && printErr 2 "The name 'active' is reserved! It cannot be deleted."
checkIfThemeExists "$1" || printNoThemeFoundError "$1"
if [ -d "$managerPath/themes/$1/" ]; then
rm -r "${managerPath:?}/themes/$1/"
fi
echo "Successfully deleted theme '$1'"
}
listThemes() {
for theme in "$managerPath/themes/"*; do
if [ -d "$theme/" ]; then
themeName=$(basename "$theme")
if ! [ "$themeName" = "active" ] && ! [ "$themeName" = "auto" ]; then
echo " - $themeName"
fi
fi
done
}
setTheme() {
[ "$1" = "active" ] && printErr 2 "The name 'active' is reserved! It cannot be set to."
checkIfThemeExists "$1" || printNoThemeFoundError "$1"
if [ -d "$managerPath/thems/active/" ]; then
mkdir -p "$managerPath/themes/active/"
fi
rm -r "$managerPath/themes/active/"
cp -r "$managerPath/themes/$1/" "$managerPath/themes/active/"
if [ "$2" = "1" ]; then
echo applying wallpaper...
theme-applier --auto-update
else
echo not applying wallpaper...
# Save the old state
hyprState="$(theme-applier -g hyprpaper)"
autoState="$(theme-applier -g auto)"
# Set the variables
theme-applier -s hyprpaper false
theme-applier -s auto true
# Execute the theme-applier
theme-applier --auto-update
# Revert the variables to what they were before
theme-applier -s hyprpaper "$hyprState"
theme-applier -s auto "$autoState"
fi
}
# check if usage has to be printed
if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
printUsage
exit 0
fi
# check if theme-generator is installed
which theme-generator >/dev/null 2>&1 || printErr 2 "theme-generator is not installed!"
# check if theme-applier is installed
which theme-applier >/dev/null 2>&1 || printErr 4 "theme-applier is not installed!"
# execute option
while [ $# -gt 0 ]; do
case "$1" in
-c | --create)
[ $# -lt 3 ] && printTooFewArgumentsError "$1"
createTheme "$2" "$3"
shift 3 ;;
-u | --update)
[ $# -lt 3 ] && printTooFewArgumentsError "$1"
updateTheme "$2" "$3"
shift 3 ;;
-d | --delete)
[ $# -lt 2 ] && printTooFewArgumentsError "$1"
deleteTheme "$2"
shift 2 ;;
-l | --list)
listThemes
shift ;;
-s | --set)
[ $# -lt 2 ] && printTooFewArgumentsError "$1"
if [ "$3" != "" ]; then
setTheme "$2" "$3"
shift
else
setTheme "$2"
fi
shift 2 ;;
*) printErr 2 "Unknown option: '$1'" ;;
esac
done