I tried to run the code:
import time
s = time.time_ns()/1000000
en = time.time_ns()/1000000
ma = 0
while True:
e = time.time_ns()/1000000
if s - en >= 1000:
ma = max(ma,int(1/(e-s)))
hi = int(((1/(e-s))/ma)*100)
jk = {hi>50:[int(((hi-50)/50)*255)-255, 255], hi<=50:[255, (((hi)/50)*255)]}[True]
print("\033[A\033[K\033[A{:>5}fps {:>3}%\033[0;0m".format(int(1/(e-s)), "\033[38;2;{};{};0m{}".format(jk[0], int(jk[1]), hi)))
en = time.time_ns()/1000000
time.sleep(0.001)
s = time.time_ns()/1000000
But when hi var was greater than 50 it showed:
95fps 214.2;255;0m58%
And what I want to happen:
75fps 75% (75% is coloured)
Can anyone explain what's happening and how to fix it?
for i in range(10)
printing something in green wheni
is 4, 5 or 6 should be enough to show your coloring problem.xxx fps y%
on each line, sometimes colored, sometimes not. The only problem I can see, related to those ANSI code, is that we go up one line each time (because of those twoESC [ A
that over-compensate the implicit\n
); I usually prefer simple\r
to print this kind of refreshed line (for i in range(10): print(f'{i}', end='\r', flush=True)
— flush=True is needed. It is usually implicit with defaultend='\n'
, but without that\n
you need to explicit it)... - 255
should be255 - ...