Skip to content

Instantly share code, notes, and snippets.

@phwelo
Created August 11, 2022 14:39
Show Gist options
  • Save phwelo/9846256c2ced628896eb412745dbecde to your computer and use it in GitHub Desktop.
Save phwelo/9846256c2ced628896eb412745dbecde to your computer and use it in GitHub Desktop.
Highlight - Simple grep tool that highlights instead of displaying only results
#!/usr/bin/env python3
from colorama import init, Fore, Back, Style
import sys
import argparse
init(autoreset=True)
what_color = Fore.BLACK + Back.WHITE
parser = argparse.ArgumentParser(exit_on_error=False)
parser.add_argument('search_string')
args = parser.parse_args()
for line in sys.stdin:
if args.search_string in str(line):
search_string = args.search_string
highlight_string = what_color + search_string + Style.RESET_ALL
splitted = line.split(search_string)
unsplit = highlight_string.join(splitted)
print(unsplit)
else:
print(line)%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment