我使用的各种bash命令 - 花哨的差异,构建脚本等,产生大量的颜色输出.
当我将此输出重定向到一个文件,然后cat或less更高版本的文件时,着色消失-大概B/C重定向剥离出来,告诉终端改变颜色的色码的输出的行为.
有没有办法捕获彩色输出,包括着色?
我为我的应用程序编写了一个简单的python脚本,并预定了一些快速命令,如make等.
我编写了一个运行系统命令的函数(linux):
def runCommand(commandLine):
print('############## Running command: ' + commandLine)
p = subprocess.Popen(commandLine, shell = True, stdout = subprocess.PIPE)
print (p.stdout.read().decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
一切都很好,除了一些事情:
我正在使用cmake,它的输出是彩色的.有没有机会在输出中保存颜色?
我可以在流程完成后查看输出.例如,make运行很长一段时间但我只能在完全编译后看到输出.如何异步进行?
我有一个执行rake命令的ruby脚本(Guardfile).
guard :shell do
watch(%r{^manifests\/.+\.pp$}) do |m|
spec = `rake spec`
retval = $?.to_i
case retval
when 0
if spec.length > 0 then
puts spec
n "#{m[0]} Tests Failed!", 'Rake Spec', :pending
else
puts spec
n "#{m[0]} Tests Passed!", 'Rake Spec', :pending
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我从命令行运行'rake spec'时,输出会着色.我怎么能这样做,以便ruby脚本的输出也着色?
从命令行:

来自ruby脚本:

更新
我能够通过使用来解决问题 script
spec = `script -q /dev/null rake spec`
Run Code Online (Sandbox Code Playgroud)
这仍然具有不实时滚动文本的缺点.虽然它确实保留了颜色,但直到最后它才输出任何东西.
是否有更原生的方式来执行此操作以允许滚动?