From 8f915727d15db12569cd462d0abc92c2901f5003 Mon Sep 17 00:00:00 2001 From: cubernetes Date: Sat, 12 Aug 2023 21:33:12 +0200 Subject: [PATCH] added script cut_term_width.py --- .local/bin/cut_term_width.py | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 .local/bin/cut_term_width.py diff --git a/.local/bin/cut_term_width.py b/.local/bin/cut_term_width.py new file mode 100755 index 0000000..979b8e2 --- /dev/null +++ b/.local/bin/cut_term_width.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import re +import sys + +def pad_to_width(line: bytes, width: int) -> str: + output_line = b'' + idx = 0 + buffer = b'' + while idx < len(line): + if width == 0: + break + if (match := re.match(rb'((?:\x1b\[[\d;]*\w)|[ -~])', line[idx:])): + span = match.span() + buffer += line[idx + span[0]:idx + span[1]] + idx += span[1] + if span[1] == 1: + width -= 1 + output_line += buffer + buffer = b'' + else: + idx += 1 + output_line += b'\033[m' + return output_line.decode('utf-8') + +def main() -> int: + width = int(sys.argv[1]) + lines = open(0, 'rb').read().strip(b'\n').split(b'\n') + for line in lines: + try: + print(pad_to_width(line, width), file=sys.stdout, flush=True) + except Exception as error: + print('There was an error printing the line', file=sys.stderr, flush=True) + print(str(error), file=sys.stderr, flush=True) + return 1 + return 0 + +if __name__ == '__main__': + raise SystemExit(main())