comparison mercurial/ui.py @ 14614:afccc64eea73

ui: use I/O descriptors internally and as a result: - fix webproto to redirect the ui descriptors instead of sys.stdout/err - fix sshserver to use the ui descriptors
author Idan Kamara <idankk86@gmail.com>
date Wed, 08 Jun 2011 01:39:20 +0300
parents 4e1ccd4c2b6d
children 8083f4d00bd1
comparison
equal deleted inserted replaced
14613:ea8938d3a5aa 14614:afccc64eea73
441 ''' 441 '''
442 if self._buffers: 442 if self._buffers:
443 self._buffers[-1].extend([str(a) for a in args]) 443 self._buffers[-1].extend([str(a) for a in args])
444 else: 444 else:
445 for a in args: 445 for a in args:
446 sys.stdout.write(str(a)) 446 self.fout.write(str(a))
447 447
448 def write_err(self, *args, **opts): 448 def write_err(self, *args, **opts):
449 try: 449 try:
450 if not getattr(sys.stdout, 'closed', False): 450 if not getattr(self.fout, 'closed', False):
451 sys.stdout.flush() 451 self.fout.flush()
452 for a in args: 452 for a in args:
453 sys.stderr.write(str(a)) 453 self.ferr.write(str(a))
454 # stderr may be buffered under win32 when redirected to files, 454 # stderr may be buffered under win32 when redirected to files,
455 # including stdout. 455 # including stdout.
456 if not getattr(sys.stderr, 'closed', False): 456 if not getattr(self.ferr, 'closed', False):
457 sys.stderr.flush() 457 self.ferr.flush()
458 except IOError, inst: 458 except IOError, inst:
459 if inst.errno not in (errno.EPIPE, errno.EIO): 459 if inst.errno not in (errno.EPIPE, errno.EIO):
460 raise 460 raise
461 461
462 def flush(self): 462 def flush(self):
463 try: sys.stdout.flush() 463 try: self.fout.flush()
464 except: pass 464 except: pass
465 try: sys.stderr.flush() 465 try: self.ferr.flush()
466 except: pass 466 except: pass
467 467
468 def interactive(self): 468 def interactive(self):
469 '''is interactive input allowed? 469 '''is interactive input allowed?
470 470
481 ''' 481 '''
482 i = self.configbool("ui", "interactive", None) 482 i = self.configbool("ui", "interactive", None)
483 if i is None: 483 if i is None:
484 # some environments replace stdin without implementing isatty 484 # some environments replace stdin without implementing isatty
485 # usually those are non-interactive 485 # usually those are non-interactive
486 return util.isatty(sys.stdin) 486 return util.isatty(self.fin)
487 487
488 return i 488 return i
489 489
490 def termwidth(self): 490 def termwidth(self):
491 '''how wide is the terminal in columns? 491 '''how wide is the terminal in columns?
519 519
520 i = self.configbool("ui", "formatted", None) 520 i = self.configbool("ui", "formatted", None)
521 if i is None: 521 if i is None:
522 # some environments replace stdout without implementing isatty 522 # some environments replace stdout without implementing isatty
523 # usually those are non-interactive 523 # usually those are non-interactive
524 return util.isatty(sys.stdout) 524 return util.isatty(self.fout)
525 525
526 return i 526 return i
527 527
528 def _readline(self, prompt=''): 528 def _readline(self, prompt=''):
529 if util.isatty(sys.stdin): 529 if util.isatty(self.fin):
530 try: 530 try:
531 # magically add command line editing support, where 531 # magically add command line editing support, where
532 # available 532 # available
533 import readline 533 import readline
534 # force demandimport to really load the module 534 # force demandimport to really load the module
535 readline.read_history_file 535 readline.read_history_file
536 # windows sometimes raises something other than ImportError 536 # windows sometimes raises something other than ImportError
537 except Exception: 537 except Exception:
538 pass 538 pass
539
540 # instead of trying to emulate raw_input, swap our in/out
541 # with sys.stdin/out
542 old = sys.stdout, sys.stdin
543 sys.stdout, sys.stdin = self.fout, self.fin
539 line = raw_input(prompt) 544 line = raw_input(prompt)
545 sys.stdout, sys.stdin = old
546
540 # When stdin is in binary mode on Windows, it can cause 547 # When stdin is in binary mode on Windows, it can cause
541 # raw_input() to emit an extra trailing carriage return 548 # raw_input() to emit an extra trailing carriage return
542 if os.linesep == '\r\n' and line and line[-1] == '\r': 549 if os.linesep == '\r\n' and line and line[-1] == '\r':
543 line = line[:-1] 550 line = line[:-1]
544 return line 551 return line