Start adding fmtwidth helper
This commit is contained in:
parent
6929bec4e6
commit
10c1831aad
2 changed files with 38 additions and 1 deletions
38
docs/pylib/fmtwidth.py
Normal file
38
docs/pylib/fmtwidth.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Format given files to a max width.
|
||||
|
||||
"""
|
||||
import glob
|
||||
import textwrap
|
||||
import argparse
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
parser.add_argument("files")
|
||||
parser.add_argument("-w", '--width', dest="width", type=int, default=79)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
filepaths = glob.glob(args.files)
|
||||
|
||||
wrapper = textwrap.TextWrapper(
|
||||
width=args.width,
|
||||
break_long_words=False,
|
||||
expand_tabs=True,
|
||||
)
|
||||
|
||||
count = 0
|
||||
for filepath in filepaths:
|
||||
with open(filepath, 'r') as fil:
|
||||
txt = fil.read()
|
||||
txt = "\n".join(wrapper.wrap(txt))
|
||||
with open(filepath, 'w') as fil:
|
||||
fil.write(txt)
|
||||
count += 1
|
||||
|
||||
print(f"Wrapped {count} files.")
|
||||
Loading…
Add table
Add a link
Reference in a new issue