##// END OF EJS Templates
Make page function more robust.
Thomas Kluyver -
Show More
@@ -68,6 +68,55 b' def page_dumb(strng, start=0, screen_lines=25):'
68 last_escape = esc_list[-1]
68 last_escape = esc_list[-1]
69 print >>io.stdout, last_escape + os.linesep.join(screens[-1])
69 print >>io.stdout, last_escape + os.linesep.join(screens[-1])
70
70
71 def _detect_screen_size(use_curses, screen_lines_def):
72 if (TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5':
73 local_use_curses = use_curses
74 else:
75 # curses causes problems on many terminals other than xterm, and
76 # some termios calls lock up on Sun OS5.
77 local_use_curses = False
78 if local_use_curses:
79 import termios
80 import curses
81 # There is a bug in curses, where *sometimes* it fails to properly
82 # initialize, and then after the endwin() call is made, the
83 # terminal is left in an unusable state. Rather than trying to
84 # check everytime for this (by requesting and comparing termios
85 # flags each time), we just save the initial terminal state and
86 # unconditionally reset it every time. It's cheaper than making
87 # the checks.
88 term_flags = termios.tcgetattr(sys.stdout)
89
90 # Curses modifies the stdout buffer size by default, which messes
91 # up Python's normal stdout buffering. This would manifest itself
92 # to IPython users as delayed printing on stdout after having used
93 # the pager.
94 #
95 # We can prevent this by manually setting the NCURSES_NO_SETBUF
96 # environment variable. For more details, see:
97 # http://bugs.python.org/issue10144
98 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None)
99 os.environ['NCURSES_NO_SETBUF'] = ''
100
101 # Proceed with curses initialization
102 scr = curses.initscr()
103 screen_lines_real,screen_cols = scr.getmaxyx()
104 curses.endwin()
105
106 # Restore environment
107 if NCURSES_NO_SETBUF is None:
108 del os.environ['NCURSES_NO_SETBUF']
109 else:
110 os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF
111
112 # Restore terminal state in case endwin() didn't.
113 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
114 # Now we have what we needed: the screen size in rows/columns
115 return screen_lines_real
116 #print '***Screen size:',screen_lines_real,'lines x',\
117 #screen_cols,'columns.' # dbg
118 else:
119 return screen_lines_def
71
120
72 def page(strng, start=0, screen_lines=0, pager_cmd=None):
121 def page(strng, start=0, screen_lines=0, pager_cmd=None):
73 """Print a string, piping through a pager after a certain length.
122 """Print a string, piping through a pager after a certain length.
@@ -123,54 +172,11 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):'
123
172
124 # auto-determine screen size
173 # auto-determine screen size
125 if screen_lines <= 0:
174 if screen_lines <= 0:
126 if (TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5':
175 try:
127 local_use_curses = use_curses
176 screen_lines += _detect_screen_size(use_curses, screen_lines_def)
128 else:
177 except Exception:
129 # curses causes problems on many terminals other than xterm, and
178 print >>io.stdout, str_toprint
130 # some termios calls lock up on Sun OS5.
179 return
131 local_use_curses = False
132 if local_use_curses:
133 import termios
134 import curses
135 # There is a bug in curses, where *sometimes* it fails to properly
136 # initialize, and then after the endwin() call is made, the
137 # terminal is left in an unusable state. Rather than trying to
138 # check everytime for this (by requesting and comparing termios
139 # flags each time), we just save the initial terminal state and
140 # unconditionally reset it every time. It's cheaper than making
141 # the checks.
142 term_flags = termios.tcgetattr(sys.stdout)
143
144 # Curses modifies the stdout buffer size by default, which messes
145 # up Python's normal stdout buffering. This would manifest itself
146 # to IPython users as delayed printing on stdout after having used
147 # the pager.
148 #
149 # We can prevent this by manually setting the NCURSES_NO_SETBUF
150 # environment variable. For more details, see:
151 # http://bugs.python.org/issue10144
152 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None)
153 os.environ['NCURSES_NO_SETBUF'] = ''
154
155 # Proceed with curses initialization
156 scr = curses.initscr()
157 screen_lines_real,screen_cols = scr.getmaxyx()
158 curses.endwin()
159
160 # Restore environment
161 if NCURSES_NO_SETBUF is None:
162 del os.environ['NCURSES_NO_SETBUF']
163 else:
164 os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF
165
166 # Restore terminal state in case endwin() didn't.
167 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
168 # Now we have what we needed: the screen size in rows/columns
169 screen_lines += screen_lines_real
170 #print '***Screen size:',screen_lines_real,'lines x',\
171 #screen_cols,'columns.' # dbg
172 else:
173 screen_lines += screen_lines_def
174
180
175 #print 'numlines',numlines,'screenlines',screen_lines # dbg
181 #print 'numlines',numlines,'screenlines',screen_lines # dbg
176 if numlines <= screen_lines :
182 if numlines <= screen_lines :
General Comments 0
You need to be logged in to leave comments. Login now