You are not logged in.

#1 2020-08-12 15:46:19

Attius_Sullivan
Member
Registered: 2019-11-02
Posts: 53

nmcli in bash script with sxhkd

Hi,

I wrote a script displaying me the available wifi connections with dmenu using the nmcli tool.

The trouble I have up to now is that

nmcli device wifi list

normally displays nice bars like this to indicate the strength of the network:
2020-08-12-17-44.png


This works fine if the script is run manually from the terminal.
But if I now execute the script with sxhkd by a keybinding, the nice Bars are gone and only some not so nice to look at stars are displayed:
2020-08-12-17-45.png

I already tried to figure out if the environment of the script is different (from what I know it has to be so that the output of the command can be different), but up to now I didn't find anything.

Does anyone of you know what might be causing this issue?

Offline

#2 2020-08-12 16:34:31

ayekat
Member
Registered: 2011-01-17
Posts: 1,611

Re: nmcli in bash script with sxhkd

Could you please post a link to that script?


pkgshackscfgblag

Offline

#3 2020-08-12 20:37:53

Attius_Sullivan
Member
Registered: 2019-11-02
Posts: 53

Re: nmcli in bash script with sxhkd

See https://gitlab.com/AtticusSullivan/dmen … dmenu.bash but beware, the script is not really to publish (lack of comments, not so good style and not finished, even though is works up to now for me).

The part relevant for this issue is actually only the third line which passes the output of nmcli to a file, where I check what was returned. (quite similar output can be seen in the dmenu menu, but I wanted to make sure that it is no issue with dmenu)

Offline

#4 2020-08-12 20:56:13

ayekat
Member
Registered: 2011-01-17
Posts: 1,611

Re: nmcli in bash script with sxhkd

Do you see any difference in the output of

nmcli device wifi list

and

nmcli device wifi list | cat

?

My guess is that nmcli detects it's not printing to a TTY, and thus adapts its output (to be more easily processable).

If you want bars to show up like that, I guess you may need some intermediate processing between the nmcli output and dmenu.
Alternatively, make nmcli believe it prints to a TTY, but that would be a hack (and I don't know immediately of a way to achieve that).


pkgshackscfgblag

Offline

#5 2020-08-12 21:02:47

Attius_Sullivan
Member
Registered: 2019-11-02
Posts: 53

Re: nmcli in bash script with sxhkd

Hm,

 nmcli device wifi list

and

nmcli devce wifi list | cat 

produce the same output here (just the colors are gone, but that is something nmcli would have a flag for to activate that).

So still searching for a reason

Offline

#6 2020-08-15 04:44:57

chaseleif
Member
From: Texas
Registered: 2020-08-01
Posts: 18

Re: nmcli in bash script with sxhkd

There is an option flag to enable colors, perhaps try that?
It says the default option is auto, which enables colors when it detects the output is stdout.

https://developer.gnome.org/NetworkMana … nmcli.html

The sxhkd does a run command for you when you do the bound key, this separation may make the auto option disable the special text.

Offline

#7 2020-08-15 08:38:11

Attius_Sullivan
Member
Registered: 2019-11-02
Posts: 53

Re: nmcli in bash script with sxhkd

Hm this could be it, but it's not the -c flag to fix it. After adding it, the only difference is that the output of the script is now always in colour (stars are remaining instead of bars)

Offline

#8 2020-08-15 19:13:25

chaseleif
Member
From: Texas
Registered: 2020-08-01
Posts: 18

Re: nmcli in bash script with sxhkd

Looking at their code from https://gitlab.freedesktop.org/NetworkM … orkManager

At the bottom of ./examples/lua/lgi/show-wifi-networks.lua the comments say it calls setlocale, otherwise it will think it is ASCII only and will go to the fallback characters.

Then, in ./clients/common/nm-client-utils.c at line 523, you can see the special characters for the signal bars instead of asterisks.... before it returns the fancy bars, it will return nm_utils_wifi_strength_bars() at line 526 if can_show_graphics() is false.

The can_show_graphics () is defined just above that, at line 494...
you can see that the first run the can_show_graphics_set is false, so the first condition isn't met, at 502 it sets the return value to can_show_utf8() .... then one more check at 505 can set the return value to false.

The can_show_utf8 is defined just above at line 471.

The function that it calls as the fallback (when the fancy stuff is turned off) is nm_utils_wifi_strength_bars(strength) ... you can find this at ./libnm-core/nm-utils.c at line 3983 ... and this just prints the asterisks.

So, it looks like it is something to do with the locale in addition to whether it detects the console environment.

It talks about locale environment variables further down the doc page I last posted.

You could play around with locale settings and see if you can get the bars ... maybe having it explicitly set will make it work.

You could also make a small shell script which just sets the locale before calling the wifi tool, and bind the key to do your shell script instead of directly calling the wifi tool

Last edited by chaseleif (2020-08-15 20:06:45)

Offline

#9 2020-08-16 10:55:45

Attius_Sullivan
Member
Registered: 2019-11-02
Posts: 53

Re: nmcli in bash script with sxhkd

Thanks for digging into the code. Based on https://gitlab.freedesktop.org/NetworkM … ils.c#L480, I checked which influences the output of

g_get_charset

and came up with https://developer.gnome.org/glib/stable … et-charset

I then changed the beginning of my script to

#!/bin/bash -i

export LC_MESSAGES=en_DK.UTF-8
export LC_ALL=en_GB.UTF-8
export LANG=en_GB.UTF-8
export CHARSET=UTF-8

nmcli device wifi list > ~/tmp

But it didn't help (even though from what I read it should). Am I doing something wrong?

Offline

#10 2020-08-16 18:29:11

chaseleif
Member
From: Texas
Registered: 2020-08-01
Posts: 18

Re: nmcli in bash script with sxhkd

Maybe it's because you're redirecting the output to a file, the stdout is changed to the file. Unrelated, but I'd make the tmp file name more specific.

Without trying to provide alternate definitions of library functions or modifying the other tools, ayekat's suggestion of intermediate processing may be what you need.

The text file is written with asterisks, so when you parse the text file you will come across series of asterisks. Replace the asterisks with the unicode bars.

If you do a matching/replacement, start with the longest series first and work your way down to just one asterisk.

Last edited by chaseleif (2020-08-16 18:31:27)

Offline

#11 2020-08-17 21:45:55

Attius_Sullivan
Member
Registered: 2019-11-02
Posts: 53

Re: nmcli in bash script with sxhkd

Well, its still weird since I'm redirecting to a file when I call the script from a terminal too and there it works. And the redirection to the tmp file is only to have some output which is not printed by dmenu (since I was afraid there somehow could be a font issue) normally this is not part of the script.

Since I still cannot figure out what is causing my problem, I wrote this:

nmcli device wifi list | \
        sed "s/^\(..*\)\*\{4,4\}/\1▂▄▆█/g" | \
        sed "s/^\(..*\)\*\{3,3\}/\1▂▄▆_/g" | \
        sed "s/^\(..*\)\*\{2,2\}/\1▂▄__/g" | \
        sed "s/^\(..*\)\*\{1,1\}/\1▂___/g"

which does the string replacement (the ^\(..\)* at the beginning is just so that the star indicating the active connection is not replaced since it is directly at the beginning of the line)

Feel free to give feedback if you want and if someone still has an idea what might be causing this weird behaviour at the first place, let me know.

Offline

#12 2023-01-19 22:16:03

ArcWand
Member
Registered: 2023-01-19
Posts: 4

Re: nmcli in bash script with sxhkd

Can confirm that running the `nmcli` command from a TTY will cause it to show asterisks, while running from a terminal emulator will not. I think the `sed` solution is probably the cleanest way to do this.

Offline

#13 2023-01-19 22:19:14

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,646

Re: nmcli in bash script with sxhkd

Closing this old thread.

Offline

Board footer

Powered by FluxBB