相关疑难解决方法(0)

可以通过shell重定向捕获彩色输出吗?

我使用的各种bash命令 - 花哨的差异,构建脚本等,产生大量的颜色输出.

当我将此输出重定向到一个文件,然后catless更高版本的文件时,着色消失-大概B/C重定向剥离出来,告诉终端改变颜色的色码的输出的行为.

有没有办法捕获彩色输出,包括着色?

bash shell terminal redirect colors

72
推荐指数
6
解决办法
3万
查看次数

如何异步获取子进程'stdout数据?

我为我的应用程序编写了一个简单的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运行很长一段时间但我只能在完全编译后看到输出.如何异步进行?

python subprocess asynchronous stdout

10
推荐指数
1
解决办法
4617
查看次数

Ruby - 打印颜色输出

我有一个执行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

bash命令在管道时保留颜色

spec = `script -q /dev/null rake spec`
Run Code Online (Sandbox Code Playgroud)

这仍然具有不实时滚动文本的缺点.虽然它确实保留了颜色,但直到最后它才输出任何东西.

是否有更原生的方式来执行此操作以允许滚动?

ruby shell guard

5
推荐指数
1
解决办法
1530
查看次数

标签 统计

shell ×2

asynchronous ×1

bash ×1

colors ×1

guard ×1

python ×1

redirect ×1

ruby ×1

stdout ×1

subprocess ×1

terminal ×1