##// END OF EJS Templates
Add new winconsole module and fixes to page_dumb() to improve its behavior...
fperez -
Show More
@@ -0,0 +1,43 b''
1 """
2 Set of functions to work with console on Windows.
3
4 Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
5 License: Public domain
6 """
7
8 __author__ = 'Alexander Belchenko (e-mail: bialix AT ukr.net)'
9 __license__ = 'Public domain'
10
11 import struct
12
13 try:
14 import ctypes
15 except ImportError:
16 ctypes = None
17
18 def get_console_size(defaultx=80, defaulty=25):
19 """ Return size of current console.
20
21 This function try to determine actual size of current working
22 console window and return tuple (sizex, sizey) if success,
23 or default size (defaultx, defaulty) otherwise.
24
25 Dependencies: ctypes should be installed.
26 """
27 if ctypes is None:
28 # no ctypes is found
29 return (defaultx, defaulty)
30
31 h = ctypes.windll.kernel32.GetStdHandle(-11)
32 csbi = ctypes.create_string_buffer(22)
33 res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
34
35 if res:
36 (bufx, bufy, curx, cury, wattr,
37 left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh",
38 csbi.raw)
39 sizex = right - left + 1
40 sizey = bottom - top + 1
41 return (sizex, sizey)
42 else:
43 return (defaultx, defaulty)
@@ -5,7 +5,7 b' General purpose utilities.'
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 967 2005-12-29 09:02:13Z fperez $"""
8 $Id: genutils.py 971 2005-12-29 18:30:45Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -37,6 +37,9 b' import types'
37 from IPython.Itpl import Itpl,itpl,printpl
37 from IPython.Itpl import Itpl,itpl,printpl
38 from IPython import DPyGetOpt
38 from IPython import DPyGetOpt
39
39
40 if os.name == "nt":
41 from IPython.winconsole import get_console_size
42
40 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
43 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
41 # 2.2-friendly
44 # 2.2-friendly
42 try:
45 try:
@@ -1179,6 +1182,31 b' def get_pager_start(pager,start):'
1179 return start_string
1182 return start_string
1180
1183
1181 #----------------------------------------------------------------------------
1184 #----------------------------------------------------------------------------
1185 if os.name == "nt":
1186 import msvcrt
1187 def page_more():
1188 """ Smart pausing between pages
1189
1190 @return: True if need print more lines, False if quit
1191 """
1192 Term.cout.write('---Return to continue, q to quit--- ')
1193 ans = msvcrt.getch()
1194 if ans in ("q", "Q"):
1195 result = False
1196 else:
1197 result = True
1198 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1199 return result
1200 else:
1201 def page_more():
1202 ans = raw_input('---Return to continue, q to quit--- ')
1203 if ans.lower().startswith('q'):
1204 return False
1205 else:
1206 return True
1207
1208 esc_re = re.compile(r"(\x1b[^m]+m)")
1209
1182 def page_dumb(strng,start=0,screen_lines=25):
1210 def page_dumb(strng,start=0,screen_lines=25):
1183 """Very dumb 'pager' in Python, for when nothing else works.
1211 """Very dumb 'pager' in Python, for when nothing else works.
1184
1212
@@ -1190,12 +1218,16 b' def page_dumb(strng,start=0,screen_lines=25):'
1190 if len(screens) == 1:
1218 if len(screens) == 1:
1191 print >>Term.cout, os.linesep.join(screens[0])
1219 print >>Term.cout, os.linesep.join(screens[0])
1192 else:
1220 else:
1221 last_escape = ""
1193 for scr in screens[0:-1]:
1222 for scr in screens[0:-1]:
1194 print >>Term.cout, os.linesep.join(scr)
1223 hunk = os.linesep.join(scr)
1195 ans = raw_input('---Return to continue, q to quit--- ')
1224 print >>Term.cout, last_escape + hunk
1196 if ans.lower().startswith('q'):
1225 if not page_more():
1197 return
1226 return
1198 print >>Term.cout, os.linesep.join(screens[-1])
1227 esc_list = esc_re.findall(hunk)
1228 if len(esc_list) > 0:
1229 last_escape = esc_list[-1]
1230 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1199
1231
1200 #----------------------------------------------------------------------------
1232 #----------------------------------------------------------------------------
1201 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1233 def page(strng,start=0,screen_lines=0,pager_cmd = None):
@@ -1235,7 +1267,10 b' def page(strng,start=0,screen_lines=0,pager_cmd = None):'
1235 # terminals. If someone later feels like refining it, it's not hard.
1267 # terminals. If someone later feels like refining it, it's not hard.
1236 numlines = max(num_newlines,int(len_str/80)+1)
1268 numlines = max(num_newlines,int(len_str/80)+1)
1237
1269
1238 screen_lines_def = 25 # default value if we can't auto-determine
1270 if os.name == "nt":
1271 screen_lines_def = get_console_size(defaulty=25)[1]
1272 else:
1273 screen_lines_def = 25 # default value if we can't auto-determine
1239
1274
1240 # auto-determine screen size
1275 # auto-determine screen size
1241 if screen_lines <= 0:
1276 if screen_lines <= 0:
@@ -1,5 +1,9 b''
1 2005-12-29 Fernando Perez <Fernando.Perez@colorado.edu>
1 2005-12-29 Fernando Perez <Fernando.Perez@colorado.edu>
2
2
3 * IPython/winconsole.py (get_console_size): add new winconsole
4 module and fixes to page_dumb() to improve its behavior under
5 win32. Contributed by Alexander Belchenko <bialix-AT-ukr.net>.
6
3 * IPython/Magic.py (Macro): simplified Macro class to just
7 * IPython/Magic.py (Macro): simplified Macro class to just
4 subclass list. We've had only 2.2 compatibility for a very long
8 subclass list. We've had only 2.2 compatibility for a very long
5 time, yet I was still avoiding subclassing the builtin types. No
9 time, yet I was still avoiding subclassing the builtin types. No
@@ -7,7 +11,7 b''
7 2.3-specific features quite yet).
11 2.3-specific features quite yet).
8 (magic_store): added Ville's patch for lightweight variable
12 (magic_store): added Ville's patch for lightweight variable
9 persistence, after a request on the user list by Matt Wilkie
13 persistence, after a request on the user list by Matt Wilkie
10 <maphew at gmail.com>. The new %store magic's docstring has full
14 <maphew-AT-gmail.com>. The new %store magic's docstring has full
11 details.
15 details.
12
16
13 * IPython/iplib.py (InteractiveShell.post_config_initialization):
17 * IPython/iplib.py (InteractiveShell.post_config_initialization):
@@ -9149,4 +9149,13 b' Tsai'
9149 \end_inset
9149 \end_inset
9150
9150
9151 ).
9151 ).
9152 \layout List
9153 \labelwidthstring 00.00.0000
9154
9155 Alexander\SpecialChar ~
9156 Belchenko
9157 \family typewriter
9158 <bialix-AT-ukr.net>
9159 \family default
9160 Improvements for win32 paging system.
9152 \the_end
9161 \the_end
General Comments 0
You need to be logged in to leave comments. Login now