Move waybar into its own role that sway depends on.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read memory usage in FreeBSD
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
|
||||
while true; do
|
||||
# TODO: Why not vm.stats.vm.v_page_size ? Are these the same?
|
||||
page_size=$(sysctl -n hw.pagesize)
|
||||
free_pages=$(sysctl -n vm.stats.vm.v_free_count)
|
||||
free_bytes=$((page_size * free_pages))
|
||||
total_pages=$(sysctl -n vm.stats.vm.v_page_count)
|
||||
free_percent=$((100 * free_pages / total_pages))
|
||||
text=""
|
||||
|
||||
if [ $free_bytes -ge $((1024 * 1024 * 1024)) ]; then
|
||||
text="$((free_bytes / 1024 / 1024 / 1024)) GiB"
|
||||
fi
|
||||
|
||||
tooltip="${free_percent}%"
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${text}",
|
||||
"tooltip":"${tooltip}",
|
||||
"percentage":${free_percent}
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
done
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read memory usage in Linux
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
|
||||
while true; do
|
||||
memory_usage=$(free --bytes --wide)
|
||||
total_bytes=$(grep 'Mem:' <<<"$memory_usage" | awk '{print $2}')
|
||||
free_bytes=$(grep 'Mem:' <<<"$memory_usage" | awk '{print $4}')
|
||||
|
||||
free_percent=$((100 * free_bytes / total_bytes))
|
||||
text=""
|
||||
|
||||
if [ $free_bytes -ge $((1024 * 1024 * 1024)) ]; then
|
||||
text="$((free_bytes / 1024 / 1024 / 1024)) GiB"
|
||||
fi
|
||||
|
||||
tooltip="${free_percent}%"
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${text}",
|
||||
"tooltip":"${tooltip}",
|
||||
"percentage":${free_percent}
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
done
|
||||
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read battery status in FreeBSD
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
# TODO: Which is better, apm or reading sysctls from hw.acpi.battery ?
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
|
||||
while true; do
|
||||
battery_percentage=$(apm -l)
|
||||
battery_status=$(apm -b)
|
||||
seconds_remaining=$(apm -t)
|
||||
class=""
|
||||
|
||||
if [ $battery_status -eq 3 ]; then
|
||||
if [ $battery_percentage -eq 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
fi
|
||||
elif [ $battery_percentage -le 20 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 40 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 60 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 80 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -lt 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
fi
|
||||
|
||||
if [ $battery_percentage -le 15 ]; then
|
||||
class="critical"
|
||||
elif [ $battery_percentage -le 30 ]; then
|
||||
class="warning"
|
||||
fi
|
||||
|
||||
if [ $seconds_remaining -eq -1 ]; then
|
||||
time_remaining="unknown time remaining."
|
||||
else
|
||||
time_remaining=$(printf '%dh:%dm:%ds\n' $((seconds_remaining/3600)) $((seconds_remaining%3600/60)) $((seconds_remaining%60)) )
|
||||
fi
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${battery_percentage}% ${battery_icon}",
|
||||
"tooltip":"${time_remaining}",
|
||||
"percentage":${battery_percentage},
|
||||
"class":"${class}"
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
done
|
||||
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read battery status in Linux
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
batteries=$(upower --enumerate | grep BAT)
|
||||
|
||||
function generate_battery_update {
|
||||
local battery_path=$1
|
||||
local battery_status=$(upower --show-info "$battery_path")
|
||||
local battery_state=$(grep 'state:' <<<"$battery_status" | awk '{print $2}')
|
||||
local battery_percentage=$(grep 'percentage:' <<<"$battery_status" | awk '{print $2}')
|
||||
battery_percentage=${battery_percentage%\%}
|
||||
local time_remaining=$(grep 'time to empty:' <<<"$battery_status" | sed 's/\W*time to empty:\W*//g')
|
||||
local tooltip="$time_remaining until empty"
|
||||
if [ -z "$time_remaining" ]; then
|
||||
time_remaining=$(grep 'time to full:' <<<"$battery_status" | sed 's/\W*time to full:\W*//g')
|
||||
tooltip="$time_remaining until full"
|
||||
fi
|
||||
local battery_icon=""
|
||||
local class=""
|
||||
|
||||
if [ $battery_state = "charging" ]; then
|
||||
if [ $battery_percentage -eq 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
fi
|
||||
elif [ $battery_percentage -le 20 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 40 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 60 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 80 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -lt 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
fi
|
||||
|
||||
if [ $battery_percentage -le 15 ]; then
|
||||
class="critical"
|
||||
elif [ $battery_percentage -le 30 ]; then
|
||||
class="warning"
|
||||
fi
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${battery_percentage}% ${battery_icon}",
|
||||
"tooltip":"$tooltip",
|
||||
"percentage":${battery_percentage},
|
||||
"class":"${class}"
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
while read bat; do
|
||||
generate_battery_update "$bat"
|
||||
done<<<"$batteries"
|
||||
|
||||
upower --monitor | grep --line-buffered 'device changed:' | while read l; do
|
||||
while read bat; do
|
||||
generate_battery_update "$bat"
|
||||
done<<<"$batteries"
|
||||
done
|
||||
108
ansible/roles/waybar/files/waybar_scripts/waybar_custom_clock.py
Normal file
108
ansible/roles/waybar/files/waybar_scripts/waybar_custom_clock.py
Normal file
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Custom waybar module for clocks. Implemented because the official clock module was downloading timezone definitions on every boot.
|
||||
import datetime
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
from typing import Final, List, Optional
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
INTERVAL: Final[int] = 10
|
||||
LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
|
||||
|
||||
|
||||
@dataclass
|
||||
class Update:
|
||||
text: Optional[str]
|
||||
alt: Optional[str]
|
||||
tooltip: Optional[str]
|
||||
css_class: Optional[List[str]]
|
||||
percentage: Optional[str]
|
||||
|
||||
def dump(self) -> str:
|
||||
# Dump a dict because we can't name our member variable "class"
|
||||
return json.dumps(
|
||||
{
|
||||
k: v
|
||||
for k, v in {
|
||||
"text": self.text,
|
||||
"alt": self.alt,
|
||||
"tooltip": self.tooltip,
|
||||
"class": self.css_class,
|
||||
"percentage": self.percentage,
|
||||
}.items()
|
||||
if v is not None
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def make_calendar(today: datetime.date, tz: datetime.tzinfo):
|
||||
width: Final[int] = 20
|
||||
start_of_month = today.replace(day=1)
|
||||
month_header = today.strftime("%B %Y")
|
||||
padding = width - len(month_header)
|
||||
right_padding = " " * (padding // 2)
|
||||
left_padding = right_padding + (" " if padding % 2 == 1 else "")
|
||||
header = f"{left_padding}{month_header}{right_padding}"
|
||||
days_of_week = "Su Mo Tu We Th Fr Sa"
|
||||
|
||||
timezone_str = str(tz)
|
||||
|
||||
# Make the grid
|
||||
first_day_padding = " " * (
|
||||
0 if start_of_month.weekday() == 6 else (3 * (1 + start_of_month.weekday())) - 1
|
||||
)
|
||||
output = f"{header}\n{days_of_week}\n{first_day_padding}"
|
||||
|
||||
print_day = start_of_month
|
||||
while print_day.month == today.month:
|
||||
if print_day.weekday() == 6:
|
||||
output += "\n"
|
||||
else:
|
||||
output += " "
|
||||
if print_day == today:
|
||||
output += '<span foreground="#323232" background="#CCCCCC">'
|
||||
output += f"{print_day.day: >2}"
|
||||
if print_day == today:
|
||||
output += "</span>"
|
||||
print_day += datetime.timedelta(days=1)
|
||||
|
||||
output += f'\n<span size="small">{timezone_str}</span>'
|
||||
return output
|
||||
|
||||
|
||||
def main():
|
||||
tz: Optional[datetime.tzinfo] = (
|
||||
ZoneInfo(sys.argv[1]) if len(sys.argv) >= 2 else LOCAL_TIMEZONE
|
||||
)
|
||||
if tz is None:
|
||||
raise Exception("Failed to detect timezone.")
|
||||
|
||||
next_update = time.time()
|
||||
while True:
|
||||
time_before_next_update = next_update - time.time()
|
||||
if time_before_next_update > 0:
|
||||
time.sleep(time_before_next_update)
|
||||
next_update = time.time() + INTERVAL
|
||||
|
||||
now = datetime.datetime.now(tz=tz)
|
||||
text = now.strftime("%Y-%m-%d %H:%M")
|
||||
tooltip = make_calendar(now.date(), tz)
|
||||
|
||||
out = Update(
|
||||
text=text,
|
||||
alt="foo",
|
||||
tooltip=f"<tt>{tooltip}</tt>",
|
||||
css_class=["foo"],
|
||||
percentage="100",
|
||||
)
|
||||
|
||||
print(out.dump(), flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read volume status in FreeBSD
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
|
||||
while true; do
|
||||
current_vol_mixer=$(mixer -S vol)
|
||||
vol_left=$(cut -d ':' -f 2 <<<"$current_vol_mixer")
|
||||
vol_right=$(cut -d ':' -f 3 <<<"$current_vol_mixer")
|
||||
avg_vol=$(((vol_left + vol_right) / 2))
|
||||
tooltip="<tt>$(mixer)</tt>"
|
||||
class=""
|
||||
icon=""
|
||||
|
||||
if [ $avg_vol -eq 0 ]; then
|
||||
icon="🔇"
|
||||
elif [ $avg_vol -le 50 ]; then
|
||||
icon="🔉"
|
||||
else
|
||||
icon="🔊"
|
||||
fi
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${avg_vol}% ${icon}",
|
||||
"tooltip":"${tooltip//$'\n'/\\n}",
|
||||
"percentage":${avg_vol},
|
||||
"class":"${class}"
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
done
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read volume status in Linux
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
|
||||
while true; do
|
||||
current_vol_decimal=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | sed 's/Volume: //g')
|
||||
if [[ "$current_vol_decimal" = *MUTED* ]]; then
|
||||
current_vol_percent=0
|
||||
else
|
||||
current_vol_percent=$(bc -s <<<"$current_vol_decimal * 100")
|
||||
current_vol_percent=${current_vol_percent%.*} # Remove decimal
|
||||
fi
|
||||
tooltip="<tt>$(wpctl status)</tt>"
|
||||
tooltip=${tooltip//$'\n'/\\n}
|
||||
tooltip=${tooltip//$'\t'/\\t}
|
||||
class=""
|
||||
icon=""
|
||||
|
||||
if [ $current_vol_percent -eq 0 ]; then
|
||||
icon="🔇"
|
||||
elif [ $current_vol_percent -le 50 ]; then
|
||||
icon="🔉"
|
||||
else
|
||||
icon="🔊"
|
||||
fi
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${current_vol_percent}% ${icon}",
|
||||
"tooltip":"${tooltip}",
|
||||
"percentage":${current_vol_percent},
|
||||
"class":"${class}"
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
done
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read temperature status in FreeBSD
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
|
||||
while true; do
|
||||
current_cpu_temps=$(sysctl 'dev.cpu' | grep -E 'dev\.cpu\.[0-9]+\.temperature')
|
||||
sum_temperature=0
|
||||
num_temperature=0
|
||||
max_temperature=0
|
||||
while read temp; do
|
||||
numeric_temp=$(echo "$temp" | cut -d ':' -f 2 | grep -oE '[0-9]+(\.[0-9]+)?')
|
||||
sum_temperature=$(bc -s <<<"$sum_temperature + $numeric_temp")
|
||||
num_temperature=$((num_temperature + 1))
|
||||
max_temperature=$(bc <<EOF
|
||||
define max(a,b){
|
||||
if(a>b)
|
||||
{
|
||||
return(a)
|
||||
}else{
|
||||
return(b)
|
||||
}
|
||||
}
|
||||
max($max_temperature,$numeric_temp)
|
||||
EOF
|
||||
)
|
||||
done<<<"$current_cpu_temps"
|
||||
avg_temperature=$(bc -s <<<"$sum_temperature / $num_temperature")
|
||||
tooltip="<tt>$current_cpu_temps</tt>"
|
||||
class=""
|
||||
icon="🌡"
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"${max_temperature}℃ ${icon}",
|
||||
"tooltip":"${tooltip//$'\n'/\\n}",
|
||||
"percentage":50,
|
||||
"class":"${class}"
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
done
|
||||
Reference in New Issue
Block a user