Color output using watchr in rails application

I use watchr in my rails3 application, since I cannot get the autotest to work correctly. Watchr was wonderful, but the only problem is I can’t get a color output. I use this as my watchr file

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

One thing I notice is that this part seems to prevent color output in some way

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

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

result = run "rake spec"

But if I do the following instead, I get color output.

result = system "rake spec"

, "", , , .

, ? Growl ?

+3
3

, , . , watchr, "rake spec" , "puts".

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

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

# then output it
puts result

, !

0

. -tty, --color, rspec, :

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

google rspec:

-tty RSpec, , , .

, . Growl, ansi strip_ansi, .

, !

+3

- .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

:

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

, ~/.watchr_images/ dev growlnotify binary .

: , , spec.opts, , , --color.

And the strip_ansigrowl method is used to mark up color codes so that it appears in the growl notification.

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

Source: https://habr.com/ru/post/1779703/


All Articles