]> git.ekhem.eu.org Git - guix.git/commitdiff
[home] Represent the status bar as a package.
authorJakub Czajka <jakub@ekhem.eu.org>
Fri, 23 Dec 2022 19:27:18 +0000 (20:27 +0100)
committerJakub Czajka <jczajka@google.com>
Sun, 24 Dec 2023 18:53:07 +0000 (19:53 +0100)
This commit adds a script which generates the status bar. It also
wraps this script as a GNU Guix package.

bash/.config/profile.d/20-env.sh
conf/home/status-bar.scm [new file with mode: 0644]
scripts/status_bar [new file with mode: 0755]

index b1a7828167bbf79edff0fa49da72cc0cd35ff947..9b1acf9d93aa4acaef5ff2489fe76251b5227494 100644 (file)
@@ -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 (file)
index 0000000..868f400
--- /dev/null
@@ -0,0 +1,90 @@
+;; Copyright (c) 2022 Jakub Czajka <jakub@ekhem.eu.org>
+;; 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>
+  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
+   (($ <status-bar-configuration> 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 (executable)
index 0000000..1aa4da3
--- /dev/null
@@ -0,0 +1,105 @@
+#!/bin/sh
+# Copyright (c) 2022 Jakub Czajka <jakub@ekhem.eu.org>
+# 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