Skip to content
Browse files

try to work more intelligently when pseudoterminal output is directed…

… into a pipe :)
  • Loading branch information...
1 parent a346cff commit dbaa91d182d12a6bbbebdad725af23190b5aea0f @ztane ztane committed Feb 12, 2013
Showing with 29 additions and 9 deletions.
  1. +21 −7 peacock/intercept.py
  2. +8 −2 peacock/peacock.py
View
28 peacock/intercept.py
@@ -46,18 +46,23 @@ def spawn(self, argv=None):
if not argv:
argv = [os.environ['SHELL']]
+ self.isatty = os.isatty(pty.STDOUT_FILENO)
pid, master_fd = pty.fork()
self.master_fd = master_fd
if pid == pty.CHILD:
os.execlp(argv[0], *argv)
old_handler = signal.signal(signal.SIGWINCH, self._signal_winch)
- try:
- mode = tty.tcgetattr(pty.STDIN_FILENO)
- tty.setraw(pty.STDIN_FILENO)
- restore = 1
- except tty.error: # This is the same as termios.error
- restore = 0
+ restore = 0
+ if self.isatty:
+ try:
+ mode = tty.tcgetattr(pty.STDIN_FILENO)
+ tty.setraw(pty.STDIN_FILENO)
+ restore = 1
+
+ except tty.error: # This is the same as termios.error
+ pass
+
self._init_fd()
try:
self._copy()
@@ -90,7 +95,13 @@ def _set_pty_size(self):
# Get the terminal size of the real terminal, set it on the pseudoterminal.
buf = array.array('h', [0, 0, 0, 0])
- fcntl.ioctl(pty.STDOUT_FILENO, termios.TIOCGWINSZ, buf, True)
+ if self.isatty:
+ fcntl.ioctl(pty.STDOUT_FILENO, termios.TIOCGWINSZ, buf, True)
+ else:
+ # set default size...
+ buf[0] = 25
+ buf[1] = 80
+
fcntl.ioctl(self.master_fd, termios.TIOCSWINSZ, buf)
def _copy(self):
@@ -142,6 +153,9 @@ def write_stdout(self, data):
'''
Writes to stdout as if the child process had written the data.
'''
+ if not self.isatty:
+ data = data.replace('\r\n', '\n')
+
os.write(pty.STDOUT_FILENO, data)
def write_master(self, data):
View
10 peacock/peacock.py
@@ -366,15 +366,21 @@ def __init__(self, prog):
super(Colorizer, self).__init__()
def do_process(self, data):
- lines = data.split('\n')
+ line_end = ''
+ if data.endswith('\n'):
+ line_end = '\n'
+ if data.endswith('\r\n'):
+ line_end = '\r\n'
+
+ lines = data.splitlines(False)
for i in range(len(lines)):
for j in self.specs:
replaced = j.do_sub(lines[i])
if replaced:
lines[i] = replaced
break
- return '\n'.join(lines)
+ return '\r\n'.join(lines) + line_end
initialize()

0 comments on commit dbaa91d

Please sign in to comment.
Something went wrong with that request. Please try again.