Skip to content

Commit

Permalink
Addressing several issues
Browse files Browse the repository at this point in the history
These changes specifically address:

- #21: I added use of argparse & logging (#48 also mentions fitwidth but it is more global and #22 is specific about fitwidth)
- #39: By makind use of argparse, this problem goes away!  No more lame command line parsing!
- #66: I had brushed this one off but I went ahead but there was a simple fix so I was able to address it easily.
  • Loading branch information
pfuntner committed Oct 17, 2020
1 parent 5b03177 commit 79fd44c
Showing 1 changed file with 39 additions and 46 deletions.
85 changes: 39 additions & 46 deletions bin/fitwidth
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import sys
import string
import signal
import logging
import argparse
import datetime

from BrunoUtils import *
Expand All @@ -13,67 +16,57 @@ def debug(msg):
sys.stderr.write('{now}: {msg}\n'.format(**locals()))

def process(file):
global width, beginning, ending, middle, ellipses

while True:
line = file.readline()
line = file.readline().expandtabs(4)
if not line:
break

debug('read {line!r}'.format(**locals()))
line = line.replace('\r', '').replace('\n', '')
if len(line) <= width:
if len(line) <= args.width:
print(line)
else:
if beginning:
print("%s%s" % (ellipses, line[-(width-len(ellipses)):]))
elif middle:
prefix = int((width-len(ellipses))/2)
suffix = width - prefix - len(ellipses)
if args.beginning:
print("%s%s" % (ellipses, line[-(args.width-len(ellipses)):]))
elif args.middle:
prefix = int((args.width-len(ellipses))/2)
suffix = args.width - prefix - len(ellipses)
print("%s%s%s" % (line[:prefix], ellipses, line[-suffix:]))
elif ending:
print("%s%s" % (line[:width-len(ellipses)], ellipses))
elif args.ending:
print("%s%s" % (line[:args.width-len(ellipses)], ellipses))
else:
log.fatal(f'Unexpected mode: {args}')

beginning = False
middle = True
ending = False
ellipses = " ... "
parser = argparse.ArgumentParser(description='Reform lines to fit your screen width')

width = BrunoUtils.cols()
group = parser.add_mutually_exclusive_group()
group.add_argument('-m', '--middle', action='store_true', help='Eliminate the middle of lines (default mode)')
group.add_argument('-b', '--beginning', action='store_true', help='Eliminate the beginning of lines')
group.add_argument('-e', '--ending', action='store_true', help='Eliminate the ending of lines')

arg = 1
if len(sys.argv) > 1:
if sys.argv[arg] == "--beginning":
beginning = True
middle = False
ending = False
ellipses = ".... "
arg += 1
elif sys.argv[arg] == "--middle":
beginning = False
middle = True
ending = False
ellipses = " ... "
arg += 1
elif sys.argv[arg] == "--ending":
beginning = False
middle = False
ending = True
ellipses = " ...."
arg += 1
elif ((arg+1) < len(sys.argv)) and (sys.argv[arg] == "--width"):
width = int(sys.argv[arg+1])
arg += 2
elif sys.argv[arg].startswith("-"):
sys.stderr.write("Syntax: %s [--width int] [--beginning|--middle|--ending] [file ...]\n" % sys.argv[0])
exit(1)
parser.add_argument('-w', '--width', type=int, default=BrunoUtils.cols(), help='Set specific width (default: {})'.format(BrunoUtils.cols()))
parser.add_argument('files', metavar='file', nargs='*', help='Zero or more files to process')
parser.add_argument('-v', '--verbose', action='count', help='Enable debugging')
args = parser.parse_args()

logging.basicConfig(format='%(asctime)s %(levelname)s %(pathname)s:%(lineno)d %(msg)s')
log = logging.getLogger()
log.setLevel(logging.WARNING - (args.verbose or 0)*10)

signal.signal(signal.SIGPIPE, lambda signum, stack_frame: exit(0))

ellipses = " ... "

if not any([args.beginning, args.middle, args.ending]):
args.middle = True

if arg <= len(sys.argv):
if args.files:
for filename in args.files:
with open(filename, "r") as file:
process(file)
else:
if sys.stdin.isatty():
sys.stderr.write("stdin is not redirected\n")
exit(1)
process(sys.stdin)
else:
for filename in sys.argv[arg:]:
with open(filename, "r") as file:
process(file)

0 comments on commit 79fd44c

Please sign in to comment.