-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect-kitty-theme
executable file
·94 lines (85 loc) · 2.62 KB
/
select-kitty-theme
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
#!/usr/bin/env bash
# Usage: select-kitty-theme
# Select a theme for kitty from the dexpota/kitty-themes repository
set -e
KITTY_CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/kitty"
KITTY_THEME_REPO="${KITTY_CONFIG_DIR}/kitty-themes"
KITTY_THEME_DIR="${KITTY_THEME_REPO}/themes"
KITTY_CONFIG_FILE="${KITTY_CONFIG_DIR}/kitty.conf"
KITTY_THEME_LINK="${KITTY_CONFIG_DIR}/theme.conf"
CURRENT_THEME="$(basename "$(readlink ~/.config/kitty/theme.conf)")"
check_for_kitty_theme_repo () {
if ! [ -d "$KITTY_THEME_REPO" ]
then
read -rp 'Theme directory not found, download? [Y/n] ' response
case "$response" in
y|Y) git clone https://github.com/dexpota/kitty-themes "$KITTY_THEME_REPO" ;;
esac
fi
(
cd "$KITTY_THEME_REPO" || (
echo 'Failed to cd to kitty theme directory.' >&2; exit 2
)
git pull
)
}
random_array_element () {
array_name="$1[@]"
array_with_indices=("${!array_name}")
random_index="$((RANDOM % ${#array_with_indices[@]}))"
random_element="${array_with_indices["$random_index"]}"
echo -n "$random_element"
}
check_for_theme_include_in_kitty_config () {
if ! grep '^include ./theme.conf' "$KITTY_CONFIG_FILE" >/dev/null
then
read -rp "Theme file is not sourced in the kitty config. Would you like to add it? [y/N]" response
case "$response" in
y|Y) echo 'include ./theme.conf' >> "$KITTY_CONFIG_FILE" ;;
*) echo 'The theme may not persist.' >&2
esac
fi
}
set_temporary_theme () {
local theme_name
theme_name="$1"
echo "Updating current session to use the theme $theme_name." >&2
kitty @ set-colors -a "${KITTY_THEME_DIR}/${theme_name}.conf"
}
themes=( '**Random theme**' )
while IFS='' read -r line
do
if [ "$CURRENT_THEME" = "$line" ]
then
line="* $line"
else
line=" $line"
fi
themes+=("${line%.conf}")
done < <(ls "$KITTY_THEME_DIR")
# This requires bash
select theme in "${themes[@]}"
do
while [ "$theme" = '**Random theme**' ]
do
echo 'Random time!'
theme="$(random_array_element 'themes')"
done
if [[ "$theme" =~ ^\* ]]
then
echo "${theme/\* } is already the default theme." >&2
continue
fi
theme="${theme// }"
set_temporary_theme "$theme"
read -rp "Use theme as default? [y/N] (or quit) " response
case "$response" in
y|Y)
ln -sf "${KITTY_THEME_DIR}/${theme}.conf" "$KITTY_THEME_LINK"
check_for_theme_include_in_kitty_config
exit
;;
q|quit) exit ;;
esac
done && \
set_session_theme "$CURRENT_THEME"