From 08dc3a58d3fd92446ce656ea8d4eae1871a030cd Mon Sep 17 00:00:00 2001 From: Narvin Singh Date: Mon, 28 Dec 2020 14:03:50 -0500 Subject: [PATCH] Feat: CPU, memory, backlight and volume modules --- module/bl | 20 ++++++++++++++++++++ module/cpu | 15 +++++++++++++++ module/mem | 15 +++++++++++++++ module/vol-amixer | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100755 module/bl create mode 100755 module/cpu create mode 100755 module/mem create mode 100755 module/vol-amixer diff --git a/module/bl b/module/bl new file mode 100755 index 0000000..c1bc7a6 --- /dev/null +++ b/module/bl @@ -0,0 +1,20 @@ +#!/bin/bash + +mod_bl () { + # Customizable configuration constants + local -r DEFAULT_PRE=' ' + local -r DEFAULT_SUF='%' + + local -r pre="${1-${DEFAULT_PRE}}" + local -r suf="${2-${DEFAULT_SUF}}" + + local bl_file bl max_bl_file max_bl + + bl_file="$(find /sys/class/backlight/*/brightness | head -n 1)" + max_bl_file="$(dirname "${bl_file}")/max_brightness" + read -r bl < "${bl_file}" + read -r max_bl < "${max_bl_file}" + # Use printf to do floating point arithmetic + printf '%b%3.0f%b' "${pre}" "$((bl * 1000 / max_bl))e-1" "${suf}" +} + diff --git a/module/cpu b/module/cpu new file mode 100755 index 0000000..eb4f8c0 --- /dev/null +++ b/module/cpu @@ -0,0 +1,15 @@ +#!/bin/bash + +mod_cpu () { + # Customizable configuration constants + local -r DEFAULT_PRE=' ' + local -r DEFAULT_SUF='' + + local -r pre="${1-${DEFAULT_PRE}}" + local -r suf="${2-${DEFAULT_SUF}}" + + printf '%b' "${pre}" + ps --no-headers -A -o %cpu | awk '{cpu = cpu + $1} END {printf "%3.0f", cpu}' + printf '%b' "${suf}" +} + diff --git a/module/mem b/module/mem new file mode 100755 index 0000000..0a715cd --- /dev/null +++ b/module/mem @@ -0,0 +1,15 @@ +#!/bin/bash + +mod_mem () { + # Customizable configuration constants + local -r DEFAULT_PRE=' ' + local -r DEFAULT_SUF='%' + + local -r pre="${1-${DEFAULT_PRE}}" + local -r suf="${2-${DEFAULT_SUF}}" + + printf '%b' "${pre}" + free | awk 'NR == 2 {printf "%3.0f", $3 * 100 / $2}' + printf '%b' "${suf}" +} + diff --git a/module/vol-amixer b/module/vol-amixer new file mode 100755 index 0000000..15c59e4 --- /dev/null +++ b/module/vol-amixer @@ -0,0 +1,34 @@ +#!/bin/bash + +# Requires amixer from the alsa-utils package + +mod_vol_amixer () { + # Customizable configuration constants + local -r DEFAULT_PRE_LOW=' ' + local -r DEFAULT_PRE_HI=' ' + local -r DEFAULT_PRE_MUTE=' ' + local -r DEFAULT_SUF='%' + + local -r pre_low="${1-${DEFAULT_PRE_LOW}}" + local -r pre_hi="${2-${DEFAULT_PRE_HI}}" + local -r pre_mute="${3-${DEFAULT_PRE_MUTE}}" + local -r suf="${4-${DEFAULT_SUF}}" + + local pre info stat vol + + info="$(amixer get Master | tail -n 1)" + stat="$(sed -E 's/.*\[(.*)\]/\1/' <<< "${info}")" + vol="$(sed -E 's/.*\[(.*)%\].*/\1/' <<< "${info}")" + + case "${stat}" in + on) + if [[ "${vol}" -lt 50 ]]; then pre="${pre_low}"; + else pre="${pre_hi}"; fi + ;; + *) + pre="${pre_mute}" + ;; + esac + printf '%b%3d%b' "${pre}" "${vol}" "${suf}" +} +