MCPcopy Create free account
hub / github.com/ipython/ipython / _detect_screen_size

Function _detect_screen_size

IPython/core/page.py:80–125  ·  view source on GitHub ↗

Attempt to work out the number of lines on the screen. This is called by page(). It can raise an error (e.g. when run in the test suite), so it's separated out so it can easily be called in a try block.

(screen_lines_def)

Source from the content-addressed store, hash-verified

78 print(last_escape + os.linesep.join(screens[-1]))
79
80def _detect_screen_size(screen_lines_def):
81 """Attempt to work out the number of lines on the screen.
82
83 This is called by page(). It can raise an error (e.g. when run in the
84 test suite), so it's separated out so it can easily be called in a try block.
85 """
86 TERM = os.environ.get('TERM',None)
87 if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'):
88 # curses causes problems on many terminals other than xterm, and
89 # some termios calls lock up on Sun OS5.
90 return screen_lines_def
91
92 try:
93 import termios
94 import curses
95 except ImportError:
96 return screen_lines_def
97
98 # There is a bug in curses, where *sometimes* it fails to properly
99 # initialize, and then after the endwin() call is made, the
100 # terminal is left in an unusable state. Rather than trying to
101 # check every time for this (by requesting and comparing termios
102 # flags each time), we just save the initial terminal state and
103 # unconditionally reset it every time. It's cheaper than making
104 # the checks.
105 try:
106 term_flags = termios.tcgetattr(sys.stdout)
107 except termios.error as err:
108 # can fail on Linux 2.6, pager_page will catch the TypeError
109 raise TypeError('termios error: {0}'.format(err))
110
111 try:
112 scr = curses.initscr()
113 except AttributeError:
114 # Curses on Solaris may not be complete, so we can't use it there
115 return screen_lines_def
116
117 screen_lines_real,screen_cols = scr.getmaxyx()
118 curses.endwin()
119
120 # Restore terminal state in case endwin() didn't.
121 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
122 # Now we have what we needed: the screen size in rows/columns
123 return screen_lines_real
124 #print '***Screen size:',screen_lines_real,'lines x',\
125 #screen_cols,'columns.' # dbg
126
127def pager_page(strng, start=0, screen_lines=0, pager_cmd=None):
128 """Display a string, piping through a pager after a certain length.

Callers 1

pager_pageFunction · 0.85

Calls 1

formatMethod · 0.45

Tested by

no test coverage detected