Mark active preset when listing.

This commit is contained in:
Shy 2024-08-05 18:09:29 +02:00
parent 6308be7749
commit 80d5972f80

23
wled
View file

@ -8,7 +8,7 @@
NAME="wled.sh" NAME="wled.sh"
COMMAND="wled" COMMAND="wled"
VERSION="0.1.0" VERSION="0.1.1"
print_usage() { print_usage() {
echo "\ echo "\
@ -35,6 +35,15 @@ curl_cmd() {
curl --silent --show-error --fail --ipv4 --max-filesize 32K "$@" curl --silent --show-error --fail --ipv4 --max-filesize 32K "$@"
} }
active_preset() {
# Find number of active preset. API returns -1 if none is active, we return
# an empty string.
preset="$(curl_cmd "http://$1/json" | \
# -1 will be a no-match.
sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p')"
printf '%u' "$preset"
}
# Parse host argument. # Parse host argument.
case "$1" in case "$1" in
""|-h|--help) # Missing host parameter. ""|-h|--help) # Missing host parameter.
@ -87,13 +96,19 @@ case "$2" in
;; ;;
l|list) # List presets. l|list) # List presets.
printf "loading ...\r" printf "loading ...\r"
active_ps=$(active_preset "$wled_host")
curl_cmd "http://$wled_host/presets.json" | \ curl_cmd "http://$wled_host/presets.json" | \
# Split into lines - one line per preset. Append final newline. # Split into lines - one line per preset. Append final newline.
sed --sandbox 's/"\(1\?[0-9]\)"[[:space:]]*:/\n&/g; $a\ ' | \ sed --sandbox 's/"\(1\?[0-9]\)"[[:space:]]*:/\n&/g; $a\ ' | \
# Extract number and name of every preset. Prepend description. # Prepend description, extract number and name of every preset,
# match active preset, right-align numbers and finally select
# lines to print.
sed --sandbox -n \ sed --sandbox -n \
-e 's/^"\(1\?[0-9]\)"[[:space:]]*:.*"n"[[:space:]]*:[[:space:]]*"\([[:print:]]*\)".*$/\1: \2/p' \ -e "1i\\Presets on $wled_host:" \
-e "1i\\Presets on $wled_host:" -e 's/^"\(1\?[0-9]\)"[[:space:]]*:.*"n"[[:space:]]*:[[:space:]]*"\([[:print:]]*\)".*$/\1: \2/' \
-e "s/^$active_ps:.*/& */" \
-e 's/^[0-9]:/ &/' \
-e '/^[1 ]\?[0-9]: [[:print:]]\+/p'
exit $? exit $?
;; ;;
dump) # Dump JSON response from API. dump) # Dump JSON response from API.