Skip to content

Instantly share code, notes, and snippets.

@fjarlq
Created August 13, 2012 06:52
Show Gist options
  • Save fjarlq/3337508 to your computer and use it in GitHub Desktop.
Save fjarlq/3337508 to your computer and use it in GitHub Desktop.
screen drawing bug in Panic Prompt v1.3.2
#!/usr/bin/python
#
# reproduces a screen drawing bug in Panic Prompt v1.3.2
# reported by fjarlq on August 13, 2012
#
# good v1.3.1 behavior: counts on your screen from 0 to 9, updated each second
#
# bad v1.3.2 behavior: it counts 0 and then 1 and then the counting stops
#
import os
import sys
import time
# moves the cursor to the screen coordinate (x, y)
def move_cursor(x, y):
# writes ESC [ y ; x H
sys.stdout.write("\33[" + str(y) + ";" + str(x) + "H")
# http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
def get_terminal_size():
"""
returns (lines:int, cols:int)
"""
import os, struct
def ioctl_GWINSZ(fd):
import fcntl, termios
return struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
# try stdin, stdout, stderr
for fd in (0, 1, 2):
try:
return ioctl_GWINSZ(fd)
except:
pass
# try os.ctermid()
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
try:
return ioctl_GWINSZ(fd)
finally:
os.close(fd)
except:
pass
# try `stty size`
try:
return tuple(int(x) for x in os.popen("stty size", "r").read().split())
except:
pass
# try environment variables
try:
return tuple(int(os.getenv(var)) for var in ("LINES", "COLUMNS"))
except:
pass
# i give up. return default.
return (25, 80)
# bug repro:
(lines, cols) = get_terminal_size()
# reopen stdout with buffer length 0, disabling buffering
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
# clear screen
sys.stdout.write("\33[2J")
# write ints 0 through 9 at the next to last line on the screen.
# move the cursor to the beginning of the last line each time.
for i in range(10):
move_cursor(1, lines - 1)
sys.stdout.write(str(i))
move_cursor(1, lines)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment