From 29cc473d59c68b315b5e59ed21c6278a65c251a2 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 22 Jul 2023 12:21:54 +0300 Subject: feat(tazjin/emacs): modeline warning if tty sessions are logged in Change-Id: I88a9ca299f82917fb5b618766aef319aa2e141fa Reviewed-on: https://cl.tvl.fyi/c/depot/+/8981 Tested-by: BuildkiteCI Reviewed-by: tazjin Autosubmit: tazjin --- users/tazjin/emacs/config/look-and-feel.el | 32 +++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/users/tazjin/emacs/config/look-and-feel.el b/users/tazjin/emacs/config/look-and-feel.el index 0281e360d6..467316aa8b 100644 --- a/users/tazjin/emacs/config/look-and-feel.el +++ b/users/tazjin/emacs/config/look-and-feel.el @@ -35,6 +35,35 @@ (when (bottom-right-window-p) (telephone-line-raw mode-line-misc-info t))) +;; Implements a mode-line warning if there are any logged in TTY +;; sessions apart from the graphical one. +;; +;; The status is only updated once every 30 seconds, as it requires +;; shelling out to some commands (for now). +(defun list-tty-sessions () + "List all logged in tty sessions, except tty7 (graphical)" + (let ((command "who | awk '{print $2}' | grep -v tty7")) + (-filter (lambda (s) (not (string-empty-p s))) + (s-lines + (s-trim (shell-command-to-string command)))))) + +(defvar cached-tty-sessions (cons (time-convert nil 'integer) (list-tty-sessions)) + "Cached TTY session value to avoid running the command too often.") + +(defun get-cached-tty-sessions () + (let ((time )) + (when (< 30 + (- (time-convert nil 'integer) + (car cached-tty-sessions))) + (setq cached-tty-sessions + (cons (time-convert nil 'integer) (list-tty-sessions))))) + + (cdr cached-tty-sessions)) + +(telephone-line-defsegment telephone-line-warn-tty-session () + (when-let (sessions (get-cached-tty-sessions)) + (format "W: [%s]!!" (s-join "," sessions)))) + (defun telephone-line-setup () (telephone-line-defsegment telephone-line-last-window-segment () (telephone-misc-if-last-window)) @@ -51,7 +80,8 @@ '(highlight . (special-highlight . special-highlight))) (setq telephone-line-lhs - '((nil . (telephone-line-position-segment)) + '((highlight . (telephone-line-warn-tty-session)) + (nil . (telephone-line-position-segment)) (accent . (telephone-line-buffer-segment)))) (setq telephone-line-rhs -- cgit 1.4.1