feat: optimized sbar, gammastep asia, wpdef

This commit is contained in:
ange 2025-04-30 02:41:45 +00:00
parent f48e958d44
commit 395119a0fe
Signed by: ange
GPG key ID: 9E0C4157BB7BEB1D
19 changed files with 159 additions and 157 deletions

View file

@ -4,46 +4,30 @@ import http.client as http
import json
# curl -sS https://api.coingecko.com/api/v3/coins/list
COINS = [
# [id, symbol]
["monero", "XMR"],
["bitcoin", "BTC"],
]
COINS = ["monero", "bitcoin"]
CURRENCY = "usd"
FORMAT = "{coin}=${price:.0f}"
def get_btc_fees() -> str:
url = "mempool.space"
path = "/api/v1/fees/mempool-blocks"
client = http.HTTPSConnection(url, timeout=10)
client.request("GET", path)
# https://mempool.space/docs/api/rest#get-mempool-blocks-fees
response = json.loads(client.getresponse().read())
return f"{int(response[0]['medianFee'])} sat/vB"
def get_coins_values() -> str:
ids = ",".join([coin[0] for coin in COINS])
url = "api.coingecko.com"
path = f"/api/v3/simple/price?ids={ids}&vs_currencies={CURRENCY}"
client = http.HTTPSConnection(url, timeout=10)
client.request("GET", path)
# https://www.coingecko.com/api/documentation
prices = json.loads(client.getresponse().read())
return " ".join(
FORMAT.format(coin=coin[1], price=prices[coin[0]][CURRENCY])
for coin in COINS
)
FORMAT = "{symbol}=${price:.0f} {trend:+.1f}%"
# https://docs.coingecko.com/v3.0.1/reference/coins-id
def main():
print(f"{get_btc_fees()} {get_coins_values()}")
path = "/api/v3/coins/{id}?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
client = http.HTTPSConnection("api.coingecko.com", timeout=10)
strings = []
for c in COINS:
client.request("GET", path.format(id=c))
j = json.loads(client.getresponse().read())
strings.append(
FORMAT.format(
symbol=j["symbol"].upper(),
price=j["market_data"]["current_price"][CURRENCY],
trend=j["market_data"]["price_change_percentage_24h"],
)
)
print(" - ".join(strings))
if __name__ == "__main__":

View file

@ -1,127 +1,117 @@
#!/bin/bash
function update_crypto() {
function scrypto() {
local c
c="$(crypto 2> /dev/null)" && echo "$c" > "$SBAR/crypto"
c="$(crypto 2> /dev/null)" && echo "$c" > crypto
}
function update_cpu() {
echo " $(awk '{print $1}' /proc/loadavg)" > "$SBAR/cpu"
function scpu() {
echo " $(awk '{print $1}' /proc/loadavg)" > cpu
}
function update_memory() {
echo " $(free -h | awk '/^Mem:/{print $3}')" > "$SBAR/memory"
function smemory() {
echo " $(free -h | awk '/^Mem:/{print $3}')" > memory
}
function update_bat() {
function sbat() {
local cap; cap="$(cat /sys/class/power_supply/BAT0/capacity)"
if grep -qv Discharging /sys/class/power_supply/BAT0/status; then
echo " $cap%" > "$SBAR/bat"
echo " $cap%"
else
echo " $cap%" > "$SBAR/bat"
fi
echo " $cap%"
fi > bat
}
function update_sink_vol() {
local vol mute
function svol() {
local st v vol=('' ' ') mut=('🔇' ' ') m
read -r _ vol mute < <(wpctl get-volume '@DEFAULT_AUDIO_SINK@') || return
if [ -n "$mute" ]; then
echo '🔇' > "$SBAR/sink"
else
echo " $((10#${vol/./}))" > "$SBAR/sink"
mapfile -t st <<< "$(wpctl status | sed -n 's/.*\*.*\[vol: \(.*\)]/\1/p')"
if [ -z "${st[0]}" ]; then
return > vol
fi
for i in "${!vol[@]}"; do
read -r v m <<< "${st[$i]}"
if [ -n "$m" ]; then
echo -n "${mut[$i]}"
else
echo -n "${vol[$i]} $((10#${v/./}))"
fi
done > vol
}
function update_source_vol() {
local vol mute
read -r _ vol mute < <(wpctl get-volume '@DEFAULT_AUDIO_SOURCE@') || return
if [ -n "$mute" ]; then
echo '' > "$SBAR/source"
else
echo " $((10#${vol/./}))" > "$SBAR/source"
fi
}
function update_net() {
function snet() {
local dev j ssid sig
dev="$(ip route | grep -oP 'default.*dev\s+\K[^\s]+')"
j="$(networkctl status "$dev" --json=short)"
case "$(jq -r .Type <<< "$j")" in
ether)
echo "🌐 $dev" > "$SBAR/net"
echo "🌐 $dev"
;;
wlan)
ssid="$(jq -r .SSID <<< "$j")"
sig="$(awk "/$dev/{printf \"%.0f\", \$3}" /proc/net/wireless)"
if [ "$sig" -gt 56 ]; then echo "▂▄▆█ $ssid" > "$SBAR/net"
elif [ "$sig" -gt 38 ]; then echo "▂▄▆_ $ssid" > "$SBAR/net"
elif [ "$sig" -gt 21 ]; then echo "▂▄__ $ssid" > "$SBAR/net"
elif [ "$sig" -gt 3 ]; then echo "▂___ $ssid" > "$SBAR/net"
else echo "____ $ssid" > "$SBAR/net"
if [ "$sig" -gt 56 ]; then echo "▂▄▆█ $ssid"
elif [ "$sig" -gt 38 ]; then echo "▂▄▆_ $ssid"
elif [ "$sig" -gt 21 ]; then echo "▂▄__ $ssid"
elif [ "$sig" -gt 3 ]; then echo "▂___ $ssid"
else echo "____ $ssid"
fi
;;
*)
echo '⚠' > "$SBAR/net"
echo '⚠'
;;
esac
esac > net
}
function update_vpn() {
function svpn() {
local v; v="$(ip -br link show type wireguard | awk '{print $1}')"
if [ -n "$v" ]; then
echo " 🔒 $v" > "$SBAR/vpn"
else
: > "$SBAR/vpn"
fi
echo " 🔒 $v"
fi > vpn
}
function update_time() {
echo "$(TZ=Asia/Makassar date '+%R %Z') - $(date -u '+%a %m-%d %R')" \
> "$SBAR/time"
function stime() {
# shellcheck disable=SC2238
TZ=Asia/Makassar date '+%a %m-%d %R %Z' > time
}
function display() {
for f in "$SBAR"/*; do
local "$(basename "$f")"="$(cat "$f")"
for f in *; do
local - "$f"="$(cat "$f")"
done
# shellcheck disable=SC2154
xsetroot -name "$crypto | $cpu | $memory | $sink $source | $net$vpn | $bat | $time"
# shellcheck disable=SC2128,SC2154
xsetroot -name "$crypto | $cpu | $memory | $vol | $net$vpn | $bat | $time"
}
# SIGNALING
# trap '<function>;display;wait -n' 'RTMIN+n'
trap 'update_sink_vol;display;wait -n' 'RTMIN+0'
trap 'update_source_vol;display;wait -n' 'RTMIN+1'
trap 'update_net;display;wait -n' 'RTMIN+2'
trap 'update_vpn;display;wait -n' 'RTMIN+3'
# trap '<function>;display;wait' 'RTMIN+n'
trap 'svol;display;wait' 'RTMIN+0'
trap 'snet;display;wait' 'RTMIN+1'
trap 'svpn;display;wait' 'RTMIN+2'
# to update it from external commands
## kill -m "$(cat "$XDG_CACHE_HOME/pidofbar")"
# where m = 34 + n
SBAR="$XDG_CACHE_HOME/sbar"
mkdir -p "$SBAR"
echo "$$" > "$SBAR/pid"
mkdir -p "$XDG_RUNTIME_DIR/sbar" && cd "$XDG_RUNTIME_DIR/sbar" || exit 1
echo "$$" > pid
(while :; do
update_crypto
sleep $((300 - 10#$(date '+%S') % 300)) & wait -n
scrypto
sleep $((300 - 10#$(date '+%S') % 300)) & wait
done &)
while true; do
update_cpu
update_memory
update_sink_vol
update_source_vol
update_net
update_vpn
update_bat
update_time
scpu
smemory
svol
snet
svpn
sbat
stime
display
sleep $((5 - 10#$(date '+%S') % 5)) & wait -n
sleep $((5 - 10#$(date '+%S') % 5)) & wait
done

View file

@ -3,50 +3,43 @@
case "$1" in
s)
DEV='@DEFAULT_AUDIO_SINK@'
SIG=34
;;
m)
DEV='@DEFAULT_AUDIO_SOURCE@'
SIG=35
;;
*)
exit 1
;;
esac
shift
function update_vol() {
case "$1" in
+*|-*)
# shellcheck disable=SC2017
CUR=$((CUR + $1 - CUR % $1 + CUR % $1 * 10 / $1 / 5 * $1))
if [ "$CUR" -lt 0 ]; then
CUR=0
elif [ "$CUR" -gt 300 ]; then
CUR=300
fi
;;
*)
CUR="$1"
esac
wpctl set-volume "$DEV" "$CUR%"
wpctl set-volume -l 3 "$DEV" "$CUR%"
}
read -r _ CUR MUTE < <(wpctl get-volume "$DEV")
CUR=$((10#${CUR/./}))
if [ -z "$1" ]; then
if [ -z "$2" ]; then
echo "$CUR"
exit
elif [ "$1" == 'm' ] && [ -z "$MUTE" ]; then
fi
if [ "$2" == 'm' ] && [ -z "$MUTE" ]; then
wpctl set-mute "$DEV" 1
else
if [[ "$1" =~ [0-9] ]]; then
update_vol "$1"
if [[ "$2" =~ [0-9] ]]; then
update_vol "$2"
fi
if [ -n "$MUTE" ]; then
wpctl set-mute "$DEV" 0
fi
fi
kill "-$SIG" "$(cat "$XDG_CACHE_HOME/sbar/pid")"
kill -34 "$(cat "$XDG_RUNTIME_DIR/sbar/pid")"

View file

@ -9,7 +9,7 @@ fi
if [ "$EUID" != 0 ]; then
exec "$SUDO" env DISPLAY="$DISPLAY" XAUTHORITY="$XAUTHORITY" \
XDG_CACHE_HOME="$XDG_CACHE_HOME" "$BASH" -- "${BASH_SOURCE[0]}" "$@"
XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" "$BASH" -- "${BASH_SOURCE[0]}" "$@"
fi
if [ "$1" != off ]; then
@ -23,4 +23,4 @@ while read -r i; do
wg-quick down "$i"
done < <(ip -br link show type wireguard | awk "\$1 != \"$1\"{print \$1}")
kill -37 "$(cat "$XDG_CACHE_HOME/sbar/pid")"
kill -36 "$(cat "$XDG_RUNTIME_DIR/sbar/pid")"

View file

@ -9,7 +9,7 @@ fi
for _ in {0..29}; do
if "${iwctl[@]}" show | grep -q '\s*State\s\+connected\s*$'; then
kill -36 "$(cat "$XDG_CACHE_HOME/sbar/pid")"
kill -35 "$(cat "$XDG_RUNTIME_DIR/sbar/pid")"
exit 0
fi
sleep 1

32
.local/bin/wpdef Executable file
View file

@ -0,0 +1,32 @@
#!/bin/bash -e
json="$(pw-dump)"
function getdevs() {
jq -r ".[] | select(.info.props.\"media.class\" == \"Audio/$1\") | \"\(.id) \(.info.props.\"node.nick\")\"" <<< "$json"
}
function getdefault() {
jq -r ".[].metadata | select(.) | .[] | select(.key == \"default.configured.audio.$1\") | .value.name" <<< "$json"
}
function getnick() {
jq -r ".[].info.props | select(.\"node.name\" == \"$1\") | .\"node.nick\"" <<< "$json"
}
case "$1" in
s)
CLASS=Sink
;;
m)
CLASS=Source
;;
*)
exit 1
;;
esac
read -r id <<< "$(getdevs "$CLASS" | dmenu -l 16)"
wpctl set-default "$id"
kill -34 "$(cat "$XDG_RUNTIME_DIR/sbar/pid")"