From 14e6e78aee673d265e458883c41a696b64063607 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 21 Dec 2024 16:25:40 -0500 Subject: [PATCH] Add the waybar scripts. --- nix/configuration/roles/waybar/default.nix | 62 ++++++++++ .../waybar_available_memory_linux.bash | 32 ++++++ .../waybar_scripts/waybar_battery_linux.bash | 80 +++++++++++++ .../waybar_scripts/waybar_custom_clock.py | 107 ++++++++++++++++++ .../waybar_scripts/waybar_night_mode.bash | 93 +++++++++++++++ .../waybar_scripts/waybar_sound_linux.bash | 41 +++++++ .../waybar_temperature_linux.bash | 56 +++++++++ 7 files changed, 471 insertions(+) create mode 100644 nix/configuration/roles/waybar/files/waybar_scripts/waybar_available_memory_linux.bash create mode 100644 nix/configuration/roles/waybar/files/waybar_scripts/waybar_battery_linux.bash create mode 100644 nix/configuration/roles/waybar/files/waybar_scripts/waybar_custom_clock.py create mode 100644 nix/configuration/roles/waybar/files/waybar_scripts/waybar_night_mode.bash create mode 100644 nix/configuration/roles/waybar/files/waybar_scripts/waybar_sound_linux.bash create mode 100644 nix/configuration/roles/waybar/files/waybar_scripts/waybar_temperature_linux.bash diff --git a/nix/configuration/roles/waybar/default.nix b/nix/configuration/roles/waybar/default.nix index 3720340..daf5881 100644 --- a/nix/configuration/roles/waybar/default.nix +++ b/nix/configuration/roles/waybar/default.nix @@ -4,14 +4,76 @@ pkgs, ... }: +let + waybar_available_memory = + (pkgs.writeScriptBin "waybar_custom_available_memory" ( + builtins.readFile ./files/waybar_scripts/waybar_available_memory_linux.bash + )).overrideAttrs + (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + }); + waybar_battery = + (pkgs.writeScriptBin "waybar_custom_battery" ( + builtins.readFile ./files/waybar_scripts/waybar_battery_linux.bash + )).overrideAttrs + (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + + }); + waybar_clock = + (pkgs.writeScriptBin "waybar_custom_clock" ( + builtins.readFile ./files/waybar_scripts/waybar_custom_clock.py + )).overrideAttrs + (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + + }); + waybar_night_mode = + (pkgs.writeScriptBin "waybar_night_mode" ( + builtins.readFile ./files/waybar_scripts/waybar_night_mode.bash + )).overrideAttrs + (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + + }); + waybar_sound = + (pkgs.writeScriptBin "waybar_custom_sound" ( + builtins.readFile ./files/waybar_scripts/waybar_sound_linux.bash + )).overrideAttrs + (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + + }); + waybar_temperature = + (pkgs.writeScriptBin "waybar_custom_temperature" ( + builtins.readFile ./files/waybar_scripts/waybar_temperature_linux.bash + )).overrideAttrs + (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + + }); +in { imports = [ ]; environment.systemPackages = with pkgs; [ waybar + waybar_available_memory + waybar_battery + waybar_clock + waybar_night_mode + waybar_sound + waybar_temperature + python3 # for clock TODO python should not be in the system packages, maybe switch to a venv? ref https://nixos.wiki/wiki/Python + bc # for temperature and sound + jq # for memory, battery, sound, night mode, and temperature + upower # for battery + wlsunset # for night mode ]; + services.upower.enable = true; # for battery + home-manager.users.talexander = { pkgs, ... }: { diff --git a/nix/configuration/roles/waybar/files/waybar_scripts/waybar_available_memory_linux.bash b/nix/configuration/roles/waybar/files/waybar_scripts/waybar_available_memory_linux.bash new file mode 100644 index 0000000..3fdf8d0 --- /dev/null +++ b/nix/configuration/roles/waybar/files/waybar_scripts/waybar_available_memory_linux.bash @@ -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 < 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 += '' + output += f"{print_day.day: >2}" + if print_day == today: + output += "" + print_day += datetime.timedelta(days=1) + + output += f'\n{timezone_str}' + 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 = 60 * (1 + int(time.time()) // 60) + + now = datetime.datetime.now(tz=tz) + text = now.strftime("%Y-%m-%d %H:%M:%S") + tooltip = make_calendar(now.date(), tz) + + out = Update( + text=text, + alt="foo", + tooltip=f"{tooltip}", + css_class=["foo"], + percentage="100", + ) + + print(out.dump(), flush=True) + + +if __name__ == "__main__": + main() diff --git a/nix/configuration/roles/waybar/files/waybar_scripts/waybar_night_mode.bash b/nix/configuration/roles/waybar/files/waybar_scripts/waybar_night_mode.bash new file mode 100644 index 0000000..847cdb9 --- /dev/null +++ b/nix/configuration/roles/waybar/files/waybar_scripts/waybar_night_mode.bash @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +# +# Power an icon for tinting the screen at night. +set -euo pipefail +IFS=$'\n\t' +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +SLEEP_INTERVAL=${SLEEP_INTERVAL:-30} + +# ◓◒●◌◎ +# 🔴🔵🟠🟡🟢🟣🟤 +# 🟥🟦🟧🟨🟩🟪🟫 +# ☀☯⭐🌝🌞⏾ +# 🌑🌓🌗🌕 +# 👓 + +############## Setup ######################### + +function cleanup { + log "Killing child process $wlsunset_pid" + kill "$wlsunset_pid" + true +} +for sig in EXIT; do + trap "set +e; cleanup" "$sig" +done + +function die { + local status_code="$1" + shift + (>&2 echo "${@}") + exit "$status_code" +} + +function log { + (>&2 echo "${@}") +} + +############## Program ######################### + +function main { + local night_mode_icon night_mode_text night_mode_class + night_mode_mode="auto" + night_mode_class="" + wlsunset -S 07:00 -s 22:00 & + wlsunset_pid=$! + + while true; do + if [ "$night_mode_mode" == "auto" ]; then + night_mode_icon="🌗" + night_mode_text="auto" + elif [ "$night_mode_mode" == "on" ]; then + night_mode_icon="🌑" + night_mode_text="night" + elif [ "$night_mode_mode" == "off" ]; then + night_mode_icon="🌕" + night_mode_text="day" + fi + + render + sleep "$SLEEP_INTERVAL" & + wait $! || true + done +} + +function render { + jq --unbuffered --compact-output <b) + { + return(a) + }else{ + return(b) + } +} +max($max_temperature,$numeric_temp) +EOF + ) + done<<<$(print_all_x86_pkg_temps) + avg_temperature=$(bc -s <<<"$sum_temperature / $num_temperature") + tooltip="" + class="" + icon="🌡" + + jq --unbuffered --compact-output <