Do not use curses to get terminal size

libncursesw.so is very difficult to bundle in the binary build and there
are now two major versions of it in use so.5 and so.6

So instead use an IOCTL directly. Should be faster as well :)
This commit is contained in:
Kovid Goyal 2015-11-09 14:37:50 +05:30
parent 143986b3bb
commit 2b7cef952b
2 changed files with 37 additions and 13 deletions

View File

@ -228,10 +228,9 @@ def test_podofo():
print ('podofo OK!') print ('podofo OK!')
def test_terminal(): def test_terminal():
import readline, curses import readline
curses.setupterm()
del readline del readline
print ('readline and curses OK!') print ('readline OK!')
def test_markdown(): def test_markdown():
from calibre.ebooks.markdown import Markdown from calibre.ebooks.markdown import Markdown

View File

@ -325,23 +325,48 @@ def windows_terminfo():
raise Exception('stdout is not a console?') raise Exception('stdout is not a console?')
return csbi return csbi
def get_term_geometry():
import fcntl, termios, struct
def ioctl_GWINSZ(fd):
try:
return struct.unpack(b'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, b'1234'))
except Exception:
return None, None
for f in (sys.stdin, sys.stdout, sys.stderr):
lines, cols = ioctl_GWINSZ(f.fileno())
if lines is not None:
return lines, cols
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
try:
lines, cols = ioctl_GWINSZ(fd)
if lines is not None:
return lines, cols
finally:
os.close(fd)
except Exception:
pass
return None, None
def geometry(): def geometry():
if iswindows: if iswindows:
try: try:
ti = windows_terminfo() ti = windows_terminfo()
return (ti.dwSize.X or 80, ti.dwSize.Y or 80) return (ti.dwSize.X or 80, ti.dwSize.Y or 25)
except: except:
return 80, 80 return 80, 25
try:
import curses
curses.setupterm()
except:
return 80, 80
else: else:
width = curses.tigetnum('cols') or 80 try:
height = curses.tigetnum('lines') or 80 lines, cols = get_term_geometry()
return width, height if lines is not None:
return cols, lines
except Exception:
pass
return 80, 25
def test(): def test():
s = ANSIStream() s = ANSIStream()