Email:
|
Latest update September 05 2010 13:23:54
|
|
Matrix Style XTerm Screensaver in Python
A funny python hack to look into the matrix behind a XTerm terminal window.
#!/usr/bin/python
import fcntl, termios, struct, os, time, datetime, sys, re, random
def ioctl_GWINSZ(fd):
try: ### Discover terminal width
cr = struct.unpack('hh',fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
return None
return cr
def terminal_size():
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
# ...then ctty
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
# env vars or finally defaults
try:
cr = (env['LINES'], env['COLUMNS'])
except:
cr = (25, 80)
# reverse rows, cols
return int(cr[1]), int(cr[0])
#::::::::::::::::::::.
sys.stderr.write("\x1b[2J") # Clear screen
sys.stderr.write("\x1b[37m") # Set FG
sys.stderr.write("\x1b[40m") # Set BG Black
crange=['\x1b[38;5;16m', '\x1b[38;5;22m', '\x1b[38;5;28m', '\x1b[38;5;35m', '\x1b[38;5;42m', '\x1b[38;5;49m', '\x1b[1m\x1b[38;5;49m', '\x1b[1m\x1b[37m']
#for t in range(len(crange)):
# print crange[t]+str(t),
sys.stderr.write("\x1b[37m") # Set FG
sys.stderr.write("\x1b[0m") # Set
def fx(x):
if x>0 and x<7:
return x
else:
return random.randint(1,2)
def write_text(x,y,text):
x2=-1
if random.randint(0,10)>4:
x2=x+random.randint(0,width)
if x2>width:
x2=x2%width
dly=random.uniform( 0.001 , 0.04 )
sys.stderr.write("\x1b[40m")
for m in range(len(text)-len(crange)):
for t in range(len(text)-1):
sys.stderr.write("\x1b["+str(y+t)+";"+str(x)+"H")
sys.stderr.write( crange[ fx(t-m) ]),
print text[t]
if x2>=0:
sys.stderr.write("\x1b["+str(y+t)+";"+str(x2)+"H")
sys.stderr.write( crange[ fx(t-m) ]),
print text[t]
time.sleep(dly)
v=random.randint(0,3)
for t in range(len(text)-1):
sys.stderr.write("\x1b["+str(y+t)+";"+str(x)+"H")
sys.stderr.write( crange[ v ]),
print text[t]
if x2>=0:
sys.stderr.write("\x1b["+str(y+t)+";"+str(x2)+"H")
sys.stderr.write( crange[ v ]),
print text[t]
#time.sleep(dly/2)
# reset color
sys.stderr.write("\x1b[39m") # Reset Color
# Read File content a text to be displayed
text=[]
fd = open( sys.argv[0] ,"r")
for line in fd:
#print line,
text=text+[line]
fd.close()
# 011001100100001110101001000101001010100101010010010011101010101010100101010101010101010110010110011001000011
# 00101010010101001010011001000011101010110001001010010101001010010010101001010100101010101010101001010101010101010101100101
# 0110011001001010011101010101010101001010100101010010001010101010101010101100101100110010000110010101
# 011010101001000101001110101001001000101010101010010101010101010101011001011011001100100110010000110010101
# 011001100100001110101001000101001010100101010010010011101001110101001010101010101010110010110011001000011
# 000011101010110001001010010101001011001000011010010010101001010100101010101010101001010101010101010101100101
# 011001100100001110101001010011101010101010101001010100101010010001010101010101010101100101100110010000110010101
# 01100111010101010010010000111010100100010100111010100100100011100100101010101001010101010101010101100101100110010000110010101
# 0101001001000101011001100100001110101001000101000111010100100100011100101010100101010010010011101010101010100101010101010101010110010110011001000011
# 01100110010001100010010100101010010101010101010101000011101001010101010101010101100101
# 011001100100001110101001010011101010101010101001010100101010010001010101010101010101100101100110010000110010101
# 0110011001000011101010010001010011101010101001010101010101010101100101100110010000110010101
# 011001100100001110101001000101001010100101010010010011101010101010100101010101010101010110010110011001000011
# 0010101001010100101001100100001110101001010100101010101010101001010101010101010101100101
# 0110011001001010011101010101010101001010100101010010001010101010101010101100101100110010000110010101
# 011010101001000101001110101001001000101010101010010101010101010101011001011011001100100110010000110010101
# 011001100100001110101001000101001010100101010010010011101001110101001010101010101010110010110011001000011
# 000011101010110001001010010101001011001000011010010010101001010100101010101010101001010101010101010101100101
# 0110011001000011101010010100111010101010101010010101001010100100010101010100101100010010100101010010100100101101010101100101100110010000110010101
# 01100111010101010010010000111010100100010100101010101001010101010101010101100101100110010000110010101
# 0101001001000101011001100100001110101001000101001010100101010010010011101010101010100101010101010101010110010110011001000011
# 01100110010001100010010100101010010101010101010101000011101001010101010101010101100101
# 011001100100001110101001010011101010101010101001010100101010010001010101010101010101100101100110010000110010101
# 0110011001000011101010010001010011101010101001010101010101010101100101100110010000110010101
# Display
while (1):
#for x in range(0,width):
width = terminal_size()[0]
height = terminal_size()[1]
text2use=text[random.randint(0,len(text)-1)]
if(len(text2use) > height) :
text2use=text2use[0:height+1]
x=random.randint(0,width-1)
#y=random.randint(0, height/16 )
y=0
if random.randint(0,100)>=92:
print
write_text(x,y, text2use)
|
|