-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPI.py
More file actions
328 lines (256 loc) · 7.18 KB
/
PI.py
File metadata and controls
328 lines (256 loc) · 7.18 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import time as t
import ESCAPES as e
import TERMINALFUNC as tf
from sys import argv
import FUNC as f
def generatePi(): #copied from stackoverflow, somewhere
k,a,b,a1,b1 = 2,4,1,12,4
while True:
p,q,k = k * k, 2 * k + 1, k + 1
a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
d,d1 = a/b, a1/b1
while d == d1:
yield int(d)
a,a1 = 10*(a % b), 10*(a1 % b1)
d,d1 = a/b, a1/b1
def initScr():
global homePos
homePos=tf.getPosition()
tf.echoKeys(disable=True)
tf.cursorVisibility(hide=True)
tf.raw(enable=True)
tf.changeStyle(reset=True)
tf.fillWithSpaces(saveCursor=False)
tf.moveCursor(home=True)
def unInitScr():
tf.cursorVisibility(show=True)
tf.echoKeys(enable=True)
tf.raw(disable=True)
tf.moveCursor(to={"row":terminalSize["rows"],"column":0})
tf.changeStyle(reset=True)
tf.fillRowWithSpaces()
tf.moveCursor(column=0)
def getColorPallete():
global digitColors
digitColors={
"1":tf.style(color8="cyan",foreground=True),
"2":tf.style(color8="magenta",foreground=True),
"3":tf.style(color8="red",foreground=True),
"4":tf.style(color8="yellow",foreground=True),
"5":tf.style(color8="green",foreground=True),
"6":tf.style(color8="cyan",foreground=True,dim=True),
"7":tf.style(color8="magenta",foreground=True,dim=True),
"8":tf.style(color8="red",foreground=True,dim=True),
"9":tf.style(color8="yellow",foreground=True,dim=True),
"0":tf.style(color8="green",foreground=True,dim=True)
}
def calculatePi():
global piDigits
global piDigit
global rateWatcher
global calculatingPi
global piGenerator
if not "piGenerator" in globals():
piGenerator=generatePi()
if not "piDigit" in globals():
piDigit=0
if not "piDigits" in globals():
piDigits=""
calculatingPi=True
rateWatcher=tf.FramerateTracker()
while calculatingPi:
rateWatcher.startFrame()
digit=next(piGenerator)
if not piDigit==0:
rateWatcher.endFrame()
rateWatcher.startFrame()
piDigits+=str(digit)
piDigit+=1
rateWatcher.endFrame()
def getBar(text,moveCursor=False):
global lastPosition
global terminalSize
renderString=""
if moveCursor:
renderString+=tf.cursor(to={"row":lastPosition[1]+1,"column":1})
renderString+=tf.style(reset=True)
renderString+=tf.style(italic=True,invert=True,bold=True)
renderString+=tf.clearer(line=True)
renderString+=f.cutString(text,terminalSize["columns"])
renderString+=tf.style(reset=True)
return renderString
def pause():
global calculatingPi, renderingPi
global paused
paused=True
calculatingPi=False
renderingPi=False
def unPause():
global paused
if paused:
f.runInParallel([[calculatePi,()]])
f.runInParallel([[renderPi,()]])
paused=False
def stopAndStartPi():
global paused
if paused:
unPause()
tf.print(getBar("",moveCursor=True))
else:
pause()
t.sleep(0.02)
tf.print(getBar("paused",moveCursor=True))
def halt():
global keyHandler
pause()
unInitScr()
keyHandler.halt()
def bellButItTakesArgs(*args):
tf.bell()
# 0-average d/s
# 1-current d/s
# 2-average s/d
# 3-current s/d
# 4-current runTime
def changeData():
global dataSource
dataSource=(dataSource+1)%5
def initKeyHandler():
global keyHandler
keyHandler=tf.KeyHandler(
{
"p":[stopAndStartPi,()],
"q":[exitMenu,()],
"s":[save,()],
"c":[changeData,()],
"default":bellButItTakesArgs
}
)
def renderPi():
global renderingPi
renderingPi=True
framerateLimiter=tf.FramerateLimiter(60)
while renderingPi:
framerateLimiter.startFrame()
tf.print(getPiRender(getPiProgressBar()))
framerateLimiter.endFrame()
framerateLimiter.delayTillNextFrame()
def save():
global piDigits, terminalSize
global lastPosition
global keyHandler
pause()
oldKeys=keyHandler.actions
keyHandler.actions={"default":bellButItTakesArgs}
t.sleep(0.02)
digitChunks=f.splitString(piDigits,(len(piDigits)//100)+1) #split digits into a 100 chunks
f.write("pi.txt","") #delete digits that are already there
tf.moveCursor(to={"row":lastPosition[1]+1,"column":0})
tf.clear(line=True)
for digit in f.everyIndexInList(digitChunks): #save the digits
f.appendTo("pi.txt",digitChunks[digit]) #append the next chunk
tf.changeStyle(italic=True,invert=True,bold=True)
tf.print("{}% done saving".format(digit+1))
tf.moveCursor(to={"row":lastPosition[1]+1,"column":0}) #go to bottom left
if pause:
unPause()
keyHandler.actions=oldKeys
def startKeyHandler():
global keyHandler
initKeyHandler()
keyHandler.start()
def getPiRender(progressBar): #(gets render of unrendered)
global terminalSize, lastPosition
global piDigits
global haltRenderPi
global renderedDigits
global digitColors
global homePos
renderString=""
if not "lastPosition" in globals():
lastPosition=[1,1]
if not "renderedDigits" in globals():
renderedDigits=0
renderString+=tf.cursor(to={"row":lastPosition[1]+1,"column":0})
renderString+=progressBar+tf.clearer(fromCursor=True,line=True,toEnd=True)+tf.style(reset=True)
haltRenderPi=False
unRenderedDigits=piDigits[renderedDigits:]
renderedDigits=len(piDigits)
if lastPosition[1]>=terminalSize["rows"]-1:
renderString+=tf.cursor(to={"row":terminalSize["rows"]-1,"column":lastPosition[0]})
else:
renderString+=tf.cursor(to={"row":lastPosition[1],"column":lastPosition[0]})
for digit in unRenderedDigits:
renderString+=digitColors[digit]+digit+tf.style(reset=True)
lastPosition[0]+=1
if lastPosition[0]==terminalSize["columns"]+1:
renderString+="\r\n\n"+progressBar+tf.style(reset=True)
renderString+=tf.cursor(column=1)+tf.cursor(up=1)
renderString+=tf.clearer(line=True)
lastPosition[0]=1
lastPosition[1]+=1
return renderString
def getPiProgressBar():
global piDigit,rateWatcher, startTime
global dataSource
if dataSource==0:
return getBar(
"currently at {}th digit of pi. Avg digits per second: {}d/s."\
.format(piDigit,rateWatcher.calculateAverageFPS())
)
elif dataSource==1:
return getBar(
"currently at {}th digit of pi. Curr digits per second: {}d/s."\
.format(piDigit,rateWatcher.calculateAverageFPS())
)
elif dataSource==2:
return getBar(
"currently at {}th digit of pi. Avg seconds per digit : {}s/d."\
.format(piDigit,rateWatcher.calculateAverageFrameTime())
)
elif dataSource==3:
return getBar(
"currently at {}th digit of pi. Curr seconds per digit : {}s/d."\
.format(piDigit,rateWatcher.calculateCurrentFrameTime())
)
elif dataSource==4:
return getBar(
"currently at {}th digit of pi. Curr runtime: {}s"\
.format(piDigit,t.perf_counter()-startTime)
)
def saveAndHalt():
save()
halt()
def exitMenu():
global keyHandler
global oldActions
pause()
oldActions=keyHandler.actions
keyHandler.actions={
"y":[saveAndHalt,()],
"n":[halt,()],
"c":[cancelExit,()],
"default":bellButItTakesArgs
}
tf.print(
getBar("Do you want to save before exiting? Press y/n to save/not save. Press c to cancel.",moveCursor=True)
)
def cancelExit():
global oldActions, keyHandler
unPause()
keyHandler.actions=oldActions
def main():
global paused
paused=False
global piGenerator, terminalSize
terminalSize=tf.getTerminalSize()
global dataSource
dataSource=0
global startTime
startTime=t.perf_counter()
getColorPallete()
initScr()
f.runInParallel([[calculatePi,()]])
f.runInParallel([[renderPi,()]])
startKeyHandler()
main()