251
252
diff_file.seek(0)
252
253
return patches.parse_patch(diff_file)
255
def _char_based(self):
256
# FIXME: A bit hackish to use INSIDE_EMACS here, but there is another
257
# work in progress moving this method (and more importantly prompt()
258
# below) into the ui area and address the issue in better ways.
260
return os.environ.get('INSIDE_EMACS', None) is None
254
262
def prompt(self, message):
255
263
"""Prompt the user for a character.
257
265
:param message: The message to prompt a user with.
258
266
:return: A character.
260
if not sys.stdin.isatty():
261
# Since there is no controlling terminal we will hang when trying
262
# to prompt the user, better abort now. See
268
char_based = self._char_based()
269
if char_based and not sys.stdin.isatty():
270
# Since there is no controlling terminal we will hang when
271
# trying to prompt the user, better abort now. See
263
272
# https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
264
273
# for more context.
265
274
raise errors.BzrError(gettext("You need a controlling terminal."))
266
275
sys.stdout.write(message)
267
char = osutils.getchar()
277
# We peek one char at a time which requires a real term here
278
char = osutils.getchar()
280
# While running tests (or under emacs) the input is line buffered
281
# so we must not use osutils.getchar(). Instead we switch to a mode
282
# where each line is terminated by a new line
283
line = sys.stdin.readline()
285
# XXX: Warn if more than one char is typed ?
288
# Empty input, callers handle it as enter
268
290
sys.stdout.write("\r" + ' ' * len(message) + '\r')
269
291
sys.stdout.flush()