-
Notifications
You must be signed in to change notification settings - Fork 9
/
script.sh
executable file
·137 lines (117 loc) · 3.66 KB
/
script.sh
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
#!/bin/sh
# pywal2alacritty - get colors from Pywal and apply to Alacritty config
# v0.2.0
# github.com/egeesin
#
# Deps: grep, sed
# Usage: ./script.sh
# ./script.sh <config toml>
# Function to display error and quit
die() {
printf "ERR: %s\n" "$1" >&2
exit 1
}
DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.toml
# Wal generates a shell script that defines color0..color15
SRC="$HOME"/.cache/wal/colors.sh
[ -e "$SRC" ] || die "Wal colors not found, exiting script. Have you executed Wal before?"
printf "Colors found, source ready.\n"
READLINK=$( command -v greadlink || command -v readlink )
# Get config file
if [ -n "$1" ]; then
[ -e "$1" ] || die "Selected config doesn't exist, exiting script."
printf "Config found, destination ready.\n"
CFG=$1
[ -L "$1" ] && {
printf "Following symlink to config...\n"
CFG=$($READLINK -f "$1")
}
else
# Default config path in Mac systems
[ -e "$DEFAULT_MACOS_CONFIG" ] || die "Alacritty config not found, exiting script."
CFG="$DEFAULT_MACOS_CONFIG"
[ -L "$DEFAULT_MACOS_CONFIG" ] && {
printf "Following symlink to config...\n"
CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG")
}
fi
# Get hex colors from Wal cache
# No need for shellcheck to check this, it comes from pywal
# shellcheck disable=SC1090
. "$SRC"
# Create temp file for sed results
tempfile=$(mktemp)
trap 'rm $tempfile' INT TERM EXIT
# Delete existing color declarations generated by this script
# If begin comment exists
if grep -q '^# BEGIN WAL' "$CFG"; then
# And if end comment exists
if grep -q '^# END WAL' "$CFG"; then
# Delete contents of the block
printf "Existing generated colors found, replacing new colors...\n"
sed '/^# BEGIN WAL/,/^# END WAL/ {
/^# BEGIN WAL/! { /^# END WAL/!d; }
}' "$CFG" > "$tempfile" \
&& cat "$tempfile" > "$CFG"
# If no end comment, don't do anything
else
die "No '# END WAL' comment found, please ensure it is present."
fi
# If no begin comment found
else
# Don't do anything and notify user if there's an end comment in the file
! grep -q '^# END WAL' "$CFG" || die "Found '# END WAL' comment, but no '# BEGIN WAL' comment found. Please ensure it is present."
printf "There's no existing 'generated' colors, adding comments...\n";
printf '# BEGIN WAL\n# END WAL' >> "$CFG";
fi
# Write new color definitions
# We know $colorX is unset, we set it by sourcing above
# shellcheck disable=SC2154
{ sed "/^# BEGIN WAL/ r /dev/stdin" "$CFG" > "$tempfile" <<EOF
[colors.primary]
background = '$color0'
foreground = '$color7'
[colors.cursor]
text = '$color0' # Apply background color to color of text inside cursor
cursor = '$color7' # Apply foreground color to color of cursor
[colors.vi_mode_cursor]
text = '$color0' # Same as above
cursor = '$color7'
[colors.search.matches]
foreground = '$color0' # Same as above
background = '$color15' # Apply bright/white color to bg color of matching search
[colors.search.focused_match]
foreground = 'CellBackground'
background = 'CellForeground'
[colors.line_indicator]
foreground = 'None'
background = 'None'
[colors.footer_bar]
foreground = '$color8'
background = '$color7'
[colors.selection]
text = 'CellBackground'
background = 'CellForeground'
[colors.normal]
black = '$color0'
red = '$color1'
green = '$color2'
yellow = '$color3'
blue = '$color4'
magenta = '$color5'
cyan = '$color6'
white = '$color7'
[colors.bright]
black = '$color8'
red = '$color9'
green = '$color10'
yellow = '$color11'
blue = '$color12'
magenta = '$color13'
cyan = '$color14'
white = '$color15'
EOF
} && cat "$tempfile" > "$CFG" \
&& rm "$tempfile"
trap - INT TERM EXIT
printf "'%s' exported to '%s'\n" "$SRC" "$CFG"