-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyMatrix.py
executable file
·191 lines (152 loc) · 4.62 KB
/
pyMatrix.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
import random
import time
import os
import sys
#sets defaults
colour = 92
interval = 0.052
spaceFreq = 0.8
charRange = [32,126]
#dictionary of colour names and escape codes
colours = {
'light grey' :'89',
'grey' :'90',
'red' :'91',
'green' :'92',
'yellow' :'93',
'blue' :'94',
'pink' :'95',
'light blue' :'96',
'white' :'97'
}
#help message
usage = '''Usage:
-f '<filepath>' Read parameters from <filepath>.
--file
-p Prompts for parameters.
--prompt
-c <int> Colour. <int> should be a valid colour escape code.
--colour
--color
-i <float> The interval between lines.
--int
--interval
-s <float> The frequency at which spaces are printed.
--spaces
-r <int>,<int> ASCII character range to print. [0] and [1] Should be between 32 and 255.
--range
--chars
-h Prints this help.
--help
-d Use default parameters. (colour: 92/green, interval: 0.052, space frequency: 0.8, characters: 32,126)
--default\n '''
#Argument parser
def parseArgs(args):
global colour, interval, spaceFreq, charRange
i = 0
#iterates over arguments and acts on them
for arg in args:
if arg == '-h' or arg == '--help':
#help
print(usage)
quit()
elif arg == '-f' or arg == '--file':
#gets filename and reads
file = open(args[i + 1],'r').read()
del args[i + 1]
#splits file into arguments and parses
fileArgs = file.split(' ')
parseArgs(fileArgs)
elif arg == '-p' or arg == '--prompt':
colourValid = False
intervalValid = False
spaceFreqValid = False
charRangeValid = False
#iterates over each question until user inputs a valid answer
while not colourValid:
try:
colour = input('What colour would you like? (light grey, grey, red, green, blue, light blue, pink, white, or a colour escape code.) ').lower()
try:
int(colour)
except ValueError:
colour = colours[colour]
colourValid = True
except KeyError:
print('Not a valid colour.')
while not intervalValid:
try:
interval = float(input('What would you like the interval between lines to be? (seconds) (suggested: 0.052) '))
intervalValid = True
except ValueError:
print('The interval must be a decimal number.')
while not spaceFreqValid:
try:
spaceFreq = float(input('What would you like the frequency of spaces to be? (suggested: 0.8) '))
spaceFreqValid = True
except ValueError:
print('The frequency must be a decimal number')
while not charRangeValid:
try:
charRange = input('What would characters would you like to include? (all basic ASCII characters would be 32,126) ').split(',')
charRangeValid = True
except ValueError:
print('The character range should be two ASCII values, seperated by a comma.')
elif arg == '-c' or arg == '--color' or arg == '--colour':
#gets colour
colour = args[i + 1]
del args[i + 1]
try: # trys for escape code
int(colour)
except ValueError:
try: # trys for colour name
colour = colours[colour]
except KeyError:
print('Not a valid colour.')
quit()
colourValid = True
elif arg == '-i' or arg == '--int' or arg == '--interval':
interval = float(args[i + 1]) # gets interval
del args[i + 1]
# error handling not implemented
elif arg == '-s' or arg == '--spaces':
spaceFreq = float(args[i + 1])
del args[i + 1]
# error handling not implemented
elif arg == '-r' or arg == '--range' or arg == '--chars':
charRange = args[i + 1].split(',')
del args[i + 1]
# error handling not implemented
elif arg == '-d' or arg == '--default':
pass
i += 1
def main():
global charRange
if len(sys.argv) == 1:
#if user inputs no arguments, print help and quit
print(usage)
quit()
else:
arguments = sys.argv
parseArgs(arguments) # otherwise, parse arguments
charRange = [int(i) for i in charRange]
os.popen('clear') # clears terminal window
while True:
width = os.popen('stty size').read().split(' ')[1] #get terminal width
line = ""
for i in range(int(width)):
temp = random.randint(*charRange)
line = line + str(chr(temp)) #adds characters to line until it is as wide as terminal
for i in range(int(int(width)/spaceFreq)): # replaces spaceFreq number of characters with spaces
point = random.randint(0,int(width)-1)
line = list(line)
line[point] = ' '
line = ''.join(line)
print("\033[" + str(colour) + 'm' + str(line) + "\033[0m") # joins colour escape codes and prints
time.sleep(interval) # waits
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt: #exit handling
print('\nThanks for using PyMatrix. Bye!')
sys.exit(0)