11from __future__ import absolute_import
22
33import sys
4+ import os
45from optparse import Option
56
67import curtsies
78import curtsies .window
89import curtsies .terminal
10+ import curtsies .events
911Window = curtsies .window .Window
1012Terminal = curtsies .terminal .Terminal
1113
@@ -19,6 +21,8 @@ def main(args=None, locals_=None, banner=None):
1921 'scroll options' , None , [
2022 Option ('--log' , '-L' , action = 'store_true' ,
2123 help = _ ("log debug messages to scroll.log" )),
24+ Option ('--type' , '-t' , action = 'store_true' ,
25+ help = _ ("enter lines of file as though interactively typed" )),
2226 ]))
2327 if options .log :
2428 import logging
@@ -36,26 +40,9 @@ def main(args=None, locals_=None, banner=None):
3640 repl .width = columns
3741 repl .height = rows
3842
39- exit_value = 0
40- if exec_args :
41- assert options , "don't pass in exec_args without options"
42- try :
43- # THIS IS NORMAL PYTHON
44- #TODO replace this so that stdout is properly harvested for display and rewind works
45- bpargs .exec_code (repl .interp , exec_args )
46- except SystemExit , e :
47- exit_value = e .args
48- if not options .interactive :
49- #TODO treat this properly: no prompt should ever display, but stdout should!
50- array , cursor_pos = repl .paint (about_to_exit = True )
51- term .render_to_terminal (array , cursor_pos )
52- raise SystemExit (exit_value )
53- else :
54- sys .path .insert (0 , '' ) # expected for interactive sessions (python does it)
55-
56- while True :
43+ def process_event (e ):
5744 try :
58- repl .process_event (tc . get_event () )
45+ repl .process_event (e )
5946 except SystemExitFromCodeThread :
6047 #Get rid of nasty constant
6148 array , cursor_pos = repl .paint (about_to_exit = 2 )
@@ -72,5 +59,34 @@ def main(args=None, locals_=None, banner=None):
7259 # Could this be calculated in the repl, avoiding this
7360 # two-way communication?
7461
62+ exit_value = 0
63+ if exec_args :
64+ assert options , "don't pass in exec_args without options"
65+ if options .type :
66+ repl .process_event (tc .get_event ()) #first event will always be a window size set
67+ paste = curtsies .events .PasteEvent ()
68+ old_argv , sys .argv = sys .argv , exec_args
69+ paste .events .extend (open (exec_args [0 ]).read ())
70+ sys .path .insert (0 , os .path .abspath (os .path .dirname (exec_args [0 ])))
71+ process_event (paste )
72+ else :
73+ try :
74+ # THIS IS NORMAL PYTHON
75+ #TODO replace this so that stdout is properly harvested for display and rewind works
76+ bpargs .exec_code (repl .interp , exec_args )
77+ except SystemExit , e :
78+ exit_value = e .args
79+ if not options .interactive :
80+ #TODO treat this properly: no prompt should ever display, but stdout should!
81+ array , cursor_pos = repl .paint (about_to_exit = True )
82+ term .render_to_terminal (array , cursor_pos )
83+ raise SystemExit (exit_value )
84+ else :
85+ sys .path .insert (0 , '' ) # expected for interactive sessions (python does it)
86+
87+ while True :
88+ process_event (tc .get_event ())
89+
90+
7591if __name__ == '__main__' :
7692 sys .exit (main ())
0 commit comments