You are not logged in.

#1 2015-01-15 09:07:59

komrad_toast
Member
From: New York
Registered: 2012-08-24
Posts: 36

[SOLVED]How would I highlight every other line in ZSH output?

komrad_toast wrote:

Hello everyone I have the following function in ZSH (it is part of a larger help file i've created called @help):

function mykeys #== Lists all dwm keybindings in a nice, formatted manner. Used in @help
{
	echo ""
	gawk 'BEGIN { printf("\33[1;7;32m%-18s\33[34m%-50s\n\33[0m"," Key"," Binding"," Description") }'
	grep "//#" $HOME/code/dwm/config.h | awk -F "//#" '{print $2}' | column -ts :
	echo ""
}

This function greps my dwm config.h for comments related to my keybindings and displays them in an easily readable manner.
The comments are in the following format for easy finding/parsing:

//#Mod+i:Increments number of windows in master area

The output of this function looks like this:

jauz9l.png

I was wondering if there was a way to 'highlight' every other line in this with a different background color (preferrably a light shade of grey) for even easier readability. Any help would be appreciated, and thanks in advaced!

I seem to have  solved my own problem. For anyone interested in the solution, it was just a little awk scripting. I changed the function to the following:

function mykeys #== Lists all dwm keybindings in a nice, formatted manner. Used in @help
{
	echo ""
	gawk 'BEGIN { printf("\33[1;7;32m%-18s\33[34m%-50s\n\33[0m"," Key"," Binding"," Description") }'
	_LINES=$(grep "//#" $HOME/code/dwm/config.h | awk -F "//#" '{print $2}' | column -ts :)
	echo $_LINES | gawk '{
		if (FNR % 2 == 0)                                                                             
		    printf("\033[00,40m%s\033[0m\n",$0)
		else
			printf("%s\n",$0)}'

	echo ""
}

Explanation:
The first gawk statement just prints the titles for the different columns in the screenshot, done mainly with escape sequences. Then I'm setting _LINES equal to the formatted comments from my config.h file. _LINES is then piped to the second gawk statement which uses the builtin FNR variable (which is set to the current record number of the file that you are parsing) to figure out if the line number is even or not. If it is, it sets the background color to a lighter grey, prints the entire record, then resets the color and prints a new line. Voila! Pretty nifty what a little gawk can do.

65u52g.png

Now if only i could get the background color to extend to the end of the column...

edit: duh, simple fix for that to. Just change to %s in the first printf of the if/else statement to %-67s

Last edited by komrad_toast (2015-01-15 10:30:20)

Offline

#2 2015-01-15 11:15:22

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [SOLVED]How would I highlight every other line in ZSH output?

This is a nice idea for a custom cli cheatsheet. Thanks for sharing!

Offline

#3 2015-01-15 18:21:50

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]How would I highlight every other line in ZSH output?

Why grep and awk, and not just use awk's pattern matching? And you can ask awk to do the formatting without the need for the other pipe:

awk -F'[#:]' '/\/\/#/ { printf "%s\t%s\n", $2,$3 }'

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#4 2015-01-17 08:34:50

komrad_toast
Member
From: New York
Registered: 2012-08-24
Posts: 36

Re: [SOLVED]How would I highlight every other line in ZSH output?

Thanks for that!

After your suggestion i revised the function to this:

function mykeys #== Lists all dwm keybindings in a nice, formatted manner. Used in @help
{
	gawk -F'[#:]' '
	BEGIN {
		printf("\n\33[1;7;32m%-21s\33[34m%-52s\n\33[0m"," Key"," Binding"," Description")
	}
	/\/\/#/ {
		if ((FNR % 2) == 0)
			printf("\033[0;2;40m %-21s%-51s\033[0m\n",$2,$3)
		else
			printf(" %-21s%-50s\n",$2,$3)
	}
	END {
		printf("\n")
	}' $HOME/code/dwm/config.h
}

This could actually go in my aliases file now, but i think it's fine as a function. Thanks again big_smile

EDIT: The only caveat now is that if any two of the lines in config.h that have '//#' on them contain lines WITHOUT '#//' on them between them it throws off the highlighting. This is why:
FNR is an awk built-in that represents the current record number in the current file. I am using it as if every record in the file will contain '//#', obviously not every record will. So if line 100 in my config.h is

{ MODKEY,                       XK_i,      incnmaster,     {.i = +1 } },           //#Mod+i:Increments number of windows in master area

then it will be highlighted, because it matches the pattern ('/\/\/#') and is an even line number. But if line 101 does not contain a comment that matches the pattern, and line 102 does, then you will see two highlighted rows right next to eachother in the final output. I do not know a way around this.

Last edited by komrad_toast (2015-01-17 09:18:28)

Offline

#5 2015-01-17 11:31:51

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED]How would I highlight every other line in ZSH output?

komrad_toast wrote:

I do not know a way around this.

Just use your own counter that you only increment for matching lines?

Offline

#6 2015-01-17 17:53:28

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]How would I highlight every other line in ZSH output?

Set your pattern to only match an anchor at the start of a record...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2015-01-27 17:53:32

komrad_toast
Member
From: New York
Registered: 2012-08-24
Posts: 36

Re: [SOLVED]How would I highlight every other line in ZSH output?

jasonwryan wrote:

Set your pattern to only match an anchor at the start of a record...

I could do that I guess, but that would require moving the comments to either above or below the respective keybinding, as they are currently at the end of the line. I don't really want to do that though so I can preserve the readability of my config.h. But if it really starts bothering me i'll just do that. For now the issue doesn't affect anything.

Offline

Board footer

Powered by FluxBB