vivien / i3blocks

The hacker-friendly status_command for Sway and i3
https://vivien.github.io/i3blocks/
GNU General Public License v3.0
2.28k stars 181 forks source link

Feature request: get colors from Xresources / Bug #232

Closed vKnmnn closed 6 years ago

vKnmnn commented 7 years ago

Hi, i would like i3blocks to chance coloring according to the colors currently set in Xresources color0 through 15 trying out `color=$(xrdb -query | grep 'color0'| awk '{print $NF}')yields no result, color={$(xrdb -query | grep '*color'| awk '{print $NF}') }` gives me dark blue output in through the bar. when run through a terminal-emulator the name of the next block in the config is colored though.

Darren-Haynes commented 6 years ago

+1 I would also like a way for i3blocks to source from Xresources. I havent figured a way to do it yet either - not sure if its possible?

vKnmnn commented 6 years ago

@Darren-Haynes I went and added the aforementioned line to my blocklets, this way, each blocklet sources the color from xresources. The variable then is output with the blocklets text in pango Syntax.

Darren-Haynes commented 6 years ago

@vKnmnn I tried using both versions of the code in your opening post, but I found neither works for me. I tried them globally, and then tried on an individual blocklet with no global color set - in either case the color wasn't set.

Tried this:

[date]
1 label=
2 command=date '+%a %b %d >  %H:%M  '
3 interval=10
color=$(xrdb -query | grep '*color5'| awk '{print $NF}')
#color={$(xrdb -query | grep '*color5'| awk '{print $NF}') }

And this:

[date]
1 label=
2 command=date '+%a %b %d >  %H:%M  '
3 interval=10
#color=$(xrdb -query | grep '*color5'| awk '{print $NF}')
color={$(xrdb -query | grep '*color5'| awk '{print $NF}') }

Is that how you do it too?

vKnmnn commented 6 years ago

as far as i understand, the i3blocks.conf is not a script. that said, you cannot run code from within it, except as an example a script ~/blocks/date.sh (thats why color=.... does not work in there) the script date.sh may then hold the line col1=$(xrdb -query | grep '*color5'| awk '{print $NF}') from then on, the variable $col1 will be available within the script.

thats what i meant with adding the command to each blocklet. you will have to write a script for your blocklet, if you wish for it to colorize output with xresources colors.

your script could look something like this:

#!/bin/env bash

date=$(date '+%a %b %d >  %H:%M  ')
color=$(xrdb -query | grep '*color5'| awk '{print $NF}') 

echo "<span foreground=\"${color}\">\"${date}\"</span>"
Darren-Haynes commented 6 years ago

@vKnmnn Getting closer :) But the actual "<span foreground" and a little more is actually getting printed to the bar. I tried to play around a bit with the formatting of the echo statement but to no success. Any ideas what might be going wrong with that?

https://imgur.com/a/dC9L0

vKnmnn commented 6 years ago

You have to enable pango markup for the blocklet edit: markup=pango

vKnmnn commented 6 years ago

and as far as i can see from your screenshot, color5 is not set in your Xresources, hence, you will not get a colored output, the replacement being an empty string "". see xrdb -query for available stuff from your Xresources and grep them accordingly.

Darren-Haynes commented 6 years ago

@vKnmnn my Xcolors-schemes are set with pywal so the xrdb query still works. Enabling pango plus an extra pair of quotes in the echo statement get things working :)

Date blocklet script:

#!/bin/env/ bash
calender=" " 
getdate=$(date '+%a %b %d >  %H:%M  ')
date="$calender$getdate" 
color=$(xrdb -query | grep '*color3'| awk '{print $NF}')
echo "<span foreground=\""$color"\">"$date"</span>"

And date code in i3blocks.conf

[date]
label=                                                                            
command=. /home/darren/dotfiles/i3/date.sh
interval=10
markup=pango

Thanks so much for your help, I would not have figured this out! It was the last piece of the puzzle in making my desktop colorschemes cohesive.

vKnmnn commented 6 years ago

i see, you're using wal. this means, you can switch to an easier command to obtain your colors, without calling xrdb, grep and awk with two pipes. performance! wal generates a sourceable script for this reason now you only need to source that file like so: . /home/darren/.cache/wal/colors.sh or source /home/darren/.cache/wal/colors.sh(source is the bash version of . ) and you will be able to use the colors, as named in that file

vKnmnn commented 6 years ago

all that info is in the readme and wiki of wal, by the way. anyway, i'm glad to help

Darren-Haynes commented 6 years ago

Right doh, I was using that easier command elsewhere. It was more that I was tripped up on how to get i3blocks to use the color.

vivien commented 6 years ago

Hi. color is simply a string. what about printing your color from a script?

#!/bin/sh
echo "This is the full text"
echo "This is the short text"
xrdb -query ... # get the color from Xresources

Note that i3bar expects a hex color (like in HTML), starting with a leading hash sign. For example, #ff0000 means red.

tkkcc commented 6 years ago

use statusline setting and unset color in i3block.conf, works as expect

bar {
    colors {
        statusline $fg
    }
}
vivien commented 6 years ago

Xresources is simply a conventional file to centralize variables such as color definitions. If you want to access it for your blocks, no need to add extra layers of complexity in i3blocks configuration, simply fetch your values with xrdb -query app.Class.resource | cut -f2 from your scripts. See #198.