-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchargen.py
executable file
·141 lines (134 loc) · 3.55 KB
/
chargen.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
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
#!/usr/bin/env python
from sys import argv
from sys import exit
from sys import stdout
def chargen(s, e, rm, cm, o, rc, cc, rd, cd):
try:
r = 0
while r < rc or rc < 1:
c = 0
while c < cc or cc < 1:
if c != 0:
try:
stdout.write(cd.encode('utf-8'))
except TypeError:
stdout.write(cd)
cp = (rm * r + cm * c + o) % (e - s) + s
try:
stdout.write(unichr(cp).encode('utf-8'))
except NameError:
stdout.write(chr(cp))
c += 1
try:
stdout.write(rd.encode('utf-8'))
except TypeError:
stdout.write(rd)
r += 1
except:
pass
def parseord(s):
try:
return int(s, 0)
except:
try:
return ord(s)
except:
print(s + " is not a single character")
exit()
def parseint(s):
try:
return int(s, 0)
except:
print(s + " is not an integer")
exit()
def parsechr(s):
try:
return unichr(int(s, 0))
except:
return s
def main():
s = 32
e = 127
rm = 1
cm = 1
o = 1
rc = 0
cc = 72
rd = u'\n'
cd = u''
i = 1
while i < len(argv):
arg = argv[i]
i += 1
if arg == "--help":
print("usage: chargen [<option> [<value>] [...]]")
print(" -sc <uni> -s <uni> start character (-sc <x> is the same as -fc <x>)")
print(" -ec <uni> -e <uni> end character (-ec <x> is the same as -lc <x-1>)")
print(" -fc <uni> -f <uni> first character (-fc <x> is the same as -sc <x>)")
print(" -lc <uni> -l <uni> last character (-lc <x> is the same as -ec <x+1>)")
print(" -rm <int> -y <int> difference in code point value between lines")
print(" -cm <int> -x <int> difference in code point value between columns")
print(" -so <int> -o <int> first character to print relative to start character")
print(" -rc <int> -n <int> number of lines to print")
print(" -cc <int> -w <int> number of columns to print")
print(" -hc -h print horizontal chart (sets -rm, -cm, -so, -rc, -cc)")
print(" -vc -v print vertical chart (sets -rm, -cm, -so, -rc, -cc)")
print(" -rd <str> -r <str> string to print between lines (default is newline)")
print(" -cd <str> -t <str> string to print between columns (default is empty)")
exit()
elif (arg == "-sc" or arg == "-s") and i < len(argv):
s = parseord(argv[i])
o = 0
i += 1
elif (arg == "-ec" or arg == "-e") and i < len(argv):
e = parseord(argv[i])
o = 0
i += 1
elif (arg == "-fc" or arg == "-f") and i < len(argv):
s = parseord(argv[i])
o = 0
i += 1
elif (arg == "-lc" or arg == "-l") and i < len(argv):
e = parseord(argv[i]) + 1
o = 0
i += 1
elif (arg == "-rm" or arg == "-y") and i < len(argv):
rm = parseint(argv[i])
o = 0
i += 1
elif (arg == "-cm" or arg == "-x") and i < len(argv):
cm = parseint(argv[i])
o = 0
i += 1
elif (arg == "-so" or arg == "-o") and i < len(argv):
o = parseint(argv[i])
i += 1
elif (arg == "-rc" or arg == "-n") and i < len(argv):
rc = parseint(argv[i])
i += 1
elif (arg == "-cc" or arg == "-w") and i < len(argv):
cc = parseint(argv[i])
i += 1
elif (arg == "-hc" or arg == "-h"):
rm = 1
cm = 16
o = 0
rc = 16
cc = ((e - s) + 15) >> 4
elif (arg == "-vc" or arg == "-v"):
rm = 16
cm = 1
o = 0
rc = ((e - s) + 15) >> 4
cc = 16
elif (arg == "-rd" or arg == "-r") and i < len(argv):
rd = parsechr(argv[i])
i += 1
elif (arg == "-cd" or arg == "-t") and i < len(argv):
cd = parsechr(argv[i])
i += 1
else:
print("Unknown option: " + arg)
exit()
chargen(s, e, rm, cm, o, rc, cc, rd, cd)
if __name__ == "__main__": main()