Last active
February 29, 2016 03:32
-
-
Save np422/816a6ffb0fec5fc3d992 to your computer and use it in GitHub Desktop.
Ruby script to print any process environment variables on stdout
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
# | |
# Formatting functions and constants | |
# | |
class FormatHelper | |
require 'English' | |
attr_reader :terminal_lines, :terminal_columns | |
# Returns a FormatHelper that know about your current terminal | |
# and how to format the key,value pairs in the hash envpairs | |
# for output | |
# | |
# Requires a hash as argument that contains the { key: value } | |
# pairs you want to format. | |
def initialize(environ) | |
init_constants | |
init_maximum_sizes(environ) | |
init_terminal_columns | |
init_terminal_lines | |
init_calculated_values | |
end | |
def header_row(label_name, label_value) | |
header_row = @f_bold_underline + @f_color_name + label_name + @f_clear | |
header_row += ' ' * padding_name(label_name) | |
header_row += @f_bold_underline + @f_color_value + label_value + @f_clear | |
header_row += ' ' * padding_value(label_value) + @f_clear | |
header_row | |
end | |
def row(name, value) | |
myvalue = format_value(value) | |
row = @f_color_name + name + @f_clear | |
row += ' ' * padding_name(name) | |
row += @f_color_value + myvalue + @f_clear | |
row += ' ' * padding_value(myvalue) | |
row | |
end | |
protected | |
def init_constants | |
@extra_whitespace = 3 | |
@cut_from_value = 10 | |
@f_color_name = "\e[34m" | |
@f_color_value = "\e[36m" | |
@f_clear = "\e[0m" | |
@f_bold = "\e[1m" | |
@f_underline = "\e[4m" | |
@f_bold_underline = @f_bold + @f_underline | |
end | |
def init_maximum_sizes(env) | |
@max_size_names = 0 | |
@max_size_values = 0 | |
@max_size_lines = 0 | |
env.keys.each do |k| | |
this_row_size = k.length + env[k].length + @extra_whitespace | |
@max_size_names = k.length if k.length > @max_size_names | |
@max_size_values = env[k].length if env[k].length > @max_size_values | |
@max_size_lines = this_row_size if this_row_size > @max_size_lines | |
end | |
end | |
def init_terminal_lines | |
if ENV['LINES'].nil? | |
begin | |
tlines = `tput lines`.chomp.to_i | |
rescue | |
tlines = 0 | |
end | |
# Assume std value of 24 if neither env-value or tput program exists | |
tlines = 24 if $CHILD_STATUS.to_i != 0 || tlines == 0 | |
else | |
tlines = ENV['LINES'] | |
end | |
@terminal_lines = tlines.to_i | |
end | |
def init_terminal_columns | |
if ENV['COLUMNS'].nil? | |
begin | |
tcols = `tput cols`.chomp.to_i | |
rescue | |
tcols = 0 | |
end | |
# Assume std value of 80 if neither env-value or tput program exists | |
tcols = 80 if $CHILD_STATUS.to_i != 0 || tcols == 0 | |
else | |
tcols = ENV['COLUMNS'] | |
end | |
@terminal_columns = tcols.to_i | |
end | |
def padding_name(name) | |
@max_size_names - name.length + @extra_whitespace | |
end | |
def padding_value(v) | |
@allowed_size_values - v.length | |
end | |
def init_calculated_values | |
calc = @terminal_columns - @max_size_names - @extra_whitespace | |
@allowed_size_values = [calc, 1].max | |
end | |
def format_value(v) | |
cut = @allowed_size_values - @cut_from_value | |
v.length > @allowed_size_values ? v[0..cut] + '....' : v | |
end | |
end # FormatHelp |
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 | |
# Add directory where procenv is run from to load-path, to find format_Helper | |
$LOAD_PATH.unshift(File.dirname(__FILE__)) | |
require 'format_helper' | |
if ARGV[0] !~ /\d+/ | |
puts 'Usage: procenv PID' | |
exit | |
end | |
envblob = File.open("/proc/#{ARGV[0]}/environ").read | |
envvars = envblob.split(?\x00) | |
envpairs = envvars.map { |l| l.split('=', 2) } | |
process_environ = {} | |
envpairs.each { |pair| process_environ["#{pair[0]}"] = pair[1] } | |
myhelper = FormatHelper.new(process_environ) | |
rows_between_headers = myhelper.terminal_lines - 3 | |
header = myhelper.header_row('Environment variable name', 'Environment value') | |
puts header | |
process_environ.keys.each_with_index do |name, i| | |
puts "\n" + header if i % rows_between_headers == 0 && i > 0 | |
puts myhelper.row(name, process_environ["#{name}"]) | |
end | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment