diff --git a/adb-logcat-color.rb b/adb-logcat-color.rb index 58b9107..33d24ab 100755 --- a/adb-logcat-color.rb +++ b/adb-logcat-color.rb @@ -3,7 +3,7 @@ require 'rubygems' require 'smart_colored/extend' -patterns = {"V/" => {:fg => "black", :bg => "blue", :label => "[ VERBOSE ]"}, +$patterns = {"V/" => {:fg => "black", :bg => "blue", :label => "[ VERBOSE ]"}, "D/" => {:fg => "black", :bg => "cyan", :label => "[ DEBUG ]"}, "I/" => {:fg => "black", :bg => "green", :label => "[ INFO ]"}, "W/" => {:fg => "black", :bg => "yellow", :label => "[ WARNING ]"}, @@ -11,24 +11,36 @@ "F/" => {:fg => "black", :bg => "magenta", :label => "[ FATAL ]"}, "S/" => {:fg => "black", :bg => "white", :label => "[ SILENT ]"}} +def colorize_line(line) + line.sub!(/\(\s*\d+\)/, "") + line.gsub!("\r\n", "") -# Ruby 1.9 support passing an array - move to this eventually -IO.popen("adb logcat #{ARGV.join(' ')}") do |f| - while line = f.gets - line.sub!(/\(\s*\d+\)/, "") - line.gsub!("\r\n", "") + match = $patterns.keys.find do |x| + line.start_with?(x) + end - match = patterns.keys.find do |x| - line.start_with?(x) - end + if match + line.sub!(match, " => ") + label = $patterns[match][:label] + fg = $patterns[match][:fg] + bg = $patterns[match][:bg] + print label.send("#{fg}_on_#{bg}") + puts line.send("#{bg}_on_#{fg}") + end +end - if match - line.sub!(match, " => ") - label = patterns[match][:label] - fg = patterns[match][:fg] - bg = patterns[match][:bg] - print label.send("#{fg}_on_#{bg}") - puts line.send("#{bg}_on_#{fg}") +use_argf=ARGV.length == 1 and File.exists?(ARGV[0]) + +if use_argf then + ARGF.each do |line| + colorize_line(line) + end +else + # Ruby 1.9 support passing an array - move to this eventually + IO.popen("adb logcat #{ARGV.join(' ')}") do |f| + while line = f.gets + colorize_line(line) end end end +