3131import bpython .curtsiesfrontend .replpainter as paint
3232import curtsies .events as events
3333from bpython .curtsiesfrontend .friendly import NotImplementedError
34- from bpython .curtsiesfrontend .coderunner import CodeRunner , FakeOutput , Unfinished
34+ from bpython .curtsiesfrontend .coderunner import CodeRunner , FakeOutput
3535
3636#TODO figure out how config.list_win_visible behaves and implement it, or stop using it
3737#TODO other autocomplete modes (also fix in other bpython implementations)
@@ -237,16 +237,17 @@ def __exit__(self, *args):
237237 sys .stderr = self .orig_stderr
238238
239239 def start_background_tasks (self ):
240- t = threading .Thread (target = self .importcompletion_thread )
240+ """starts tasks that should run on startup in the background"""
241+
242+ def importcompletion_thread ():
243+ #TODO use locks or something to avoid error on import completion right at startup
244+ while importcompletion .find_coroutine (): # returns None when fully initialized
245+ pass
246+
247+ t = threading .Thread (target = importcompletion_thread )
241248 t .daemon = True
242249 t .start ()
243250
244- def importcompletion_thread (self ):
245- """task that should run on startup in the background"""
246- #TODO use locks or something to avoid error on import completion right at startup
247- while importcompletion .find_coroutine (): # returns None when fully initialized
248- pass
249-
250251 def clean_up_current_line_for_exit (self ):
251252 """Called when trying to exit to prep for final paint"""
252253 logging .debug ('unhighlighting paren for exit' )
@@ -255,15 +256,15 @@ def clean_up_current_line_for_exit(self):
255256
256257 ## Event handling
257258 def process_event (self , e ):
258- """Returns True if shutting down, otherwise mutates state of Repl object"""
259+ """Returns True if shutting down, otherwise returns None.
260+ Mostly mutates state of Repl object"""
259261 # event names uses here are curses compatible, or the full names
260262 # for a full list of what should have pretty names, see curtsies.events.CURSES_TABLE
261263
262264 if not isinstance (e , events .Event ):
263265 self .last_events .append (e )
264266 self .last_events .pop (0 )
265267
266- result = None
267268 logging .debug ("processing event %r" , e )
268269 if isinstance (e , events .RefreshRequestEvent ):
269270 if self .status_bar .has_focus :
@@ -275,9 +276,9 @@ def process_event(self, e):
275276 logging .debug ('window change to %d %d' , e .width , e .height )
276277 self .width , self .height = e .width , e .height
277278 elif self .status_bar .has_focus :
278- result = self .status_bar .process_event (e )
279+ return self .status_bar .process_event (e )
279280 elif self .stdin .has_focus :
280- result = self .stdin .process_event (e )
281+ return self .stdin .process_event (e )
281282
282283 elif isinstance (e , events .SigIntEvent ):
283284 logging .debug ('received sigint event' )
@@ -353,8 +354,6 @@ def process_event(self, e):
353354 self .add_normal_character (e if len (e ) == 1 else e [- 1 ]) #strip control seq
354355 self .update_completion ()
355356
356- return result
357-
358357 def on_enter (self , insert_into_history = True ):
359358 self .cursor_offset_in_line = - 1 # so the cursor isn't touching a paren
360359 self .unhighlight_paren () # in unhighlight_paren
@@ -367,33 +366,6 @@ def on_enter(self, insert_into_history=True):
367366 #self._current_line = ''
368367 self .push (line , insert_into_history = insert_into_history )
369368
370- def send_to_stdout (self , output ):
371- lines = output .split ('\n ' )
372- logging .debug ('display_lines: %r' , self .display_lines )
373- self .current_stdouterr_line += lines [0 ]
374- if len (lines ) > 1 :
375- self .display_lines .extend (paint .display_linize (self .current_stdouterr_line , self .width , blank_line = True ))
376- self .display_lines .extend (sum ([paint .display_linize (line , self .width , blank_line = True ) for line in lines [1 :- 1 ]], []))
377- self .current_stdouterr_line = lines [- 1 ]
378- logging .debug ('display_lines: %r' , self .display_lines )
379-
380- def send_to_stderr (self , error ):
381- #self.send_to_stdout(error)
382- self .display_lines .extend ([func_for_letter (self .config .color_scheme ['error' ])(line )
383- for line in sum ([paint .display_linize (line , self .width )
384- for line in error .split ('\n ' )], [])])
385-
386- def send_to_stdin (self , line ):
387- if line .endswith ('\n ' ):
388- self .display_lines .extend (paint .display_linize (self .current_output_line [:- 1 ], self .width ))
389- self .current_output_line = ''
390- #self.display_lines = self.display_lines[:len(self.display_lines) - self.stdin.old_num_lines]
391- #lines = paint.display_linize(line, self.width)
392- #self.stdin.old_num_lines = len(lines)
393- #self.display_lines.extend(paint.display_linize(line, self.width))
394- pass
395-
396-
397369 def on_tab (self , back = False ):
398370 """Do something on tab key
399371 taken from bpython.cli
@@ -549,7 +521,6 @@ def run_code_and_maybe_finish(self, for_code=None):
549521 if r :
550522 logging .debug ("----- Running finish command stuff -----" )
551523 logging .debug ("saved_indent: %r" , self .saved_indent )
552- unfinished = r == Unfinished
553524 err = self .saved_predicted_parse_error
554525 self .saved_predicted_parse_error = False
555526
@@ -573,13 +544,7 @@ def keyboard_interrupt(self):
573544 self .display_lines .extend (self .display_buffer_lines )
574545 self .display_lines .extend (paint .display_linize (self .current_cursor_line , self .width ))
575546 self .display_lines .extend (paint .display_linize ("KeyboardInterrupt" , self .width ))
576-
577- self .display_buffer = []
578- self .buffer = []
579- self .cursor_offset_in_line = 0
580- self .saved_indent = 0
581- self ._current_line = ''
582- self .cursor_offset_in_line = len (self ._current_line )
547+ self .clear_current_block (remove_from_history = False )
583548
584549 def unhighlight_paren (self ):
585550 """modify line in self.display_buffer to unhighlight a paren if possible
@@ -597,10 +562,10 @@ def unhighlight_paren(self):
597562 new = bpythonparse (format (saved_tokens , self .formatter ))
598563 self .display_buffer [lineno ] = self .display_buffer [lineno ].setslice (0 , len (new ), new )
599564
600-
601- def clear_current_block (self ):
565+ def clear_current_block (self , remove_from_history = True ):
602566 self .display_buffer = []
603- [self .history .pop () for _ in self .buffer ]
567+ if remove_from_history :
568+ [self .history .pop () for _ in self .buffer ]
604569 self .buffer = []
605570 self .cursor_offset_in_line = 0
606571 self .saved_indent = 0
@@ -610,6 +575,33 @@ def clear_current_block(self):
610575 def get_current_block (self ):
611576 return '\n ' .join (self .buffer + [self ._current_line ])
612577
578+ def send_to_stdout (self , output ):
579+ lines = output .split ('\n ' )
580+ logging .debug ('display_lines: %r' , self .display_lines )
581+ self .current_stdouterr_line += lines [0 ]
582+ if len (lines ) > 1 :
583+ self .display_lines .extend (paint .display_linize (self .current_stdouterr_line , self .width , blank_line = True ))
584+ self .display_lines .extend (sum ([paint .display_linize (line , self .width , blank_line = True ) for line in lines [1 :- 1 ]], []))
585+ self .current_stdouterr_line = lines [- 1 ]
586+ logging .debug ('display_lines: %r' , self .display_lines )
587+
588+ def send_to_stderr (self , error ):
589+ #self.send_to_stdout(error)
590+ self .display_lines .extend ([func_for_letter (self .config .color_scheme ['error' ])(line )
591+ for line in sum ([paint .display_linize (line , self .width )
592+ for line in error .split ('\n ' )], [])])
593+
594+ def send_to_stdin (self , line ):
595+ if line .endswith ('\n ' ):
596+ self .display_lines .extend (paint .display_linize (self .current_output_line [:- 1 ], self .width ))
597+ self .current_output_line = ''
598+ #self.display_lines = self.display_lines[:len(self.display_lines) - self.stdin.old_num_lines]
599+ #lines = paint.display_linize(line, self.width)
600+ #self.stdin.old_num_lines = len(lines)
601+ #self.display_lines.extend(paint.display_linize(line, self.width))
602+ pass
603+
604+
613605 ## formatting, output
614606 @property
615607 def done (self ):
0 commit comments