Page 1 of 1

[locked]colr

Posted: Sat Mar 14, 2015 11:55 pm
by dkeg
Do you tend toward continuity between your terminal applications? With every xcolor change, you are left picking up the pieces elsewhere to ensure sycnchronicity? Or lets say you change your font and want to carry it over everywhere.

Simple demo of it in action

Re: colr

Posted: Sun Mar 15, 2015 12:31 am
by Dr_Chroot
Simply astounding, dkeg. Thank you; I believe this tool shall invariably prove to be a great asset to the bbq as a collective :)

Re: colr

Posted: Sun Mar 15, 2015 9:26 am
by stark
Excellent dkeg! Thanks so much for sharing colr.

Re: colr

Posted: Sun Mar 15, 2015 11:59 am
by wuxmedia
nice theme-ing tool. kudos dkeg

Re: colr

Posted: Sun Mar 15, 2015 1:28 pm
by dkeg
Glad to hear.

Re: colr

Posted: Mon Apr 27, 2015 4:16 pm
by stark
So dkeg, as you mentioned you wanted a cleaner version of colr, i *ripped off* code from z3bra's cdump and whipped two versions of the function style2 and it pulls the colors from `xrdb -query` so saves you editing the script everytime you change colors :)

EDIT: Remove seq. Thanks pidsley :D

Without array but more readable:

Code: Select all

getcolf ()
{
	for NUM in {0..7}; do
		colf=$(xrdb -query | grep "^\*color${NUM}:" | cut -d '#' -f2)
		printf "$colf\n"
	done
}

getcolb ()
{
	for NUM in {0..7}; do
		colb=$(xrdb -query | grep "^\*color$((${NUM} + 8)):" | cut -d '#' -f2)
		printf "$colb\n"
	done
}

read bg fg <<< $(xrdb -query | grep -E "(back|fore)ground" | cut -d '#' -f2)

read blkf redf grnf ylwf bluf magf cynf whtf <<< $(getcolf)
read blkb redb grnb ylwb blub magb cynb whtb <<< $(getcolb)

#echo "$bg $fg"
#echo "$blkf $redf $grnf $ylwf $bluf $magf $cynf $whtf"
#echo "$blkb $redb $grnb $ylwb $blub $magb $cynb $whtb"
exit 0

With an array:

Code: Select all

getcolf ()
{
	for NUM in {0..7}; do
		colf[$NUM]=$(xrdb -query | grep "^\*color${NUM}:" | cut -d '#' -f2)
		printf "${colf[$NUM]}\n"
	done
}

getcolb ()
{
	for NUM in {0..7}; do
		colb[$NUM]=$(xrdb -query | grep "^\*color$((${NUM} + 8)):" | cut -d '#' -f2)
		printf "${colb[$NUM]}\n"
	done
}

Re: colr

Posted: Tue Apr 28, 2015 12:27 am
by dkeg
pulls the colors from `xrdb -query` so saves you editing the script everytime you change colors :)
Thank you stark, but I think there requires some clarification. My original script is automatic. In various scripts, I am sourcing this script and setting color/font variables.
so if I change my .Xresource file; whatever file that is sourcing the colr script automatically with no manual intervention. Same if I change font. So all popup notifications and dmenu follow suit. The only manual piece is killing and restarting bar, which I set as an alias - 'rebar'.

You posted some interesting options stark, but I'm not sure if this is the right solution. Consolidating the loop into all colors, 0-15, and passing into a single array is what I'm looking to do.

Re: colr

Posted: Tue Apr 28, 2015 6:44 am
by stark
^ Apologies, for the misstatement. The thing is I have very messy configs, ( even the color files have some commented out colors ) so this works for me. I hope this doesn't seem to be too offensive or aggressive just expressing how I handle my messy configs :)

Re: colr

Posted: Tue Apr 28, 2015 11:00 am
by dkeg
well there my friend, use this as motivation to organize!

Re: colr

Posted: Wed Apr 29, 2015 7:37 am
by stark
Well looks like this and the above method works with both style1 and style2 ! :D
Everything is in an array, bg is in 16 and fg is in 17.

EDIT: Sourcing didn't work so added a case, so no need for sourcing, just put colr in $HOME/bin or any $PATH.

Code: Select all

#!/bin/bash
getcol ()
{
	for NUM in {0..15}
	do
		col[$NUM]=$(xrdb -query | grep "^\*color${NUM}:" | cut -d '#' -f2)
	done

	read col[16] col[17] <<< $(xrdb -query | grep -E "(back|fore)ground" | cut -d '#' -f2)
}

getcol
	case $1 in
		blkf) echo "${col[0]}";;
		redf) echo "${col[1]}";;
		grnf) echo "${col[2]}";;
		ylwf) echo "${col[3]}";;
		bluf) echo "${col[4]}";;
		magf) echo "${col[5]}";;
		cynf) echo "${col[6]}";;
		whtf) echo "${col[7]}";;
		blkb) echo "${col[8]}";;
		redb) echo "${col[9]}";;
		grnb) echo "${col[10]}";;
		ylwb) echo "${col[11]}";;
		blub) echo "${col[12]}";;
		magb) echo "${col[13]}";;
		cynb) echo "${col[14]}";;
		whtb) echo "${col[15]}";;
		  bg) echo "${col[16]}";;
		  fg) echo "${col[17]}";;
		   *) echo "${col[*]}";;
	esac
exit 0
Example usage in dmenu scripts:

Code: Select all

nb=$(colr bg)
sb=$(colr bg)
nf=$(colr whtf)
sf=$(colr fg)

DMENU="/usr/bin/dmenu -nb '#$nb' -sb '#$sb' -nf '#$nf' -sf '#$sf'"

Re: colr

Posted: Wed Apr 29, 2015 11:29 am
by dkeg
Looks nice.
Thank you.

Re: colr

Posted: Wed Apr 29, 2015 4:45 pm
by stark
^ Actually Thank You for this overall idea and ofcourse this is possible due to z3bra's cdump and memory script so Thank You z3bra :)

Re: colr

Posted: Sun May 10, 2015 4:27 pm
by stark
An ugly, faster version:

Code: Select all

#!/bin/sh

bg() { xrdb -query | grep "background" | cut -d '#' -f2;}
fg() { xrdb -query | grep "foreground" | cut -d '#' -f2;}

blkf() { xrdb -query | grep "^\*color0:" | cut -d '#' -f2;  }
redf() { xrdb -query | grep "^\*color1:" | cut -d '#' -f2;  }
grnf() { xrdb -query | grep "^\*color2:" | cut -d '#' -f2;  }
ylwf() { xrdb -query | grep "^\*color3:" | cut -d '#' -f2;  }
bluf() { xrdb -query | grep "^\*color4:" | cut -d '#' -f2;  }
magf() { xrdb -query | grep "^\*color5:" | cut -d '#' -f2;  }
cynf() { xrdb -query | grep "^\*color6:" | cut -d '#' -f2;  }
whtf() { xrdb -query | grep "^\*color7:" | cut -d '#' -f2;  }
blkb() { xrdb -query | grep "^\*color8:" | cut -d '#' -f2;  }
redb() { xrdb -query | grep "^\*color9:" | cut -d '#' -f2;  }
grnb() { xrdb -query | grep "^\*color10:" | cut -d '#' -f2; }
ylwb() { xrdb -query | grep "^\*color11:" | cut -d '#' -f2; }
blub() { xrdb -query | grep "^\*color12:" | cut -d '#' -f2; }
magb() { xrdb -query | grep "^\*color13:" | cut -d '#' -f2; }
cynb() { xrdb -query | grep "^\*color14:" | cut -d '#' -f2; }
whtb() { xrdb -query | grep "^\*color15:" | cut -d '#' -f2; }


	case $1 in
		blkf) printf $(blkf);;
		redf) printf $(redf);;
		grnf) printf $(grnf);;
		ylwf) printf $(ylwf);;
		bluf) printf $(bluf);;
		magf) printf $(magf);;
		cynf) printf $(cynf);;
		whtf) printf $(whtf);;
		blkb) printf $(blkb);;
		redb) printf $(redb);;
		grnb) printf $(grnb);;
		ylwb) printf $(ylwb);;
		blub) printf $(blub);;
		magb) printf $(magb);;
		cynb) printf $(cynb);;
		whtb) printf $(whtb);;
		  bg) printf $(bg);;
		  fg) printf $(fg);;
		   *) printf "Unknown Option";;
	esac
exit 0

Re: colr

Posted: Sun May 10, 2015 4:43 pm
by pidsley
^ Why query all the colors every time you call the script, if you don't do anything except print one? Get rid of the query block at the top, and just do the query in the case statement.

Code: Select all

case $1 in
      blkf) xrdb -query | grep "^\*color0:" | cut -d '#' -f2;;
      redf) xrdb -query | grep "^\*color1:" | cut -d '#' -f2;;
      bg) xrdb -query | grep "background" | cut -d '#' -f2;;
and so on...

You could also use awk to avoid using two pipes to call grep and cut.

Code: Select all

     redf) xrdb -query | awk -F# '/^\*color1:/ {print $2}';;
Not trying to be annoying, just trying to help.

Re: colr

Posted: Sun May 10, 2015 5:11 pm
by stark
^ Awesome ! I Need to learn awk properly.

Oh and it's not querying all the colors at all according to `bash -x colr bg`. If I make them variables then it queries all the colors no matter the argument which is why I have kept them as functions. I learned this today. I just wanted to keep the case statements clean otherwise I would've definitely kept them in the case statements.

And please never hesitate to post corrections / suggestions / improvements, I'm always looking forward to learn from others :)

Re: colr

Posted: Sun May 10, 2015 5:16 pm
by pidsley
^ Oh you're right (about the functions). Sorry.

As for awk, just remember it can grep/sed/cut (and much more) and any time you process text try to find a way to awk it -- this is how I learn (not "learned" because I keep finding new ways to use it ;)

Re: colr

Posted: Sun May 10, 2015 5:29 pm
by stark
^ And when you're almost done learning awk, someone comes along and says "sed it !" - That's what she sed.

Not editing the above code as anyone can learn from my mistake :)

Code: Select all

#!/bin/sh

bg() { xrdb -query | awk -F# '/background/ {print $2}';}
fg() { xrdb -query | awk -F# '/foreground/ {print $2}';}

blkf() { xrdb -query | awk -F# '/^\*color0:/ {print $2}';  }
redf() { xrdb -query | awk -F# '/^\*color1:/ {print $2}';  }
grnf() { xrdb -query | awk -F# '/^\*color2:/ {print $2}';  }
ylwf() { xrdb -query | awk -F# '/^\*color3:/ {print $2}';  }
bluf() { xrdb -query | awk -F# '/^\*color4:/ {print $2}';  }
magf() { xrdb -query | awk -F# '/^\*color5:/ {print $2}';  }
cynf() { xrdb -query | awk -F# '/^\*color6:/ {print $2}';  }
whtf() { xrdb -query | awk -F# '/^\*color7:/ {print $2}';  }
blkb() { xrdb -query | awk -F# '/^\*color8:/ {print $2}';  }
redb() { xrdb -query | awk -F# '/^\*color9:/ {print $2}';  }
grnb() { xrdb -query | awk -F# '/^\*color10:/ {print $2}'; }
ylwb() { xrdb -query | awk -F# '/^\*color11:/ {print $2}'; }
blub() { xrdb -query | awk -F# '/^\*color12:/ {print $2}'; }
magb() { xrdb -query | awk -F# '/^\*color13:/ {print $2}'; }
cynb() { xrdb -query | awk -F# '/^\*color14:/ {print $2}'; }
whtb() { xrdb -query | awk -F# '/^\*color15:/ {print $2}'; }

	case $1 in
		blkf) printf $(blkf);;
		redf) printf $(redf);;
		grnf) printf $(grnf);;
		ylwf) printf $(ylwf);;
		bluf) printf $(bluf);;
		magf) printf $(magf);;
		cynf) printf $(cynf);;
		whtf) printf $(whtf);;
		blkb) printf $(blkb);;
		redb) printf $(redb);;
		grnb) printf $(grnb);;
		ylwb) printf $(ylwb);;
		blub) printf $(blub);;
		magb) printf $(magb);;
		cynb) printf $(cynb);;
		whtb) printf $(whtb);;
		  bg) printf $(bg);;
		  fg) printf $(fg);;
		   *) printf "Unknown Option";;
	esac
exit 0

Re: colr

Posted: Fri May 15, 2015 1:03 pm
by dkeg
colr is dead. Start a new thread.