From 63303977071f34d8142709960e8dae97cea1951e Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 9 Aug 2022 23:48:44 +0100 Subject: examples -> etc --- etc/build | 17 +++++ etc/padding-icon.sh | 56 +++++++++++++++++ etc/status.sh | 56 +++++++++++++++++ etc/xmobar.config | 39 ++++++++++++ etc/xmobar.el | 164 +++++++++++++++++++++++++++++++++++++++++++++++++ etc/xmobar.hs | 77 +++++++++++++++++++++++ etc/xmonadpropwrite.hs | 41 +++++++++++++ 7 files changed, 450 insertions(+) create mode 100644 etc/build create mode 100755 etc/padding-icon.sh create mode 100755 etc/status.sh create mode 100644 etc/xmobar.config create mode 100644 etc/xmobar.el create mode 100644 etc/xmobar.hs create mode 100644 etc/xmonadpropwrite.hs (limited to 'etc') diff --git a/etc/build b/etc/build new file mode 100644 index 0000000..9e9eb72 --- /dev/null +++ b/etc/build @@ -0,0 +1,17 @@ +#!/usr/bin/bash + +# An example build script that directs ghc to use a temporary directory for its +# intermediate files instead of writing them into XMOBAR_CONFIG_DIR. This +# allows using a read-only XMOBAR_CONFIG_DIR. To use this script, place it in +# XMOBAR_CONFIG_DIR and call it "build". + +bin=$1 +object_dir=$(mktemp -d) + +default_build_args=(--make xmobar.hs -i -ilib -fforce-recomp -main-is main -v0 -o "$bin" -threaded -rtsopts -with-rtsopts -V0) # From src/Xmobar/App/Compile.hs +extra_build_args=(-odir "$object_dir" -hidir "$object_dir") + +ghc "${default_build_args[@]}" "${extra_build_args[@]}" +status=$? +rm -r "$object_dir" +exit $status diff --git a/etc/padding-icon.sh b/etc/padding-icon.sh new file mode 100755 index 0000000..ec3ada2 --- /dev/null +++ b/etc/padding-icon.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Detects the width of running window with name given as first +# argument (xprop name '$1') and creates an XPM icon of that width, +# 1px height, and transparent. Outputs an -tag for use in +# xmobar to display the generated XPM icon. +# +# Run script from xmobar and trayer: +# `Run Com "/where/ever/padding-icon.sh" ["panel"] "trayerpad" 10` +# and use `%trayerpad%` in your template. +# or, if you're using for instance stalonetray: +# `Run Com "/where/ever/padding-icon.sh" ["stalonetray"] "tray" 10` + +# Very heavily based on Jonas Camillus Jeppensen code +# https://codeberg.org/xmobar/xmobar/issues/239#issuecomment-233206552 + +# Function to create a transparent Wx1 px XPM icon +create_xpm_icon () { +timestamp=$(date) +pixels=$(for i in `seq $1`; do echo -n "."; done) + +cat << EOF > "$2" +/* XPM * +static char * trayer_pad_xpm[] = { +/* This XPM icon is used for padding in xmobar to */ +/* leave room for trayer-srg. It is dynamically */ +/* updated by by trayer-pad-icon.sh which is run */ +/* by xmobar. */ +/* Created: ${timestamp} */ +/* */ +"$1 1 1 1", +/* Colors (none: transparent) */ +". c none", +/* Pixels */ +"$pixels" +}; +EOF +} + +# panel window name +pname=${1:-panel} + +# Width of the trayer window +width=$(xprop -name $pname | grep 'program specified minimum size' | cut -d ' ' -f 5) + +# Icon file name +iconfile="/tmp/$pname-padding-${width:-0}px.xpm" + +# If the desired icon does not exist create it +if [ ! -f $iconfile ] +then + create_xpm_icon $width $iconfile +fi + +# Output the icon tag for xmobar +echo "" diff --git a/etc/status.sh b/etc/status.sh new file mode 100755 index 0000000..151728b --- /dev/null +++ b/etc/status.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +STATUSPIPE="/tmp/xmobar_status_jrk" + +function isMuted () { + # retrieve mute status + # return an arbitrary string for true or nothing at all + echo +} + +function getPercent () { + # somehow retrieve the percent value as plain int (e.g. "66") + echo "66" +} + +function percentBar () { + local i=1 res= + normal=47 high=80 + fgColor='#657b83' mutedColor='#cb4b16' + lowColor='#859900' midColor='#b58900' + highColor='#cb4b16' + + bar="$(echo -ne "\u2588")" + percent="$( getPercent )" + muted="$( isMuted )" + + if [ -n "$muted" ]; then + res="" + else + res="" + fi + + while [ $i -lt $percent ]; do + if [ $i -eq $normal -a -z "$muted" ]; then + res+="" + elif [ $i -eq $high -a -z "$muted" ]; then + res+="" + fi + + res+=$bar + i=$((i+1)) + done + + res+="" + + while [ $i -lt 100 ]; do + res+='-' + i=$((i+1)) + done + + echo "$res" +} + +[[ -p $STATUSPIPE ]] || mkfifo $STATUSPIPE + +echo "$( percentBar )" > "$STATUSPIPE" diff --git a/etc/xmobar.config b/etc/xmobar.config new file mode 100644 index 0000000..e951b67 --- /dev/null +++ b/etc/xmobar.config @@ -0,0 +1,39 @@ +Config { font = "-misc-fixed-*-*-*-*-10-*-*-*-*-*-*-*" + , additionalFonts = [] + , borderColor = "black" + , border = TopB + , bgColor = "black" + , fgColor = "grey" + , alpha = 255 + , position = Top + , textOffset = -1 + , iconOffset = -1 + , lowerOnStart = True + , pickBroadest = False + , persistent = False + , hideOnStart = False + , iconRoot = "." + , allDesktops = True + , overrideRedirect = True + , textOutputFormat = Ansi + , commands = [ Run Weather "EGPF" ["-t",": C", + "-L","18","-H","25", + "--normal","green", + "--high","red", + "--low","lightblue"] 36000 + , Run Network "eth0" ["-L","0","-H","32", + "--normal","green","--high","red"] 10 + , Run Network "eth1" ["-L","0","-H","32", + "--normal","green","--high","red"] 10 + , Run Cpu ["-L","3","-H","50", + "--normal","green","--high","red"] 10 + , Run Memory ["-t","Mem: %"] 10 + , Run Swap [] 10 + , Run Com "uname" ["-s","-r"] "" 36000 + , Run Date "%a %b %_d %Y %H:%M:%S" "date" 10 + ] + , sepChar = "%" + , alignSep = "}{" + , template = "%cpu% | %memory% * %swap% | %eth0% - %eth1% }\ + \{ %date%| %EGPF% | %uname%" + } diff --git a/etc/xmobar.el b/etc/xmobar.el new file mode 100644 index 0000000..905af3d --- /dev/null +++ b/etc/xmobar.el @@ -0,0 +1,164 @@ +;; xmobar.el --- Display xmobar text output -*- lexical-binding: t -*- + +;; Copyright 2022 jao +;; Version: 0.0.1 +;; Package-Requires: ((emacs "28.1")) +;; Keywords: unix + +;; Heavily inspired by Steven Allen's https://github.com/Stebalien/i3bar.el + +;; This file is not part of GNU Emacs. + +;; This file is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. + +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. + +;;; Commentary: + +;; Displays the output of an xmobar command in the Emacs mode-line (or tab-line). + +;;; Code: + +(eval-when-compile (require 'cl-lib)) + +(require 'tab-bar) +(require 'xterm-color nil t) + +(defgroup xmobar nil + "xmobar status display for Emacs." + :version "0.0.1" + :group 'mode-line) + +(defcustom xmobar-command '("xmobar" "-TAnsi") + "The xmobar command and flags." + :type '(choice (string :tag "Shell Command") + (repeat (string)))) + +(defcustom xmobar-tab-bar t + "Whether to dispaly xmobar output in the tab bar." + :type 'boolean) + +(defcustom xmobar-tab-split nil + "Split on this string for `xmobar-left-string' and `xmobar-right-string'." + :type 'string) + +(defcustom xmobar-tab-bar-format + '(xmobar-left-string tab-bar-format-align-right xmobar-right-string) + "Format for the tab bar when `xmobar-tab-bar' is t." + :type 'list) + +(defvar xmobar--process nil + "The running xmobar process, if any.") + +(defvar xmobar--left-string "") + +(defvar xmobar-string "" + "The xmobar string to be displayed in the mode-line or tab-bar.") + +(put 'xmobar-string 'risky-local-variable t) + +(defvar xmobar--colorize-fn + (if (featurep 'xterm-color) #'xterm-color-filter #'ansi-color-apply)) + +(defvar xmobar--old-tab-format tab-bar-format) + +(defun xmobar-string () xmobar-string) +(defun xmobar-right-string () xmobar-string) +(defun xmobar-left-string () xmobar--left-string) + +;;;###autoload +(define-minor-mode xmobar-mode + "Display an xmobar in the mode-line." + :global t :group 'xmobar + (xmobar--stop) + (if xmobar-mode + (progn (if xmobar-tab-bar + (progn + (setq xmobar--old-tab-format tab-bar-format) + (setq tab-bar-format xmobar-tab-bar-format) + (tab-bar-mode 1)) + (or global-mode-string (setq global-mode-string '(""))) + (unless (memq 'xmobar-string global-mode-string) + (setq global-mode-string + (append global-mode-string '(xmobar-string))))) + (xmobar--start)) + (when xmobar-tab-bar (setq tab-bar-format xmobar--old-tab-format)))) + +(defun xmobar--update (update) + "Apply an UPDATE to the xmobar bar." + (when xmobar-mode + (let* ((str (funcall xmobar--colorize-fn update)) + (strs (and xmobar-tab-split (split-string str xmobar-tab-split)))) + (setq xmobar-string (if strs (cadr strs) str) + xmobar--left-string (or (car strs) ""))) + (force-mode-line-update t))) + +(defun xmobar--process-filter (proc string) + "Process output from the xmobar process." + (let ((buf (process-buffer proc))) + (when (buffer-live-p buf) + (with-current-buffer buf + ;; Write the input to the buffer (might be partial). + (save-excursion + (goto-char (process-mark proc)) + (insert string) + (set-marker (process-mark proc) (point))) + (when (string-match-p "\n$" string) + (xmobar--update (buffer-string)) + (delete-region (point-min) (point-max))))))) + +(defun xmobar--process-sentinel (proc status) + "Handle events from the xmobar process (PROC). +If the process has exited, this function stores the exit STATUS in +`xmobar-string'." + (unless (process-live-p proc) + (setq xmobar--process nil) + (let ((buf (process-buffer proc))) + (when (and buf (buffer-live-p buf)) (kill-buffer buf))) + (setq xmobar-string (format "xmobar: %s" status) xmobar--left-string ""))) + +(defun xmobar--start () + "Start xmobar." + (xmobar--stop) + (condition-case err + (setq xmobar--process + (make-process + :name "xmobar" + :buffer " *xmobar process*" + :stderr " *xmobar stderr*" + :command (ensure-list xmobar-command) + :connection-type 'pipe + :noquery t + :sentinel #'xmobar--process-sentinel + :filter #'xmobar--process-filter)) + (error + (setq xmobar-string + (format "starting xmobar: %s" (error-message-string err)) + xmobar--left-string "")))) + +(defun xmobar--stop () + "Stop xmobar." + (when (and xmobar--process (process-live-p xmobar--process)) + (delete-process xmobar--process)) + (setq xmobar-string "" xmobar--left-string "")) + +;;;###autoload +(defun xmobar-restart () + "Restart the xmobar program." + (interactive) + (unless xmobar-mode (user-error "The xmobar-mode is not enabled")) + (xmobar--start)) + +(provide 'xmobar) +;;; xmobar.el ends here diff --git a/etc/xmobar.hs b/etc/xmobar.hs new file mode 100644 index 0000000..f8434fe --- /dev/null +++ b/etc/xmobar.hs @@ -0,0 +1,77 @@ +------------------------------------------------------------------------------ +-- | +-- Copyright: (c) 2018, 2019, 2022 Jose Antonio Ortega Ruiz +-- License: BSD3-style (see LICENSE) +-- +-- Maintainer: jao@gnu.org +-- Stability: unstable +-- Portability: portable +-- Created: Sat Nov 24, 2018 21:03 +-- +-- +-- An example of a Haskell-based xmobar. Compile it with +-- ghc --make -- xmobar.hs +-- with the xmobar library installed or simply call: +-- xmobar /path/to/xmobar.hs +-- and xmobar will compile and launch it for you and +------------------------------------------------------------------------------ + +import Xmobar + +-- Example user-defined plugin + +data HelloWorld = HelloWorld + deriving (Read, Show) + +instance Exec HelloWorld where + alias HelloWorld = "hw" + run HelloWorld = return "Hello World!!" + +-- Configuration, using predefined monitors as well as our HelloWorld +-- plugin: + +config :: Config +config = defaultConfig { + font = "xft:Sans Mono-9" + , additionalFonts = [] + , borderColor = "black" + , border = TopB + , bgColor = "black" + , fgColor = "grey" + , alpha = 255 + , position = Top + , textOffset = -1 + , iconOffset = -1 + , lowerOnStart = True + , pickBroadest = False + , persistent = False + , hideOnStart = False + , iconRoot = "." + , allDesktops = True + , overrideRedirect = True + , textOutputFormat = Ansi + , commands = [ Run $ Weather "EGPH" ["-t",": C", + "-L","18","-H","25", + "--normal","green", + "--high","red", + "--low","lightblue"] 36000 + , Run $ Network "eth0" ["-L","0","-H","32", + "--normal","green","--high","red"] 10 + , Run $ Network "eth1" ["-L","0","-H","32", + "--normal","green","--high","red"] 10 + , Run $ Cpu ["-L","3","-H","50", + "--normal","green","--high","red"] 10 + , Run $ Memory ["-t","Mem: %"] 10 + , Run $ Swap [] 10 + , Run $ Com "uname" ["-s","-r"] "" 36000 + , Run $ Date "%a %b %_d %Y %H:%M:%S" "date" 10 + , Run HelloWorld + ] + , sepChar = "%" + , alignSep = "}{" + , template = "%cpu% | %memory% * %swap% | %eth0% - %eth1% }\ + \ %hw% { %date%| %EGPH% | %uname%" +} + +main :: IO () +main = configFromArgs config >>= xmobar diff --git a/etc/xmonadpropwrite.hs b/etc/xmonadpropwrite.hs new file mode 100644 index 0000000..f3f4a5d --- /dev/null +++ b/etc/xmonadpropwrite.hs @@ -0,0 +1,41 @@ +-- Copyright Spencer Janssen +-- Tomas Janousek +-- BSD3 (see LICENSE) +-- +-- Reads from standard input and writes to an X propery on root window. +-- To be used with XPropertyLog: +-- Add it to commands: +-- Run XPropertyLog "_XMONAD_LOG_CUSTOM" +-- Add it to the template: +-- template = "... %_XMONAD_LOG_CUSTOM% ..." +-- Run: +-- $ blah blah | xmonadpropwrite _XMONAD_LOG_CUSTOM + +import Control.Monad +import Graphics.X11 +import Graphics.X11.Xlib.Extras +import qualified Data.ByteString as B +import Foreign.C (CChar) +import System.IO +import System.Environment + +main = do + atom <- flip fmap getArgs $ \args -> case args of + [a] -> a + _ -> "_XMONAD_LOG" + + d <- openDisplay "" + xlog <- internAtom d atom False + ustring <- internAtom d "UTF8_STRING" False + + root <- rootWindow d (defaultScreen d) + + forever $ do + msg <- B.getLine + changeProperty8 d root xlog ustring propModeReplace (encodeCChar msg) + sync d True + + return () + +encodeCChar :: B.ByteString -> [CChar] +encodeCChar = map fromIntegral . B.unpack -- cgit v1.2.3