在rails应用程序中使用watchr进行彩色输出

时间:2021-02-06 00:17:33

I'm using watchr in my rails3 app since I can't get autotest to work right. Watchr has been great but the only problem is I can't get colored output. I'm using this as my watchr file

我在rails3应用中使用watchr,因为我不能让autotest正常工作。Watchr很好,但唯一的问题是我无法获得彩色输出。我用这个作为我的watchr文件。

If I run my tests manually via "rspec spec/" then I get colored output. However, letting watchr run the tests (via modifying one of my files) the output is no longer in color.

如果我通过“rspec spec/”手动运行我的测试,那么我将得到有颜色的输出。但是,让watchr运行测试(通过修改我的一个文件),输出不再是彩色的。

One thing I notice is that this part seems to prevent colored output somehow

我注意到这部分似乎以某种方式阻止了彩色输出

def run(cmd)
  puts(cmd)
  `#{cmd}`
end

Normally, watchr runs the tests using this (in my watchr file)

通常,watchr使用这个(在我的watchr文件中)运行测试

result = run "rake spec"

But if I instead do the following I get colored output.

但是如果我这样做的话,我就会得到有颜色的输出。

result = system "rake spec"

Only problem is that the above line no longer captures the output to "result", which in turn mans growl notifications no longer work.

唯一的问题是,上面的一行不再捕获“结果”的输出,而“结果”通知反过来就不起作用了。

Any ideas on how I can have the best of both worlds? Growl notifications AND my test output in color?

有没有什么办法可以让我同时拥有这两个世界的优点?咆哮通知和我的测试输出颜色?

3 个解决方案

#1


3  

I just ran into this issue as well. The solution is to provide the --tty option, in addition to --color, to the rspec command line invocation, e.g.:

我也遇到了这个问题。解决方案是为rspec命令行调用提供——tty选项,除此之外——颜色,例如:

result = run("bundle exec rspec --color --tty #{spec}")

According to this thread on the rspec google group:

根据rspec谷歌组的这个线程:

The --tty option is there to tell RSpec that the output is being streamed to a terminal as opposed to a file, in which case we don't want to print the color codes even if --color is invoked.

tty选项是告诉RSpec输出流到终端而不是文件,在这种情况下,我们不希望打印颜色代码,即使——颜色被调用。

If you then "puts" the result back to the terminal, you'll see the original coloring. If you want to parse that output for Growl, you'll want to strip out the ansi color codes using the strip_ansi method Bill Turner provided.

如果您将结果“放置”回终端,您将看到原始的着色。如果您想要解析咆哮的输出,您需要使用Bill Turner提供的strip_ansi方法去掉ansi颜色代码。

Hope that helps!

希望会有帮助!

#2


0  

This is how I have something along those lines it in my .watchr file:

这就是我的。watchr文件中的内容:

def run(cmd)
  `#{cmd}`
end

def run_spec_file(spec)
  system('clear')
  puts "Running #{spec}"
  result = run("bundle exec rspec #{spec}")
  puts result
  growl result.split("\n").last rescue nil
end

And the growl bit is this:

咆哮的是:

def growl(message)
  growlnotify = `which growlnotify`.chomp
  unless growlnotify.empty?
    title = "Test Results"
    image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/ruby_green.png" : "~/.watchr_images/ruby_red.png"
    options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{strip_ansi(message)}' '#{title}'"
    run("#{growlnotify} #{options} &")
  end
end

Which expects some images in a ~/.watchr_images/ directory on your dev box, and the growlnotify binary installed as well.

它期望在~/中有一些图像。开发框上的watchr_images/目录,以及安装的growlnotify二进制文件。

EDIT: Oh, and you may also want to check what's in your spec.opts file if there is one - it may not have --color specified.

编辑:哦,您可能还想检查您的spec.opts文件中的内容,如果有一个——它可能没有——指定了颜色。

And the strip_ansi in the growl method is used to strip the color codes so it will display in the growl notice.

使用growl方法中的strip_ansi来去除颜色代码,以便在growl通知中显示。

def strip_ansi(m)
  m.gsub(/\e\[[^m]*m/, '')
end

#3


0  

The output not showing up in color has to do with saving the test output to a variable. The way I have watchr setup it save "rake spec" output to a variable, then this variable is output to the terminal using "puts".

没有显示颜色的输出与将测试输出保存到一个变量有关。我设置watchr的方式是将“rake spec”输出保存到一个变量,然后这个变量使用“put”输出到终端。

def run(cmd)
  puts(cmd)
  `#{cmd}`
end

# what's called to run my tests
result = run "rake spec"

# then output it
puts result

Now I just need to figure out how to output the info in color!

现在我只需要弄清楚如何输出颜色的信息!

#1


3  

I just ran into this issue as well. The solution is to provide the --tty option, in addition to --color, to the rspec command line invocation, e.g.:

我也遇到了这个问题。解决方案是为rspec命令行调用提供——tty选项,除此之外——颜色,例如:

result = run("bundle exec rspec --color --tty #{spec}")

According to this thread on the rspec google group:

根据rspec谷歌组的这个线程:

The --tty option is there to tell RSpec that the output is being streamed to a terminal as opposed to a file, in which case we don't want to print the color codes even if --color is invoked.

tty选项是告诉RSpec输出流到终端而不是文件,在这种情况下,我们不希望打印颜色代码,即使——颜色被调用。

If you then "puts" the result back to the terminal, you'll see the original coloring. If you want to parse that output for Growl, you'll want to strip out the ansi color codes using the strip_ansi method Bill Turner provided.

如果您将结果“放置”回终端,您将看到原始的着色。如果您想要解析咆哮的输出,您需要使用Bill Turner提供的strip_ansi方法去掉ansi颜色代码。

Hope that helps!

希望会有帮助!

#2


0  

This is how I have something along those lines it in my .watchr file:

这就是我的。watchr文件中的内容:

def run(cmd)
  `#{cmd}`
end

def run_spec_file(spec)
  system('clear')
  puts "Running #{spec}"
  result = run("bundle exec rspec #{spec}")
  puts result
  growl result.split("\n").last rescue nil
end

And the growl bit is this:

咆哮的是:

def growl(message)
  growlnotify = `which growlnotify`.chomp
  unless growlnotify.empty?
    title = "Test Results"
    image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/ruby_green.png" : "~/.watchr_images/ruby_red.png"
    options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{strip_ansi(message)}' '#{title}'"
    run("#{growlnotify} #{options} &")
  end
end

Which expects some images in a ~/.watchr_images/ directory on your dev box, and the growlnotify binary installed as well.

它期望在~/中有一些图像。开发框上的watchr_images/目录,以及安装的growlnotify二进制文件。

EDIT: Oh, and you may also want to check what's in your spec.opts file if there is one - it may not have --color specified.

编辑:哦,您可能还想检查您的spec.opts文件中的内容,如果有一个——它可能没有——指定了颜色。

And the strip_ansi in the growl method is used to strip the color codes so it will display in the growl notice.

使用growl方法中的strip_ansi来去除颜色代码,以便在growl通知中显示。

def strip_ansi(m)
  m.gsub(/\e\[[^m]*m/, '')
end

#3


0  

The output not showing up in color has to do with saving the test output to a variable. The way I have watchr setup it save "rake spec" output to a variable, then this variable is output to the terminal using "puts".

没有显示颜色的输出与将测试输出保存到一个变量有关。我设置watchr的方式是将“rake spec”输出保存到一个变量,然后这个变量使用“put”输出到终端。

def run(cmd)
  puts(cmd)
  `#{cmd}`
end

# what's called to run my tests
result = run "rake spec"

# then output it
puts result

Now I just need to figure out how to output the info in color!

现在我只需要弄清楚如何输出颜色的信息!

相关文章