You are not logged in.
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:
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.
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)
(github)
Offline
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 }'
Offline
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
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)
(github)
Offline
I do not know a way around this.
Just use your own counter that you only increment for matching lines?
Offline
Set your pattern to only match an anchor at the start of a record...
Offline
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.
(github)
Offline