-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-folder-icon
executable file
·358 lines (303 loc) · 7.64 KB
/
set-folder-icon
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env bash
# This script allows changing the icon of a folder
#
# @author: Junior Martins (https://github.com/jrom99)
# @license: MIT license (MIT)
# @link: https://github.com/jrom99/set-folder-icon
if test -z "$BASH_VERSION"; then
printf "Error: this script only works in bash.\n" >&2
exit 1
fi
if (( BASH_VERSINFO[0] * 10 + BASH_VERSINFO[1] < 40 )); then
printf "Error: this script requires bash version >= 4.0\n" >&2
exit 1
fi
# set -x # Uncomment to debug this shell script
set -o errexit \
-o noclobber \
-o pipefail
PROGNAME="$(basename "${BASH_SOURCE[0]}")"
PROGVERSION="0.1.2"
readonly PROGNAME
readonly PROGVERSION
readonly -a ARGS=("$@")
msg() {
printf "%s: %b\n" "$PROGNAME" "$*"
}
err() {
msg "Error:" "$*" >&2
}
verbose() {
[ -t 4 ] || return 0
msg "$@" >&4
}
fatal() {
err "$*"
exit 1
}
usage() {
cat <<- EOF
USAGE
$ $PROGNAME [-d] FOLDER [-t] TYPE
$ $PROGNAME --delete-icon FOLDER
$ $PROGNAME --get-icon FOLDER
$ $PROGNAME --list
OPTIONS
-t --type TYPE folder icon type
-d --dir FOLDER directory path
--del --delete-icon FOLDER remove custom icon
-f --force ignore type validity check
-g --get --get-icon FOLDER show icons
-l --list show available types
-V --version print $PROGNAME version and exit
-h --help show this help
EOF
exit "${1:-0}"
}
_is_valid_type() {
local dir_type="$1"
for i in "${types[@]}"; do
[ "$i" == "$dir_type" ] || continue
return 0
done
return 1
}
get_types() {
# based on Papyrus icons types
local -a duptypes=()
local -a valid_types=("activities" "android" "apple" "applications"
"arduino" "backup" "books" "cd" "code" "copy-cloud" "desktop"
"development" "documents-open" "documents" "download-open" "download"
"downloads" "drag-accept" "dropbox" "favorites" "games" "git" "github"
"gitlab" "gnome" "google-drive" "image-people" "important" "java"
"kde" "linux" "locked" "mail-cloud" "mail" "mega" "meocloud"
"music-open" "music" "network" "nextcloud" "onedrive" "open"
"owncloud" "pcloud" "photo" "pictures-open" "pictures" "print"
"private" "projects" "public" "publicshare-open" "recent"
"remove-open" "remote" "script" "snap" "steam" "sync" "syncthing"
"tar" "templates-open" "templates" "torrent" "unlocked" "vbox"
"video" "videos-open" "videos" "visiting" "vmware" "wifi"
"wine" "yandex-disk")
for theme_dir in "${THEME_DIRS[@]}"; do
for dir_type in "${valid_types[@]}"; do
prefix="$theme_dir/48x48/places/folder-$dir_type"
if [[ -e "$prefix.svg" || -e "$prefix.png" ]]; then
duptypes+=( "$dir_type" )
fi
done
done
# remove duplicates
while IFS= read -r -d '' item; do
types+=("$item")
done < <(printf "%s\0" "${duptypes[@]}" | sort -uz)
verbose "Found types ${types[*]}"
}
get_theme_dirs() {
declare -A THEME_DIRS_DICT
local -a themes_order=()
local -a data_dirs=()
local -a icons_dirs=(
"$HOME/.icons"
"${XDG_DATA_HOME:-$HOME/.local/share}/icons"
)
# Get data directories from XDG_DATA_DIRS variable and
# convert colon-separated list into bash array
IFS=: read -ra data_dirs <<< "${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
for data_dir in "${data_dirs[@]}"; do
[[ -d "$data_dir/icons" ]] || continue
icons_dirs=( "${icons_dirs[@]}" "${data_dir%/}/icons" )
done
# removes trailing quotes
THEME_NAME="$(gsettings get org.gnome.desktop.interface icon-theme)"
THEME_NAME="${THEME_NAME//\'/}"
# set first theme dir
for icon_dir in "${icons_dirs[@]}"; do
theme_dir="$icon_dir/$THEME_NAME"
if [[ -d "$theme_dir" && -f "$theme_dir/index.theme" ]]; then
THEME_DIRS_DICT[$THEME_NAME]="$theme_dir"
fi
done
[[ ! -d "${THEME_DIRS_DICT[$THEME_NAME]}" ]] && fatal "Theme $THEME_NAME not found"
themes_order+=( "$THEME_NAME" )
CONTINUE=1
while [[ $CONTINUE -eq 1 ]]; do
# get subthemes
CONTINUE=0
for theme_dir in "${THEME_DIRS_DICT[@]}"; do
[[ "$theme_dir" == "NOTFOUND" ]] && continue
theme_index="$theme_dir/index.theme"
if grep -qoP "(?<=^Inherits=).+" "$theme_index"; then
IFS=, read -ra subthemes <<< "$(grep -oP -m1 "(?<=^Inherits=).+" "$theme_index")"
for theme_name in "${subthemes[@]}"; do
if [[ "${THEME_DIRS_DICT[$theme_name]}" == "NOTFOUND" ]]; then
continue
elif [[ ! -d "${THEME_DIRS_DICT[$theme_name]}" ]]; then
THEME_DIRS_DICT[$theme_name]=""
CONTINUE=1
fi
done
fi
done
# search theme dirs
for theme_name in "${!THEME_DIRS_DICT[@]}"; do
[[ -z "${THEME_DIRS_DICT[$theme_name]}" ]] || continue
for icon_dir in "${icons_dirs[@]}"; do
theme_dir="$icon_dir/$theme_name"
if [[ -d "$theme_dir" && -f "$theme_dir/index.theme" ]]; then
THEME_DIRS_DICT[$theme_name]="$theme_dir"
fi
done
if [[ -d "${THEME_DIRS_DICT[$theme_name]}" ]]; then
themes_order+=( "$theme_name" )
else
msg "Warning: Theme $theme_name not found"
THEME_DIRS_DICT[$theme_name]="NOTFOUND"
fi
done
done
verbose "Found themes ${themes_order[*]}"
for theme_name in "${themes_order[@]}"; do
THEME_DIRS+=( "${THEME_DIRS_DICT[$theme_name]}" )
done
}
list_types() {
local dir_type=''
if [[ "${#types[@]}" -eq 0 ]]; then
fatal "No type found in ${THEME_DIRS[*]}"
fi
if [ -t 1 ]; then
cat <<- EOF
List of available types:
$(printf "\n%s" "${types[@]}" | column)
EOF
else
printf "\n%s" "${types[@]}"
fi
}
change_icon () {
local folder="$1"
local type="$2"
if [[ "$FORCE" == "0" ]]; then
_is_valid_type "$type" || fatal "Unable to find '$type' type in ${THEME_DIRS[*]}"
fi
if [[ -d "$folder" ]]; then
if [[ "$FORCE" == "1" ]]; then
_type="$type"
else
_type="folder-${type}"
fi
gio set -t string "$folder" metadata::custom-icon-name "$_type"
else
fatal "'$folder' is not a path to a directory"
fi
}
get_icon () {
local folder="$1"
if [[ -d "$folder" ]]; then
gio info -a standard::name,standard::icon,metadata::custom-icon-name "$folder"
else
fatal "'$folder' is not a path to a directory"
fi
}
delete_icon () {
local folder="$1"
if [[ -d "$folder" ]]; then
gio set -d "$folder" metadata::custom-icon-name
else
fatal "'$folder' is not a path to a directory"
fi
}
parse_args() {
# Show help if no argument is passed
if [ -z "$1" ]; then
usage 2
fi
# parse kwargs
while [ $# -gt 0 ]; do
case "$1" in
--dir*|-d*)
if [[ "$1" != = ]]; then shift; fi
FOLDER="${1#*=}"
;;
--type*|-t*)
if [[ "$1" != = ]]; then shift; fi
TYPE="${1#*=}"
;;
--help|-h)
usage 0
;;
--version|-V)
printf "%s %s\n" "$PROGNAME" "$PROGVERSION"
exit 0
;;
--verbose)
VERBOSE=1
;;
--force|-f)
FORCE=1
;;
--list|-l)
OPERATION="list"
;;
--del*)
OPERATION="delete"
;;
-g|--get*)
OPERATION="get"
;;
-*)
echo "illegal option -- '$1'"
usage 2
;;
*)
args+=( "$1" )
;;
esac
shift
done
# parse args
for arg in "${args[@]}"; do
if [[ -d "$arg" && -z "$FOLDER" ]]; then
FOLDER="$arg"
elif [[ -z "$TYPE" ]]; then
TYPE="$arg"
else
FOLDER="$arg"
fi
done
if [[ -z "$OPERATION" ]]; then
OPERATION="change"
fi
}
main() {
local FOLDER TYPE OPERATION
declare -a THEME_DIRS
declare -a types
declare -i VERBOSE="${VERBOSE:-0}"
declare -i FORCE=0
parse_args "${ARGS[@]}"
if [[ "$VERBOSE" -eq "1" ]]; then
# open a file descriptor for verbose messages
exec 4>&1
# close it before exiting
trap 'exec 4>&-' EXIT HUP INT TERM
fi
get_theme_dirs
get_types
case "$OPERATION" in
delete)
delete_icon "$FOLDER"
;;
get)
get_icon "$FOLDER"
;;
list)
list_types
;;
change)
change_icon "$FOLDER" "$TYPE"
;;
esac
exit 0
}
main