Created
May 5, 2015 19:58
-
-
Save fuzzy/d36e9422de0781e61b18 to your computer and use it in GitHub Desktop.
java commandline colorizer (ps output)
This file contains hidden or 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 | |
procs = Hash.new | |
scrpt = `basename #{__FILE__}`.strip | |
$color = { | |
:black => 30, :red => 31, :green => 32, | |
:yellow => 33, :blue => 34, :purple => 35, | |
:cyan => 36, :white => 37 | |
} | |
$attrs = { | |
:clean => 0, :bold => 1, :underline => 4, | |
:blink => 5, :reverse => 7, :conceal => 8 | |
} | |
class String | |
def valid(args) | |
if args.keys.include? :color and $color.keys.include? args[:color] | |
true | |
elsif args.keys.include? :attr and $attrs.keys.include? args[:attr] | |
true | |
else | |
false | |
end | |
end | |
def fg(color) | |
if $stdout.isatty | |
"\033[#{$color[color]}m#{self}\033[0m" if valid(:color => color) | |
else | |
"#{self}" | |
end | |
end | |
def bg(color) | |
if $stdout.isatty | |
"\033[#{($color[color]+10)}m#{self}\033[0m" if valid(:color => color) | |
else | |
"#{self}" | |
end | |
end | |
def attr(attr) | |
if $stdout.isatty | |
"\033[#{($attrs[attr])}m#{self}\033[0m" if valid(:attr => attr) | |
else | |
"#{self}" | |
end | |
end | |
def method_missing(m, *args, &block) | |
if $color.keys.include?(m) | |
fg(m) | |
elsif $attrs.keys.include?(m) | |
attr(m) | |
else | |
raise NoMethodError, "Undefined method '#{m.inspect}'." | |
end | |
end | |
end | |
`ps aux|grep java|grep -v grep`.each_line {|l| | |
d = l.strip.split | |
if l !~ /.*#{scrpt}.*/ | |
if not procs.keys.include? d[0].to_sym | |
procs[d[0].to_sym] = Array.new | |
end | |
procs[d[0].to_sym].push(:pid => d[1], :cmd => d[10..-1]) | |
end | |
} | |
cpf = false | |
jarf = false | |
procs.keys.each {|user| | |
procs[user].each {|entry| | |
ostr = String.new | |
entry[:cmd].each {|token| | |
if token =~ /^sudo$/ | |
ostr += "#{'sudo'.bold.white} " | |
elsif token =~ /.*java$/ | |
ostr += "#{token.bold.yellow} " | |
elsif token =~ /-jar/ | |
ostr += "#{token.bold.red} " | |
jarf = true | |
elsif jarf | |
ostr += "#{token.bold.purple} " | |
jarf = false | |
elsif token =~ /.*\.xml/ | |
ostr += "#{token.green} " | |
elsif token =~ /^-X[ms][xs].*[0-9+m]$/ | |
ostr += "#{token[0..3].bold.red}#{token[4..-1].bold.cyan} " | |
elsif token =~ /^-D.+.+$/ | |
k, v = token[2..-1].split('=') | |
ks = "#{k.cyan}" | |
if v | |
vs = "#{v.bold.cyan.gsub(/\,/, ','.bold.green)}" | |
else | |
vs = "" | |
end | |
vs.gsub(/:/, ':'.bold.green) | |
ostr += "#{'-D'.bold.red}#{ks}#{'='.bold.white}#{vs} " | |
elsif token =~ /^-D.+=$/ | |
k = token[2..-1].split('=')[0] | |
ks = "#{k.cyan}" | |
ostr += "#{'-D'.bold.red}#{ks}#{'='.bold.white} " | |
elsif token =~ /^-(cp|classpath)$/ | |
ostr += "#{token.bold.red} " | |
cpf = true | |
elsif cpf | |
ostr += "#{token.bold.cyan.gsub(/:/, ':'.bold.green)} " | |
cpuf = false | |
else | |
ostr += "#{token} " | |
end | |
} | |
puts ostr | |
puts | |
} | |
} | |
#require 'pp' | |
#pp procs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment