Exit with 2 ^C in 3 seconds

This commit is contained in:
Paul Gauthier 2023-07-20 11:13:39 -03:00
parent bd1811db4d
commit 842ea95de2

View file

@ -4,6 +4,7 @@ import hashlib
import json import json
import os import os
import sys import sys
import time
import traceback import traceback
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from pathlib import Path, PurePosixPath from pathlib import Path, PurePosixPath
@ -46,6 +47,7 @@ class Coder:
functions = None functions = None
total_cost = 0.0 total_cost = 0.0
num_exhausted_context_windows = 0 num_exhausted_context_windows = 0
last_keyboard_interrupt = None
@classmethod @classmethod
def create( def create(
@ -120,7 +122,6 @@ class Coder:
self.abs_fnames = set() self.abs_fnames = set()
self.cur_messages = [] self.cur_messages = []
self.done_messages = [] self.done_messages = []
self.num_control_c = 0
self.io = io self.io = io
self.stream = stream self.stream = stream
@ -395,13 +396,22 @@ class Coder:
return return
except KeyboardInterrupt: except KeyboardInterrupt:
self.num_control_c += 1 self.keyboard_interrupt()
if self.num_control_c >= 2:
break
self.io.tool_error("^C again or /exit to quit")
except EOFError: except EOFError:
return return
def keyboard_interrupt(self):
now = time.time()
thresh = 3 # seconds
if self.last_keyboard_interrupt and now - self.last_keyboard_interrupt < thresh:
self.io.tool_error("\n\n^C KeyboardInterrupt")
sys.exit()
self.io.tool_error("\n\n^C again to exit")
self.last_keyboard_interrupt = now
def should_dirty_commit(self, inp): def should_dirty_commit(self, inp):
cmds = self.commands.matching_commands(inp) cmds = self.commands.matching_commands(inp)
if cmds: if cmds:
@ -438,8 +448,6 @@ class Coder:
self.commands, self.commands,
) )
self.num_control_c = 0
if self.should_dirty_commit(inp): if self.should_dirty_commit(inp):
self.io.tool_output("Git repo has uncommitted changes, preparing commit...") self.io.tool_output("Git repo has uncommitted changes, preparing commit...")
self.commit(ask=True, which="repo_files") self.commit(ask=True, which="repo_files")
@ -521,8 +529,6 @@ class Coder:
content = "" content = ""
if interrupted: if interrupted:
self.io.tool_error("\n\n^C KeyboardInterrupt")
self.num_control_c += 1
content += "\n^C KeyboardInterrupt" content += "\n^C KeyboardInterrupt"
self.io.tool_output() self.io.tool_output()
@ -665,6 +671,7 @@ class Coder:
else: else:
self.show_send_output(completion, silent) self.show_send_output(completion, silent)
except KeyboardInterrupt: except KeyboardInterrupt:
self.keyboard_interrupt()
interrupted = True interrupted = True
if not silent: if not silent: