Eww update
This commit is contained in:
27
modules/home/wayland/apps/eww/bar/scripts/net/bluetooth
Executable file
27
modules/home/wayland/apps/eww/bar/scripts/net/bluetooth
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
emit() {
|
||||
local powered=false connected=false device=""
|
||||
|
||||
if bluetoothctl show 2>/dev/null | grep -q "Powered: yes"; then
|
||||
powered=true
|
||||
while IFS= read -r line; do
|
||||
mac=$(echo "$line" | awk '{ print $2 }')
|
||||
info=$(bluetoothctl info "$mac" 2>/dev/null)
|
||||
if echo "$info" | grep -q "Connected: yes"; then
|
||||
device=$(echo "$info" | awk -F': ' '/^\tName:/ { print $2; exit }')
|
||||
connected=true
|
||||
break
|
||||
fi
|
||||
done < <(bluetoothctl devices 2>/dev/null)
|
||||
fi
|
||||
|
||||
printf '{"powered":%s,"connected":%s,"device":"%s"}\n' "$powered" "$connected" "$device"
|
||||
}
|
||||
|
||||
emit
|
||||
bluetoothctl monitor 2>/dev/null | while IFS= read -r line; do
|
||||
case "$line" in
|
||||
*"Powered"*|*"Connected"*|*"Device"*) emit ;;
|
||||
esac
|
||||
done
|
||||
@@ -1,81 +1,47 @@
|
||||
#!/usr/bin/env zsh
|
||||
#!/usr/bin/env bash
|
||||
|
||||
function get_time_ms {
|
||||
date -u +%s%3N
|
||||
}
|
||||
|
||||
icons=("" "" "" "" "")
|
||||
|
||||
function get_wifi_interface() {
|
||||
get_wifi_iface() {
|
||||
awk 'NR > 2 { gsub(":", "", $1); print $1; exit }' /proc/net/wireless
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
status=$(rfkill | grep wlan | awk '{print $4}')
|
||||
|
||||
if [ "$status" = "unblocked" ]; then
|
||||
rfkill block wlan
|
||||
else
|
||||
rfkill unblock wlan
|
||||
fi
|
||||
signal_icon() {
|
||||
local dbm="$1"
|
||||
if [ -z "$dbm" ]; then echo ""; return; fi
|
||||
if [ "$dbm" -ge -50 ]; then echo ""
|
||||
elif [ "$dbm" -ge -60 ]; then echo ""
|
||||
elif [ "$dbm" -ge -70 ]; then echo ""
|
||||
elif [ "$dbm" -ge -80 ]; then echo ""
|
||||
else echo ""; fi
|
||||
}
|
||||
|
||||
function gen_wifi() {
|
||||
wifi_iface=$(get_wifi_interface)
|
||||
signal=$(awk -v iface="$wifi_iface" '$1 == iface ":" { print $3; exit }' /proc/net/wireless)
|
||||
level=$(awk -v n="$signal" 'BEGIN{print int((n-1)/20)}')
|
||||
if [ "$level" -gt 4 ]; then
|
||||
level=4
|
||||
make_content() {
|
||||
local wifi_iface eth_iface
|
||||
|
||||
wifi_iface=$(get_wifi_iface)
|
||||
eth_iface=$(ip link | awk '/^[0-9]+: en[po]/ { gsub(":",""); print $2; exit }')
|
||||
|
||||
# Ethernet
|
||||
local eth_connected=false
|
||||
if [ -n "$eth_iface" ]; then
|
||||
eth_state=$(ip link show "$eth_iface" 2>/dev/null | awk '/state/ { print $9 }')
|
||||
[ "$eth_state" = "UP" ] && eth_connected=true
|
||||
fi
|
||||
|
||||
icon=${icons[$level]}
|
||||
ip="-"
|
||||
class="net-connected"
|
||||
name_raw=$(wpa_cli -g "/run/wpa_supplicant/$wifi_iface" status | grep \^ssid= | sed 's/ssid=//g')
|
||||
name=$(printf "%s" "$name_raw")
|
||||
}
|
||||
|
||||
function gen_ethernet() {
|
||||
icon=""
|
||||
class="net-connected"
|
||||
ip=""
|
||||
name=Wired
|
||||
}
|
||||
|
||||
function make_content() {
|
||||
local ethernet wifi wifi_iface
|
||||
ethernet=$(ip link | rg "^[0-9]+: en[po]+" | head -n1 | sed 's/[a-zA-Z0-9_,><:\ -]*state //g' | sed 's/ mode [a-zA-Z0-9 ]*//g')
|
||||
wifi_iface=$(get_wifi_interface)
|
||||
if [ -n "$wifi_iface" ]; then
|
||||
wifi=$(wpa_cli -g "/run/wpa_supplicant/$wifi_iface" status | rg "^wpa_state=" | sed 's/wpa_state=//g')
|
||||
# WiFi — use IP presence as connection indicator (more reliable than wpa_cli)
|
||||
local wifi_connected=false wifi_icon="" wifi_ssid=""
|
||||
if [ -n "$wifi_iface" ] && ip -4 addr show "$wifi_iface" 2>/dev/null | grep -q "inet "; then
|
||||
wifi_connected=true
|
||||
wifi_ssid=$(wpa_cli -g "/run/wpa_supplicant/$wifi_iface" status 2>/dev/null \
|
||||
| awk -F= '/^ssid=/ { print $2 }')
|
||||
signal=$(awk -v iface="$wifi_iface" '$1 == iface ":" { gsub(/\./, "", $4); print $4; exit }' /proc/net/wireless)
|
||||
wifi_icon=$(signal_icon "$signal")
|
||||
fi
|
||||
|
||||
# test ethernet first
|
||||
if [[ $ethernet == "UP" ]]; then
|
||||
gen_ethernet
|
||||
elif [[ $wifi == "COMPLETED" ]]; then
|
||||
gen_wifi
|
||||
else
|
||||
icon=""
|
||||
ip="-"
|
||||
class="net-disconnected"
|
||||
name="Disconnected"
|
||||
fi
|
||||
|
||||
echo '{"icon": "'$icon'", "name": "'$name'", "ip": "'$ip'", "class": "'$class'"}'
|
||||
printf '{"wifi":{"connected":%s,"icon":"%s","ssid":"%s"},"ethernet":{"connected":%s}}\n' \
|
||||
"$wifi_connected" "$wifi_icon" "$wifi_ssid" "$eth_connected"
|
||||
}
|
||||
|
||||
if [ "$1" = "toggle" ]; then
|
||||
toggle
|
||||
else
|
||||
last_time=$(get_time_ms)
|
||||
make_content
|
||||
ip monitor | while read -r _; do
|
||||
make_content
|
||||
ip monitor | while read -r _; do
|
||||
current_time=$(get_time_ms)
|
||||
delta=$((current_time - last_time))
|
||||
if [[ $delta -gt 50 ]]; then
|
||||
make_content
|
||||
last_time=$(get_time_ms)
|
||||
fi
|
||||
done
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user