Last active
July 31, 2021 11:51
-
-
Save tobihans/3cb9950acc1811bf82b862e252eb5d77 to your computer and use it in GitHub Desktop.
My small command-line utility to read markdown files from CLI
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
#!/usr/bin/env python3 | |
# You should have the `rich` module installed | |
# pip3 install rich | |
# See https://github.com/willmcgugan/rich | |
from rich.console import Console | |
from rich.markdown import Markdown | |
from sys import argv | |
console = Console() | |
readme = None # holds file content | |
for file in argv[1:]: | |
try: | |
readme = open(file) | |
except FileNotFoundError: | |
console.print(f"[bold red]File [/bold red] [gray][u]`{file}`" + | |
"[/u][/gray] [bold red]Not Found [/bold red]") | |
except: | |
console.print("[bold red]An error occurred while[/bold red] " + | |
f"[bold red]processing file[/bold red] [gray][u]`{file}`" + | |
"[/u][/gray]" | |
) | |
else: | |
console.print(f"\n[bold green][u]File {file}[/u][/bold green]") | |
markdown = Markdown(readme.read()) | |
console.print(markdown) | |
# Voila 😇 | |
# Just add it to your path. | |
# Maybe in $USER/bin directory, assuming it's in the PATH. | |
# You can even alias it to avoid typing the extension | |
# alias readme='$USER/bin/readme.py' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment