From 46a10a698d53e4cf4af997400193ab9cb0e003e5 Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Wed, 9 Nov 2022 14:08:23 -0700 Subject: [PATCH] Add fitwidth.py script --- bin/fitwidth.py | 51 +++++++++++++++++++++++++++++++++++++++++++ bin/lyrics_to_html.py | 34 ++++++++++------------------- 2 files changed, 62 insertions(+), 23 deletions(-) create mode 100755 bin/fitwidth.py diff --git a/bin/fitwidth.py b/bin/fitwidth.py new file mode 100755 index 0000000..5ff56c4 --- /dev/null +++ b/bin/fitwidth.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +import argparse, sys + +# Parse args +parser = argparse.ArgumentParser(prog="FitWidth v1.0.0", + description='Fit text (stdin|file) to a given character width'); +parser.add_argument('-t', '--truncate', action='store_true', + help='Truncate longer lines with a "..."'); +parser.add_argument('-w', '--wrap', action='store_true', + help='Wrap longer lines across two lines with a ">" at the end'); +parser.add_argument('width', type=int, + help='Wrap longer lines across two lines with a ">" at the end'); +parser.add_argument('file', type=argparse.FileType('r'), nargs='?', + help="File to read from, instead of stdin"); +args = parser.parse_args(); + +# Validate args +if not args.truncate and not args.wrap: + args.truncate = True + +if args.width < 10: + print("Minimum width is 10") + exit(1); + +if args.file is not None: + file = args.file +elif not sys.stdin.isatty(): + file = sys.stdin +else: + print("Please provide a file or pipe in text to fit") + exit(1) + +# Print out file +w = args.width + +for line in file: + line = line.rstrip() + + if len(line) > w: + if args.truncate: + print(line[:w-3] + "...") + else: + i = w-1 + print(line[:i] + ">") + while i < len(line): + print(">" + line[i:i+w-1]) + i += w-1 + else: + print(line) + +file.close() diff --git a/bin/lyrics_to_html.py b/bin/lyrics_to_html.py index 0e9a4be..68ea0c9 100755 --- a/bin/lyrics_to_html.py +++ b/bin/lyrics_to_html.py @@ -67,10 +67,6 @@ html_template_bottom = lambda title, url : f""" """ + """ """