nnscripts

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 81a0bd3c39a4812e16910bfc588c441a99840976
parent 311828d427ca1f3fe61a5de97ad22a4a6bf84342
Author: Luxferre <lux@ferre>
Date:   Thu, 11 Jan 2024 17:16:56 +0200

Added nnstatus-stdout and nnkeys

Diffstat:
MREADME.md | 5+++--
Annkeys.conf | 22++++++++++++++++++++++
Annkeys.sh | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Annstatus-stdout.sh | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 151 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md @@ -7,9 +7,11 @@ This is a set of (mostly Linux-specific unless stated otherwise) various useful - `nnwifi.sh`: easily set up a WiFi connection (depends on `ip`, `iw`, `wpa_supplicant` and `udhcpc`) - `nnlight.sh`: control laptop screen brightness (depends on `nawk` or other AWK command) - `nnstatus.sh`: output useful status information to dzen2 panel (Xorg-specific but can be rewritten to use any other panel of your choice, depends on `nawk` or other AWK command, `iw` for WLAN stats, `amixer` for volume controls and xkb-switch for keyboard layout controls) +- `nnstatus-stdout.sh`: same as `nnstatus.sh` but without interactivity and keyboard layout functionality, suitable for use in dvtm, a4 and other terminal-based tools with configurable status bar, as it just outputs the information line into the stdout +- `nnkeys.sh`: shell-based multimedia keys manager daemon with a separate configuration file (see `nnkeys.conf` for an example), depends on `evtest`, `pkill` and AWK ## Credits All scripts in this repo were created by Luxferre starting from 2023 and released into public domain. -Made in Ukraine. -\ No newline at end of file +Made in Ukraine. diff --git a/nnkeys.conf b/nnkeys.conf @@ -0,0 +1,22 @@ +# an example config file for nnkeys.sh script +# the keys are as specified in the evtest output +# if the command is empty, the line is ignored + +# volume control keys +KEY_VOLUMEUP amixer sset Master playback 5%+ +KEY_VOLUMEDOWN amixer sset Master playback 5%- +KEY_MUTE amixer sset Master toggle + +# playback control keys +KEY_PLAYPAYSE +KEY_PREVIOUSSONG +KEY_NEXTSONG + +# brightness control keys +KEY_BRIGHTNESSDOWN +KEY_BRIGTNESSUP + +# misc keys found e.g. on macbooks +KEY_SCALE +KEY_DASHBOARD +KEY_EJECTCD diff --git a/nnkeys.sh b/nnkeys.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# nnkeys: no-nonsense multimedia keys manager for bare CLI systems +# Linux-specific, depends on pkill and evtest +# as well as AWK engine of your choice (nawk or mawk recommended) +# Usage: nnkeys.sh nnkeys.conf +# nnkeys.conf lines format (see example in the repo): +# KEY_* cmd +# where KEY_* is the identifier from evtest +# the script is daemonized automatically, just re-run it to reconfigure +# created by Luxferre in 2024, released into public domain + +# static configuration section + +DEVICE="/dev/input/event1" # check your path with evtest +AWK_ENGINE=nawk # change to yours if necessary +AWK_TMP="/tmp/nnkeys.awk" # temporary awk script location +NNPID="/tmp/nnkeys.pid" # temporary PID location + +# do not change beyond this point unless you know what you're doing + +# config file +CONF="$1" + +[[ ! -f "$CONF" ]] && echo "No config file!" && exit 1 + +# guard from double-launch: kill the previous instance + +[[ -f "$NNPID" ]] && pkill -P $(cat $NNPID) && rm -f $NNPID $AWK_TMP + +strim() { # string whitespace trimmer + local s2 s="$*" + until s2="${s#[[:space:]]}"; [[ "$s2" = "$s" ]]; do s="$s2"; done + until s2="${s%[[:space:]]}"; [[ "$s2" = "$s" ]]; do s="$s2"; done + echo "$s" +} + +# read config and shape the AWK key handler script +while read -r cline; do + [[ "$cline" = \#* ]] && continue # skip comments + ckey="${cline%% *}" + ccmd="${cline#$ckey}" + ccmd="$(strim $ccmd)" + [[ -n "$ccmd" ]] && echo "/${ckey}.*value\ 1/{system(\"$ccmd\")}" +done < $CONF > $AWK_TMP + +echo "Config read, daemonizing..." + +nohup $SHELL -c "evtest "$DEVICE" | $AWK_ENGINE -f $AWK_TMP" > /dev/null 2>&1 & +MYPID="$!" +echo "$MYPID" > $NNPID + +echo "Listener started, pid is at $NNPID" diff --git a/nnstatus-stdout.sh b/nnstatus-stdout.sh @@ -0,0 +1,74 @@ +#!/bin/sh +# nnstatus-stdout.sh: no-nonsense status line script (Linux-specific) +# outputs a single line to stdout, it's up to other scripts what to do with it +# only depends on awk (I recommend nawk/one-true-awk) to work with POSIX shells +# and amixer to work with sound and iw to work with wireless networks +# created by Luxferre in 2024, released into public domain + +# configure some parameters here +AWK_CMD=nawk # your awk engine +DELIM=' | ' # delimiter between the fields +SLEEP_INTERVAL=2 # data refresh interval (in seconds) +BATDEV='BAT0' # battery device indicator, if applicable (unset to disable) +MIXER_CHAN='Master' # ALSA channel to display + +BATDIR="/sys/class/power_supply/$BATDEV" + +# helper functions + +cpustat() { # returns: Idle NonIdle + $AWK_CMD 'NR==1{printf("%u %u", $5+$6, $2+$3+$4+$7+$8+$9)}' /proc/stat +} + +ramstat() { # print used RAM + $AWK_CMD '/^MemTotal:/{mt=$2}/^Buffers:/{mb=$2}/^MemFree:/{mf=$2}\ + /^Cached:/{mc=$2}/^Shmem:/{ms=$2}/^SReclaimable:/{msr=$2}\ + END{d=mt-mf-mc-msr-mb+ms;if(d<1048576) printf("RAM %4.0fM",d/1024);\ + else printf("RAM %4.2fG",d/1048576)}' /proc/meminfo +} + +batstat() { # print battery status + local bstatus="$(cat $BATDIR/status)" + local bpercent="$(cat $BATDIR/capacity)" + local cstatus="CHG" # charging by default + [ "$bstatus" = "Discharging" ] && cstatus="BAT" + printf "%s %03u%%" "$cstatus" "$bpercent" +} + +volstat() { + amixer sget $MIXER_CHAN | $AWK_CMD 'BEGIN{acc=0;c=0;con=1}\ + /\[[0-9][0-9]*\%\]/{for(i=1;i<=NF;i++){\ + if($i ~ /\[[0-9][0-9]*\%\]/){acc+=int(substr($i,2));c++}\ + if($i ~ /\[off\]/){con=0}\ + }}END{printf(con?"VOL %03u%%":"VOL OFF ",int(c>0?acc/c:acc))}' +} + +wlanstat() { + local ssid="$(iw dev $1 link | $AWK_CMD '/SSID:/{print $2}')" + printf "%s: " "$1" + [ -z "$ssid" ] && printf "None" || printf "%s" "$ssid" +} + +CPUFILE='/tmp/nnstatus.cpu' +[ -e "$CPUFILE" ] && PREVSTAT="$(cat $CPUFILE)" || PREVSTAT="$(cpustat)" + +# you can reorder the calls here + +# wifi +wlanstat wlan0 +# ram +printf "%s" "$DELIM" +ramstat +# cpu +printf "%s" "$DELIM" +CURSTAT="$(cpustat)" +printf "%s %s" "$PREVSTAT" "$CURSTAT" | $AWK_CMD 'NR==1{t=$3+$4-$2-$1;i=$3-$1;printf("CPU %03d%%",100*(t-i)/t)}' +echo "$CURSTAT" > $CPUFILE +# battery +[ -n "$BATDEV" ] && [ -d "$BATDIR" ] && printf "%s" "$DELIM" && batstat +# volume +printf "%s" "$DELIM" +volstat +# clock +printf "%s\n" "$DELIM$(date +'%a %b %d %H:%M')" +