Created
February 27, 2016 17:26
-
-
Save LeviTaule/d3088bc479f3887bc532 to your computer and use it in GitHub Desktop.
Console output highlighter for Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# Highlight.ME - console output higlighter | |
# Copyright (c) 2016 Levi Taule | |
# | |
# This software is provided 'as-is', without any express or implied | |
# warranty. In no event will the authors be held liable for any damages | |
# arising from the use of this software. | |
# | |
# Permission is granted to anyone to use this software for any purpose, | |
# including commercial applications, and to alter it and redistribute it | |
# freely, subject to the following restrictions: | |
# | |
# 1. The origin of this software must not be misrepresented; you must not | |
# claim that you wrote the original software. If you use this software | |
# in a product, an acknowledgement in the product documentation would be | |
# appreciated but is not required. | |
# 2. Altered source versions must be plainly marked as such, and must not be | |
# misrepresented as being the original software. | |
# 3. This notice may not be removed or altered from any source distribution. | |
# Usage: ./higlight.me 'COMMAND' words to highlight divided by space | |
# example: ./highlight.me 'git --help' help | |
# Write out the output of the program to the temporary file 'tmp' | |
$1 >tmp | |
shift | |
y=0 | |
command="" | |
for var in "$@" | |
do | |
# Create the expression | |
d='"(' | |
for a in "$@" | |
do | |
d=$d$a"|" | |
done | |
# Removing trailing '|' | |
d=${d%?} | |
d=$d')"' | |
shift | |
# What to color. The first pass takes the contents of our 'tmp', the other ones operate on top of that | |
what="" | |
if [ "$y" -eq 0 ] | |
then | |
what='tmp' | |
else | |
what='-A10000 -B10000' | |
fi | |
# Choose a nice color | |
n=$((32+$y)) | |
# Glue it all together | |
# '-i' is for "case-insensitive" | |
command=$command"GREP_COLOR='1;$n' grep -P -i $d --color=always $what | " | |
y=$((y+1)) | |
done | |
# There is trailing ' | ' we want to remove | |
for a in [1, 2, 3] | |
do | |
command=${command%?} | |
done | |
# Finally execute the command | |
cat tmp | |
eval $command | |
rm -rf tmp | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment