kolunmi / dwlb

Feature-Complete Bar for DWL
Other
60 stars 20 forks source link

[Question] Block scripts #11

Closed idr4n closed 1 year ago

idr4n commented 1 year ago

Hi and thanks for a really nice bar.

Do you know by any chance which block scripts are being used in the second screenshot in the README, the one with the colored blocks? Is it using someblocks?

Thanks in advance!

kolunmi commented 1 year ago

Hey! It's actually using a nushell script, which runs the blocks asynchronously:

#!/usr/bin/env nu

const delimeter = '  '

mut blocks = (open -r ~/.setup/ref/statusblocks.json | from json | insert value '' | enumerate)
mut cnt = 0

loop {
    let tmp = $cnt
    let refresh = ($blocks | par-each {|it|
        if $tmp mod $it.item.frequency == 0 {
            $it.index
        }
    })
    if not ($refresh | is-empty) {
        $blocks = ($blocks | par-each {|it|
            if $it.index in $refresh {
                update item.value $"(^$it.item.command)"
            } else {
                $it
            }
        } | sort-by index)
        dwlb -status all ($blocks | each {|it| 
            if not ($it.item.value | is-empty) {
                $"^fg\(($it.item.color)\)($it.item.value)"
            }
        } | str join $delimeter)
    }
    $cnt = $cnt + 1
    sleep 1sec
}

~/.setup/ref/statusblocks.json (which you can replace with a path of your choosing) looks something like this:

[
    {
    "command": "sb-sound",
    "frequency": 10,
    "color": "#a9a1e1"
    },
    {
    "command": "sb-wifi",
    "frequency": 10,
    "color": "#51afef"
    },
    {
    "command": "sb-cpu",
    "frequency": 3,
    "color": "#da8548"
    },
    {
    "command": "sb-mem",
    "frequency": 5,
    "color": "#98be65"
    },
    {
    "command": "sb-date",
    "frequency": 30,
    "color": "#ff6c6b"
    },
]

I can share the individual scripts used as commands if you'd like.

I'm sure you can use someblocks or even a pretty short posix shell script to achieve the same result.

Thank you!

idr4n commented 1 year ago

This is really great. Thank you so much @kolunmi đŸ™đŸ» . I will implement it right away. I was using someblocks so far but your script look much better.

I can share the individual scripts used as commands if you'd like.

That would be great, really. Although I have some scripts, I will really like to compare them with yours. One more question, in your scripts do you send signals to update the status bar (e.g. when changing volume)?

kolunmi commented 1 year ago

One more question, in your scripts do you send signals to update the status bar (e.g. when changing volume)?

I don't find it necessary since the blocks update so frequently. If you'd like that feature, however, I think someblocks is capable of receiving signals

Here are my scripts, make sure to place them in your PATH:

sb-sound:

#!/bin/sh

output="$(pamixer --get-volume)"

[ "$(pamixer --get-mute)" = 'true' ] &&
    output="${output}m"

echo " ${output}"

sb-wifi:

#!/bin/sh

awk 'NR==3 {printf(" %.1f%%\n", $3*10/7)}' /proc/net/wireless

sb-cpu:

#!/bin/sh

# help: https://www.idnt.net/en-US/kb/941772

IDLE_CACHE_FILE=/tmp/cpu-usage-idle-cache
SUM_CACHE_FILE=/tmp/cpu-usage-sum-cache

cpu=$(awk 'NR==1 {$1="";print}' /proc/stat)
cpu=${cpu#?}

idle=$(echo $cpu | cut -d' ' -f4)
sum=$(($(echo $cpu | tr ' ' +)))

if [ ! -f "$IDLE_CACHE_FILE" -o ! -f "$SUM_CACHE_FILE" ]; then
    echo $idle > "$IDLE_CACHE_FILE"
    echo $sum > "$SUM_CACHE_FILE"
    echo 'first iteration'
    exit
fi

read -r idle_last < "$IDLE_CACHE_FILE"
idle_delta=$((idle-idle_last))

read -r sum_last < "$SUM_CACHE_FILE"
sum_delta=$((sum-sum_last))

used=$((sum_delta-idle_delta))

usage_expr=100*$used/$sum_delta
usage=$(awk 'BEGIN {printf("%.1f\n", '"$usage_expr"')}')

echo " ${usage}%"

echo $idle > "$IDLE_CACHE_FILE"
echo $sum > "$SUM_CACHE_FILE"

sb-mem:

#!/bin/sh

free -h | awk '/^Mem/ { print " " $3}' | sed s/i//g

sb-thermal:

#!/bin/sh

cnt=0
celsius_expr=
for dir in /sys/class/thermal/thermal_zone*; do
    read -r val < ${dir}/temp
    celsius_expr=${celsius_expr}${val}+
    cnt=$((cnt+1))
done
celsius_expr="(${celsius_expr%?})/(${cnt}*1000)"
fahrenheit_expr=${celsius_expr}*1.8+32

fahrenheit=$(awk 'BEGIN {printf("%.1f\n", '"$fahrenheit_expr"')}')

echo "ïĄŁ ${fahrenheit}°"

sb-date:

#!/bin/sh

date '+ï„ł %_H:%M %_m/%_d/%y' | tr -s ' ' | sed 's|/ |/|'
idr4n commented 1 year ago

This is really helpful. Thanks again for this đŸ™đŸ»