Skip to content

Commit

Permalink
Standardize some weird interactions in the terminal.
Browse files Browse the repository at this point in the history
  • Loading branch information
culler committed Oct 26, 2024
1 parent bb3f498 commit d8495e5
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions python/tkterminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,32 +351,35 @@ def handle_keyrelease(self, event):
self.text.tag_add('history', 'output_end', Tk_.INSERT)

def handle_return(self, event=None):
# If the input consists of one complete line of code we run it,
# regardless of where the insert cursor is located. Otherwise we only
# run the code if the cursor is at the end of the input
self.clear_completions()
if self.running_code:
return 'break'
cursor = int(self.text.index(Tk_.INSERT).split('.')[0])
last = int(self.text.index(Tk_.END).split('.')[0])
first = int(self.text.index('output_end').split('.')[0])
if cursor == first == last - 1: # single line input
self.text.mark_set(Tk_.INSERT, Tk_.END)
self.text.insert(Tk_.INSERT, '\n')
if not self.running_code:
cell = self.text.get('output_end', Tk_.INSERT)
self.process_return(cell)
return 'break'

def handle_shift_return(self, event):
self.text.mark_set(Tk_.INSERT, Tk_.END)
return self.handle_return()

def process_return(self, cell):
line, pos = map(int, self.text.index(Tk_.INSERT).split('.'))
last_line = int(self.text.index(Tk_.END).split('.')[0])
if line == last_line - 1:
try:
self.interact_handle_input(cell)
except KeyboardInterrupt:
self.write('(IP) Keyboard Interrupt: ')
self.reset()
cell = self.text.get('output_end', Tk_.INSERT)
try:
self.interact_handle_input(cell)
except KeyboardInterrupt:
self.write('(IP) Keyboard Interrupt: ')
self.reset()
self.hist_pointer = 0
self.hist_stem = ''
self.interact_prompt()
self.text.see(Tk_.INSERT)
if self.IP.more:
self.text.insert(Tk_.INSERT, ' '*self._current_indent, ())
return 'break'

def handle_shift_return(self, event):
self.text.mark_set(Tk_.INSERT, Tk_.END)
return self.handle_return()

def jump_up(self, event):
return self.handle_up(event, jump=True)
Expand Down

0 comments on commit d8495e5

Please sign in to comment.