LukeSmithxyz / dwmblocks

My status bar: my build of the modular dwmblocks
GNU General Public License v2.0
430 stars 368 forks source link

How can a dwmblock update its output based on $BUTTON? #114

Closed youssefaltai closed 1 year ago

youssefaltai commented 1 year ago

Problem

I have a simple bash script myscript.sh:

#!/usr/bin/bash

clicked="none"

case $BUTTON in
    1) notify-send hello; clicked="left";;
    3) notify-send goodbye; clicked="right";;
esac

printf $clicked

and I have this in dwmblocks/blocks.h:

//Modify this file to change what commands output to your statusbar, and recompile using the make command.
static const Block blocks[] = {
    /*Icon*/    /*Command*/     /*Update Interval*/ /*Update Signal*/
    { "my script: ", "myscript.sh", 0, 12},
};

//sets delimeter between status commands. NULL character ('\0') means no delimeter.
static char delim[] = " | ";
static unsigned int delimLen = 5;

and this in dwm/config.h:

static const Button buttons[] = {
//  ...
    { ClkStatusText,        0,              Button1,        sigdwmblocks,   {.i = 1} },
    { ClkStatusText,        0,              Button2,        sigdwmblocks,   {.i = 2} },
    { ClkStatusText,        0,              Button3,        sigdwmblocks,   {.i = 3} },
// ...

What I would like to happen is for the block to display "my script: none" at first, and every time I left-click it I would like it to display "my script: left", and "my script: right" every time I right-click it.

The current behavior is that when I left-click the block, notify-send works and I get a notification saying "hello", and when I right-click it I get a notification saying "goodbye", but the $clicked variable stays the same for some reason, it doesn't change even though I update it's value after calling notify-send.

What am I missing?

Patches

I applied this patch to dwm: https://dwm.suckless.org/patches/statuscmd/dwm-statuscmd-20210405-67d76bd.diff

and this one to dwmblocks: https://dwm.suckless.org/patches/statuscmd/dwmblocks-statuscmd-20210402-96cbb45.diff

corbin-zip commented 1 year ago

Can you elaborate on this:

the $clicked variable stays the same for some reason, it doesn't change even though I update it's value after calling notify-send.

What I'm guessing is happening is that the script is executing, and $clicked's value, within the local scope of this script alone, is being updated. The script finishes and variable is now "out of scope," thus its value is lost. I think that you're expecting $clicked to retain its value between runs, which is something you can achieve by reading/storing its value in a text file, for example, rather than a temporary variable

I could be totally off-base with that guess, though, but it's the first thing that came to mind as a potential cause/solution

Corbin

youssefaltai commented 1 year ago

I think that you're expecting $clicked to retain its value between runs, which is something you can achieve by reading/storing its value in a text file, for example, rather than a temporary variable

You are right, that's exactly what was happening.

Here is the new script:

#!/usr/bin/bash

clicked=$(cat /tmp/clicked)

case $BUTTON in
    1) notify-send hello; echo "left" > /tmp/clicked;;
    3) notify-send goodbye; echo "right" > /tmp/clicked;;
esac

printf $clicked

Now it works just fine, thank you, Corbin.