From 28d2190d669f25af66b0d41c1bfe4cf147221a12 Mon Sep 17 00:00:00 2001 From: Bezleputh Date: Thu, 11 Jun 2015 15:00:03 +0200 Subject: [PATCH] manage tty colummns while displaying text --- .../tools/application/formatters/iformatter.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/weboob/tools/application/formatters/iformatter.py b/weboob/tools/application/formatters/iformatter.py index d22d7d04..51f93b47 100644 --- a/weboob/tools/application/formatters/iformatter.py +++ b/weboob/tools/application/formatters/iformatter.py @@ -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