-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyPar.py
More file actions
143 lines (133 loc) · 4.88 KB
/
PyPar.py
File metadata and controls
143 lines (133 loc) · 4.88 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
################################################################################
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
################################################################################
APPLICATION_NAME = "PyPar"
APPLICATION_VERSION = "1.0.0.0"
import sys
import random
import argparse
def find_token(s, start, maxlen):
i = start
# skip whitespace
while (i < len(s)) and (s[i].isspace()):
i += 1
# find token
length = 0
index = i
while (i < len(s)) and (not s[i].isspace()) and (length < maxlen):
length += 1
i += 1
return index, length
def format(s, indent, maxlen):
width = maxlen - indent
lines = []
line = []
line_length = 0
index, length = find_token(s, 0, width)
while length > 0:
word = s[index:index + length]
if line_length + len(word) <= width:
line.append(word)
line_length += len(word) + 1 # space
if len(word) == width: # line is full with one word
lines.append(line)
line = []
line_length = 0
width = maxlen
else:
lines.append(line)
line = []
width = maxlen
line.append(word)
line_length = len(word) + 1
index, length = find_token(s, index + length, width)
if len(line) > 0:
lines.append(line)
return lines
def line_width(line):
if len(line) == 0:
return 0
l = 0
for word in line:
l += len(word) + 1 # space
return l - 1 # no space after last word
def printx(lines, margin, indent, maxlen, justify):
line_indent = indent # indent first line
lineno = 0
while lineno < len(lines):
line = lines[lineno]
# ------- distribute remaining spaces -------
if justify:
if len(line) > 1: # avoid problems with randint()
remaining_spaces = maxlen - line_width(line) - line_indent
n = len(line) - 1
k = random.randint(0, n - 1)
spaces = [0] * n
while remaining_spaces > 0:
spaces[k] += 1
k += 1
if k == n:
k = 0
remaining_spaces -= 1
# ------- print words -------
print(' ' * (margin + line_indent), end='')
nspaces = 0
i = 0
while i < len(line):
print(line[i], end='')
if justify:
if lineno < len(lines) - 1:
if i < len(line) - 1:
nspaces = spaces[i] + 1
else:
nspaces = 0 # no spaces after last word in line
else:
nspaces = 1
else:
nspaces = 1 if i < len(line) - 1 else 0
print(' ' * nspaces, end='')
i += 1
print()
line_indent = 0
lineno += 1
def init():
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--indent", type=int, default=10, help="first line paragraph indentation")
ap.add_argument("-j", "--justify", action="store_true", help="justify text")
ap.add_argument("-m", "--margin", type=int, default=0, help="left margin of text")
ap.add_argument("-w", "--width", type=int, default=75, help="text width")
ap.add_argument("FILE", type=argparse.FileType('r', encoding="utf-8"), nargs='*', help="input files")
args = ap.parse_args()
if args.width < 1:
ap.error("-w/--width must be greater than 0")
if args.indent >= args.width:
ap.error("-i/--indent must be less than text width")
if args.margin < 0:
ap.error("-m/--margin must be greater than or equal to 0")
if len(args.FILE) == 0:
print(f"{ap.prog}: no input files")
return args
def main():
args = init()
for f in args.FILE:
for line in f:
if line == '\n':
print()
else:
lines = format(line, args.indent, args.width)
printx(lines, args.margin, args.indent, args.width, args.justify)
f.close()
if __name__ == "__main__":
main()