From: Jakub Czajka Date: Fri, 23 Dec 2022 19:27:18 +0000 (+0100) Subject: [home] Represent the status bar as a package. X-Git-Url: https://git.ekhem.eu.org/?a=commitdiff_plain;h=deb3398e3fbbdc5403dfae34c7d8cc82dd962078;p=guix.git [home] Represent the status bar as a package. This commit adds a script which generates the status bar. It also wraps this script as a GNU Guix package. --- diff --git a/bash/.config/profile.d/20-env.sh b/bash/.config/profile.d/20-env.sh index b1a7828..9b1acf9 100644 --- a/bash/.config/profile.d/20-env.sh +++ b/bash/.config/profile.d/20-env.sh @@ -4,4 +4,5 @@ # 20-env.sh - generic environment variables. export EDITOR="emacs" +export DISPLAY=":1" export PATH="${HOME}/.local/bin:${PATH}" diff --git a/conf/home/status-bar.scm b/conf/home/status-bar.scm new file mode 100644 index 0000000..868f400 --- /dev/null +++ b/conf/home/status-bar.scm @@ -0,0 +1,90 @@ +;; Copyright (c) 2022 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; status-bar.scm - program for a status bar. + +(define-module (conf home status-bar) + #:use-module (gnu home services) + #:use-module (gnu home services shepherd) + #:use-module (gnu packages base) + #:use-module (gnu packages gawk) + #:use-module (gnu packages linux) + #:use-module (gnu packages mail) + #:use-module (gnu packages xdisorg) + #:use-module (gnu packages xorg) + #:use-module (guix build-system trivial) + #:use-module (guix gexp) + #:use-module (guix licenses) + #:use-module (guix packages) + #:use-module (guix records) + #:use-module (ice-9 match) + #:export (status-bar + status-bar-configuration + status-bar-configuration? + status-bar-configuration-package + status-bar-service + status-bar-service-type)) + +(define status-bar + (package + (name "status-bar") + (version "1.0") + (source + (local-file + (string-concatenate + (list (getenv "GUIX_PACKAGE_PATH") + "/scripts/status_bar")))) + (build-system trivial-build-system) + (arguments + '(#:builder + (begin + (let* ((ins (assoc-ref %build-inputs "source")) + (out (assoc-ref %outputs "out")) + (file (string-append out "/status_bar"))) + (mkdir out) + (copy-file ins file) + (chmod file #o555))))) + (synopsis "Script which generates a status bar.") + (description "`status-bar` is a shell script which repeatedly prints a status +bar. The output should be redirected to the desktop.") + (home-page "https://git.ekhem.eu.org") + (license gpl3+))) + +(define-record-type* + status-bar-configuration make-status-bar-configuration + status-bar-configuration? + (package status-bar-configuration-package + (default status-bar))) + +(define status-bar-shepherd-service + (match-lambda + (($ package) + (shepherd-service + (provision '(status-bar)) + (start #~(make-forkexec-constructor + (list #$(file-append package "/status_bar")) + #:directory + (getenv "HOME") + #:log-file + (string-append (getenv "XDG_LOG_HOME") + "/status_bar.log"))) + (stop #~(make-kill-destructor)) + (documentation ""))))) + +(define (install-status-bar-dependencies config) + (list alsa-utils gawk notmuch sed xclip xsetroot)) + +(define status-bar-service-type + (service-type + (name 'status-bar-service) + (extensions + (list (service-extension home-profile-service-type + install-status-bar-dependencies) + (service-extension home-shepherd-service-type + (compose list + status-bar-shepherd-service)))) + (default-value (status-bar-configuration)) + (description "Shepherd service for the `status-bar`."))) + +(define status-bar-service + (service status-bar-service-type)) diff --git a/scripts/status_bar b/scripts/status_bar new file mode 100755 index 0000000..1aa4da3 --- /dev/null +++ b/scripts/status_bar @@ -0,0 +1,105 @@ +#!/bin/sh +# Copyright (c) 2022 Jakub Czajka +# License: GPL-3.0 or later. + +OLD_UNREAD=0 +OLD_DOWNLOAD=0 +OLD_UPLOAD=0 + +clock() { + echo "📅 $(date "+%T")" +} + +day() { + echo "⏰ $(date "+%d-%m-%Y")" +} + +volume() { + VOLUME=$(amixer | awk -F"[][]" '/dB/ { print $2 }' | head -n1) + if [ "${VOLUME}" = "0%" ]; then + echo "🔇 ${VOLUME}" + else + echo "🔊 ${VOLUME}" + fi +} + +battery() { + BATTERY_DEVICE="/sys/class/power_supply/BAT0" + + if [ -d "${BATTERY_DEVICE}" ] + then + STATUS=$(cat "${BATTERY_DEVICE}/status") + LEVEL=$(cat "${BATTERY_DEVICE}/capacity") + else + STATUS="Disconnected" + LEVEL="-" + fi + + case "${STATUS}" in + "Disconnected") + BATTERY_ICON="🔋" + BATTERY_LEVEL=" n/a" + ;; + "Charging") + BATTERY_ICON="⬆🔋" + ;; + "Discharging") + BATTERY_ICON="⬇🔋" + ;; + *) + esac + + echo "${BATTERY_ICON}${BATTERY_LEVEL}" +} + +session_duration() { + UPTIME=$(cut --delimiter=. --field=1 /proc/uptime) + + echo "⌛ $(date --date=@${UPTIME} --universal +%T)" +} + +unread() { + NEW_UNREAD=$(notmuch search --output=files tag:unread | wc --lines) + MESSAGES="$((${NEW_UNREAD} - ${OLD_UNREAD}))" + [ "${MESSAGES}" -gt "0" ] && notify-send "📧 got ${MESSAGES} new message(s)" + OLD_UNREAD=${NEW_UNREAD} + + echo "📬 ${OLD_UNREAD}" +} + +language() { + CODE=$(echo ${LANG} | sed -e "s/^[a-z\_]*//" -e "s/\..*$//g") + echo "🏳 ${CODE}" +} + +bandwidth() { + for INTERFACE in /sys/class/net/* + do + DOWNLOAD_NEW=$(cat "${INTERFACE}/statistics/rx_bytes") + DOWNLOAD="$((${DOWNLOAD} + ${DOWNLOAD_NEW}))" + + UPLOAD_NEW=$(cat "${INTERFACE}/statistics/tx_bytes") + UPLOAD="$((${UPLOAD} + ${UPLOAD_NEW}))" + done + + NEW_DOWNLOAD=$(echo ${DOWNLOAD} | cut --delimiter=" " --fields=2) + DOWNLOAD="$((${NEW_DOWNLOAD} - ${OLD_DOWNLOAD}))" + OLD_DOWNLOAD=${NEW_DOWNLOAD} + + echo "🌐 ${DOWNLOAD}B/s" +} + +while true +do + TIME=`clock` + DATE=`day` + VOLUME=`volume` + BATTERY=`battery` + UPTIME=`session_duration` + #UNREAD=`unread` + CODE=`language` + BANDWIDTH=`bandwidth` + + xsetroot -name "${BANDWIDTH} ${CODE} ${UPTIME} ${BATTERY} ${VOLUME} ${DATE} ${TIME}" + sleep 1 +done