diff --git a/README.md b/README.md index 926fbd5..94337bb 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,13 @@ dwmbar - A Modular Status Bar for dwm ===================================== ![Example image](res/example.png) +

+ +

+

+

dwmbar is a very simple status bar written for dwm. diff --git a/bar.sh b/bar.sh index 2e096bc..bd012d1 100755 --- a/bar.sh +++ b/bar.sh @@ -23,20 +23,52 @@ fi export LC_ALL=C export LANG=C - # shellcheck source=/dev/null -source "$CONFIG_FILE" +if [[ "$CONFIG_FILE" == *.json ]]; then + MODULES=$(jq -r '.modules | join(" ")' "$CONFIG_FILE") + ONLINE_MODULES=$(jq -r '.online_modules | join(" ")' "$CONFIG_FILE") + DELAY=$(jq -r '.delay // 0.05' "$CONFIG_FILE") + SEPARATOR=$(jq -r '.separator // " | "' "$CONFIG_FILE") + LEFT_PADDING=$(jq -r '.left_padding // " "' "$CONFIG_FILE") + RIGHT_PADDING=$(jq -r '.right_padding // " "' "$CONFIG_FILE") + CUSTOM_DIR=$(jq -r '.custom_dir' "$CONFIG_FILE") + CUSTOM_DIR="${CUSTOM_DIR/#\~/$HOME}" + CUSTOM_DIR="${CUSTOM_DIR/\$USER/$USER}" + CUSTOM_DIR="${CUSTOM_DIR/\$HOME/$HOME}" + [[ -z "$CUSTOM_DIR" ]] && CUSTOM_DIR="$HOME/.config/dwmbar/modules/custom/" +else + # shellcheck source=/dev/null + source "$CONFIG_FILE" +fi + +export SEPARATOR +export MODULES +export ONLINE_MODULES +export DELAY +export LEFT_PADDING +export RIGHT_PADDING +export CUSTOM_DIR get_bar() { + local bar="" + local first=true + for module in $MODULES; do - if [[ $INTERNET -eq 0 ]] || [[ $ONLINE_MODULES != *"$module"* ]];then - module_out="$(sed 's/\.$//g' "$OUTPUT_CACHE$module")" - bar="$bar$module_out" + if [[ $INTERNET -eq 0 ]] || [[ $ONLINE_MODULES != *"$module"* ]]; then + module_out="$(cat "$OUTPUT_CACHE$module" 2>/dev/null)" + + if [[ -n "$module_out" ]]; then + if [[ "$first" == true ]]; then + bar="$module_out" + first=false + else + bar="$bar$SEPARATOR$module_out" + fi + fi fi done - # Uncomment to remove last separator - # bar="$(echo "$bar" | sed 's/.$//g')" - echo "$LEFT_PADDING${bar::-2}$RIGHT_PADDING" + + echo "$LEFT_PADDING$bar$RIGHT_PADDING" } run_module() @@ -50,8 +82,7 @@ run_module() if [[ "$out" = " " ]]; then echo "" > "$OUTPUT_CACHE$module" - elif [[ ! "$out" = "" ]]; then - out="$out$SEPARATOR." + else echo "$out" > "$OUTPUT_CACHE$module" fi } diff --git a/dwmbar b/dwmbar index a038c93..f7483bc 100755 --- a/dwmbar +++ b/dwmbar @@ -29,12 +29,19 @@ export SYNC_SCRIPT CONFIG_DIR="/home/$USER/.config/dwmbar/" export CONFIG_DIR -CUSTOM_DIR="$CONFIG_DIR/custom/" +CUSTOM_DIR="$CONFIG_DIR/modules/custom/" export CUSTOM_DIR -CONFIG_FILE="$CONFIG_DIR/config" -if [[ ! -f "$CONFIG_FILE" ]]; then - CONFIG_FILE=$DEFAULT_CONFIG_LOCATION +# Prefer JSON config if jq is available and file exists +JSON_CONFIG="$CONFIG_DIR/config.json" +BASH_CONFIG="$CONFIG_DIR/config" + +if command -v jq >/dev/null 2>&1 && [[ -f "$JSON_CONFIG" ]]; then + CONFIG_FILE="$JSON_CONFIG" +elif [[ -f "$BASH_CONFIG" ]]; then + CONFIG_FILE="$BASH_CONFIG" +else + CONFIG_FILE="$DEFAULT_CONFIG_LOCATION" fi export CONFIG_FILE @@ -90,6 +97,33 @@ check_files(){ fi } +load_config() { + if [[ "$CONFIG_FILE" == *.json ]]; then + MODULES=$(jq -r '.modules | join(" ")' "$CONFIG_FILE") + ONLINE_MODULES=$(jq -r '.online_modules | join(" ")' "$CONFIG_FILE") + DELAY=$(jq -r '.delay // 0.05' "$CONFIG_FILE") + SEPARATOR=$(jq -r '.separator // " | "' "$CONFIG_FILE") + LEFT_PADDING=$(jq -r '.left_padding // " "' "$CONFIG_FILE") + RIGHT_PADDING=$(jq -r '.right_padding // " "' "$CONFIG_FILE") + CUSTOM_DIR=$(jq -r '.custom_dir' "$CONFIG_FILE") + CUSTOM_DIR="${CUSTOM_DIR/#\~/$HOME}" + CUSTOM_DIR="${CUSTOM_DIR/\$USER/$USER}" + CUSTOM_DIR="${CUSTOM_DIR/\$HOME/$HOME}" + [[ -z "$CUSTOM_DIR" ]] && CUSTOM_DIR="$HOME/.config/dwmbar/modules/custom/" + else + # shellcheck source=/dev/null + source "$CONFIG_FILE" + fi + + export SEPARATOR + export MODULES + export ONLINE_MODULES + export DELAY + export LEFT_PADDING + export RIGHT_PADDING + export CUSTOM_DIR +} + while getopts 'vc' flag; do case "${flag}" in v) print_help @@ -118,14 +152,18 @@ if [[ -x "$SYNC_SCRIPT" ]]; then "$SYNC_SCRIPT" 2>/dev/null || true fi +load_config + while :; do date=$(date +'%S') if [ $(( 10#$date % 5 )) -eq 0 ]; then INTERNET=$(check_internet) + export INTERNET # Sync again periodically in case user edits config if [[ -x "$SYNC_SCRIPT" ]]; then "$SYNC_SCRIPT" 2>/dev/null || true fi + load_config sleep 1 fi xsetroot -name "$(exec $DEFAULT_BAR_LOCATION)" diff --git a/modules/archupdates b/modules/archupdates index 22144d8..d38e41f 100755 --- a/modules/archupdates +++ b/modules/archupdates @@ -5,28 +5,19 @@ set -euo pipefail # shellcheck source=../lib/cache.sh . "$(dirname "$0")/../lib/cache.sh" -PREFIX=' Updates: ' -CACHE_KEY="arch_updates" +PREFIX=' Updates: ' +CACHE_KEY="arch_updates_official" CACHE_DURATION=${ARCH_UPDATES_CACHE_SECONDS:-300} get_updates() { - local updates_arch=0 updates_aur=0 total=0 + local updates=0 if command -v checkupdates >/dev/null 2>&1; then - updates_arch=$(checkupdates 2>/dev/null | wc -l) + updates=$(checkupdates 2>/dev/null | wc -l) fi - if command -v yay >/dev/null 2>&1; then - updates_aur=$(yay -Qum 2>/dev/null | wc -l) - elif command -v paru >/dev/null 2>&1; then - updates_aur=$(paru -Qum 2>/dev/null | wc -l) - elif command -v trizen >/dev/null 2>&1; then - updates_aur=$(trizen -Qum 2>/dev/null | wc -l) - fi - - total=$((updates_arch + updates_aur)) - cache_write "$CACHE_KEY" "$total" - echo "$PREFIX$total" + cache_write "$CACHE_KEY" "$updates" + echo "$PREFIX$updates" } if cached=$(cache_read "$CACHE_KEY" "$CACHE_DURATION" 2>/dev/null); then diff --git a/modules/archupdates-aur b/modules/archupdates-aur new file mode 100755 index 0000000..e129ae0 --- /dev/null +++ b/modules/archupdates-aur @@ -0,0 +1,31 @@ +#!/bin/bash +set -euo pipefail +# shellcheck source=../lib/common.sh +. "$(dirname "$0")/../lib/common.sh" +# shellcheck source=../lib/cache.sh +. "$(dirname "$0")/../lib/cache.sh" + +PREFIX=' AUR Updates: ' +CACHE_KEY="arch_updates_aur" +CACHE_DURATION=${ARCH_UPDATES_AUR_CACHE_SECONDS:-300} + +get_aur_updates() { + local updates=0 + + if command -v yay >/dev/null 2>&1; then + updates=$(yay -Qum 2>/dev/null | wc -l) + elif command -v paru >/dev/null 2>&1; then + updates=$(paru -Qum 2>/dev/null | wc -l) + elif command -v trizen >/dev/null 2>&1; then + updates=$(trizen -Qum 2>/dev/null | wc -l) + fi + + cache_write "$CACHE_KEY" "$updates" + echo "$PREFIX$updates" +} + +if cached=$(cache_read "$CACHE_KEY" "$CACHE_DURATION" 2>/dev/null); then + echo "$PREFIX$cached" +else + get_aur_updates +fi diff --git a/res/bar2.png b/res/bar2.png new file mode 100644 index 0000000..f6d4c17 Binary files /dev/null and b/res/bar2.png differ diff --git a/tests/expected/archupdates.txt b/tests/expected/archupdates.txt index 2cdebd2..d646874 100644 --- a/tests/expected/archupdates.txt +++ b/tests/expected/archupdates.txt @@ -1 +1 @@ - Updates: 4 + Updates: 1 diff --git a/todo.md b/todo.md index 9c04c43..36aaab8 100644 --- a/todo.md +++ b/todo.md @@ -1,5 +1,3 @@ # TODO -- Separator optional between modules - Call all modules once on startup, then deal with delays -- Different archupdates and arch AUR updates that write to the same file so you can use only 1 of them