manage tty colummns while displaying text

This commit is contained in:
Bezleputh 2015-06-11 15:00:03 +02:00
commit 28d2190d66

View file

@ -48,7 +48,8 @@ else:
def readch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(fd)
tty.setcbreak(fd)
try:
c = sys.stdin.read(1)
# XXX do not read magic number
@ -95,6 +96,7 @@ class IFormatter(object):
self.interactive = False
self.print_lines = 0
self.termrows = 0
self.termcols = None
self.outfile = outfile
# XXX if stdin is not a tty, it seems that the command fails.
@ -111,12 +113,17 @@ class IFormatter(object):
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
self.termrows = right - left + 1
self.termcols = bottom - top + 1
else:
self.termrows = 80 # can't determine actual size - return default values
self.termcols = 80
else:
self.termrows = int(
subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE).communicate()[0].split()[0]
)
self.termcols = int(
subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE).communicate()[0].split()[1]
)
def output(self, formatted):
if self.outfile != sys.stdout:
@ -132,10 +139,16 @@ class IFormatter(object):
self.outfile.write('\b \b' * len(PROMPT))
self.print_lines = 0
plen = len(u'%s'.replace(self.BOLD, '').replace(self.NC, '') % line)
if isinstance(line, unicode):
line = line.encode(guess_encoding(self.outfile), 'replace')
print(line)
self.print_lines += 1
if self.termcols:
self.print_lines += int(plen/self.termcols) + 1
else:
self.print_lines += 1
def start_format(self, **kwargs):
pass