You are not logged in.

#1 2018-10-27 07:16:40

maddingl
Member
Registered: 2018-02-18
Posts: 11

[SOLVED] i3blocks: way to use color in a block with 'interval=persist'

Hi,

I would like to use different colors in an i3-block that has interval=persist.
At the moment the script looks like this:

public_ip
#!/bin/bash

kill $(pgrep $(basename "$0") | grep -v $$)

function write_to_stdout {
    IP=$(dig +short myip.opendns.com @resolver1.opendns.com) && echo -e '$IP\n\n#00FF00' || echo -e 'no internet connection\n\n#FF0000'
}

write_to_stdout

TIME=$SECONDS

ip monitor all | while IFS= read -r line; do
    if [[ $SECONDS > ${TIME} ]]; then
        write_to_stdout
	SECONDS=0
	TIME=$SECONDS
    fi
done

However, since the persist-option will make i3blocks output each line individually, the colors in the third line are not seen as a third line, but as the first line of a new output, so the block shows a white '#00FF00' or '#FF0000'. As you can see, I already tried outputting everything in one echo-command, but as expected that does not change anything.

Is there any way to change this? Any other suggestions for an elegant solution?

I know I could run this script independently from i3blocks, output everything into a file (overriding it), send a signal to i3blocks each time I do that, and have the block react to that signal and read out that file. However, I already have both SIGUSR-signals in use and would prefer a more elegant solution that does not need an extra file.

*EDIT: changed 'curl ifconfig.me/ip' to 'dig +short myip.opendns.com @resolver1.opendns.com', as it is a lot faster (see this post). dig is part of the bind package in Arch.

Last edited by maddingl (2018-10-30 16:12:41)

Offline

#2 2018-10-27 07:35:21

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: [SOLVED] i3blocks: way to use color in a block with 'interval=persist'

You should probably use the json format and write everything in one line.


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#3 2018-10-27 07:53:06

maddingl
Member
Registered: 2018-02-18
Posts: 11

Re: [SOLVED] i3blocks: way to use color in a block with 'interval=persist'

progandy wrote:

You should probably use the json format and write everything in one line.

Yes, thank you, that was exactly the info I needed. It works now smile

For others that might come across this post:

I did the following:

function write_to_stdout {
    IP=$(dig +short myip.opendns.com @resolver1.opendns.com) && echo "{\"full_text\": \"$IP\", \"color\": \"#00FF00\"}" || echo "{\"full_text\": \"no internet connection\", \"color\": \"#FF0000\"}"
}

and 'format=json' in the i3blocks config in the public-ip block.

Last edited by maddingl (2018-10-30 16:13:17)

Offline

Board footer

Powered by FluxBB