Created
March 1, 2012 22:34
-
-
Save jrun/1953711 to your computer and use it in GitHub Desktop.
Experiment with coloring and filtering stdout for a capistrano+chef project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'capistrano/cli' | |
require 'term/ansicolor' | |
include Term::ANSIColor | |
# | |
# This might get a real CLI at some point | |
# | |
puts "Target hosts: %s\nRecipes: %s\n" % [ENV['HOSTS'], ENV['RECIPES']] | |
# | |
# Hook into stdout for syntax highlighting and | |
# content filtering. | |
# | |
$stdout.instance_eval do | |
alias __original_puts__ puts | |
def puts *argv | |
filtered = argv.map {|string| filter string }.compact | |
__original_puts__ *filtered unless filtered.empty? | |
end | |
alias __original_write__ write | |
def write string | |
if filtered = filter(string) | |
__original_write__ filtered | |
end | |
end | |
# | |
# Colorize and filter output. | |
# | |
# Quick and dirty. It'll get cleaned up if this gets used. | |
# | |
def filter string | |
case string | |
when /.*(\[\d+\.\d+\.\d+\.\d+\]).*/ | |
string.sub $1, yellow($1) | |
when /(.*)(INFO:)(.*)/ | |
string.sub $2, blue($2) | |
when /^(\s+\* )executing "(.*)"/ | |
$1 + green($2) + "\n" | |
when /command finished/, /executing command$/, /^\s*$/ then nil | |
else string | |
end | |
end | |
end | |
cap_argv = %w[-p cook -l STDOUT] | |
Capistrano::CLI.parse(cap_argv).execute! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment