forked from DrazharLn/scient-unofficial-smacx-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineshortener.py
41 lines (29 loc) · 1.09 KB
/
lineshortener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def trimline(line, maxlen=124):
"""string -> string[]
Recursively shorten string if it is longer than maxlen. Return list of shortened lines
If a line cannot be shortened an exception will be thrown.
Default maxlen comes from the max line length in the original english helpx.txt:
for f in \#\#current\ patch\ files/EN_orig/SMACX/*; do
echo -e "$(LC_ALL=latin1 awk '{print length($0)}' < "$f" |sort -n|tail -n1)\t$f";
done | sort -n
"""
if len(line) <= maxlen:
return [line]
# Find the first space before maxlen and replace with \r\n
splitpt = line.rindex(' ', 0, maxlen)
before = line[:splitpt] + '\n'
after = line[splitpt+1:]
tmp = [before]
tmp.extend(trimline(after, maxlen))
return tmp
def main():
import sys
filename = sys.argv[1]
with open(filename, encoding="latin1") as f:
lines = f.readlines()
newlines = []
for line in lines:
newlines.extend(trimline(line))
with open(filename, "w", encoding="latin1", newline="\r\n") as f:
f.writelines(newlines)
main()