X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=bin%2Ftext-wrap.py;h=ecfafa8f0d9d70b9ab41eca855f27962508e8419;hb=210cd046d06e1b007c9e9fc89d399e9e3d2a9ce2;hp=a8e4871206954c8defad2b11dbf028e3afb169f0;hpb=f46bd4d41cc7f243bc8a321effee5200aa69e709;p=dotfiles.git diff --git a/bin/text-wrap.py b/bin/text-wrap.py index a8e4871..ecfafa8 100755 --- a/bin/text-wrap.py +++ b/bin/text-wrap.py @@ -1,71 +1,73 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 +from __future__ import print_function import sys def usage(code=0): - sys.stderr.write("Usage: %s [-0|--null] [-n|--no-newline] [-s|--space] [-w|--width] [width]\n" % sys.argv[0]) - sys.exit(code) + sys.stderr.write("Usage: %s [-0|--null] [-n|--no-newline] [-s|--space] [-w|--width] [width]\n" % sys.argv[0]) + sys.exit(code) def get_args(): - from getopt import getopt, GetoptError - - try: - options, arguments = getopt(sys.argv[1:], "0nsw:", - ["null", "no-newline", "space", "width="]) - except GetoptError: - usage(1) - - print0 = False - newline = True - space = '' - width = None - - for option, value in options: - if option in ("-h", "--help"): - usage() - elif option in ("-0", "--null"): - print0 = True - elif option in ("-n", "--no-newline"): - newline = False - elif option in ("-s", "--space"): - space = u' ' - elif option in ("-w", "--width"): - width = int(value) - else: - usage(2) - - if arguments: - if width is not None: - usage(3) - elif len(arguments) > 1: - usage(4) - else: - width = int(arguments[0]) - - return print0, newline, space, width + from getopt import getopt, GetoptError + + try: + options, arguments = getopt( + sys.argv[1:], "0nsw:", + ["null", "no-newline", "space", "width="]) + except GetoptError: + usage(1) + + print0 = False + newline = True + space = '' + width = None + + for option, value in options: + if option in ("-h", "--help"): + usage() + elif option in ("-0", "--null"): + print0 = True + elif option in ("-n", "--no-newline"): + newline = False + elif option in ("-s", "--space"): + space = u' ' + elif option in ("-w", "--width"): + width = int(value) + else: + usage(2) + + if arguments: + if width is not None: + usage(3) + elif len(arguments) > 1: + usage(4) + else: + width = int(arguments[0]) + + return print0, newline, space, width print0, newline, space, width = get_args() -from m_lib.defenc import default_encoding -text = sys.stdin.read().decode(default_encoding) +text = sys.stdin.read() if not newline: - text = text.rstrip() + text = text.rstrip() if width: - import textwrap - text = textwrap.fill(text, width - 2*len(space), - initial_indent=space, subsequent_indent=space) - if space: - text = u'\n'.join([line+space for line in text.split(u'\n')]) + import textwrap + text = textwrap.fill( + text, width - 2*len(space), + initial_indent=space, subsequent_indent=space) + if space: + text = u'\n'.join([line+space for line in text.split(u'\n')]) else: - text = u"%s%s%s" % (space, text, space) + text = u"%s%s%s" % (space, text, space) -sys.stdout.write(text.encode(default_encoding)) +sys.stdout.write(text) if print0: - sys.stdout.write('\0') + sys.stdout.write('\0') elif newline: - print + print()