#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import subprocess
reload(sys)
sys.setdefaultencoding("utf-16")
HAS_COLORAMA = True
try:
import colorama
colorama.init()
except:
HAS_COLORAMA = False
p = subprocess.Popen(sys.argv[1:], stdout=subprocess.PIPE)
def decode_ansi(s):
assert s[0] == '[', "string does not start with control sequence"
for i in range(1, len(s)):
if s[i] == 'm':
break
return s[i+1:], s[1:i].split(';')
for line in p.stdout:
substrs = line.split(chr(27))
sys.stdout.write(substrs[0].decode('utf-8'))
for s in substrs[1:]:
s, cs = decode_ansi(s)
if HAS_COLORAMA:
sys.stdout.write("\033[%sm" % ';'.join(cs))
sys.stdout.write(s.decode('utf-8'))
Recommended Posts