lib/command.rb in dev_commands-0.0.4 vs lib/command.rb in dev_commands-0.0.5

- old
+ new

@@ -1,5 +1,7 @@ +require_relative('./array.rb') +require_relative('./hash.rb') require_relative('./timer.rb') class Command < Hash def initialize command @@ -8,12 +10,12 @@ self[:timeout] = 0 self[:directory] = '' self[:exit_code] = 0 self[:output] = '' self[:error] = '' - self[:machine_name] = '' - self[:user_name] = '' + self[:machine] = '' + self[:user] = '' self[:start_time] = nil self[:end_time] = nil end if(command.kind_of?(Hash)) @@ -22,16 +24,16 @@ } end end def execute - - #Logger.start_command self - pwd=Dir.pwd Dir.chdir(self[:directory]) if(self.has_key?(:directory) && File.exists?(self[:directory])) - #print " " + Color.green + self[:input] + Color.clear + self[:directory] = pwd if(self[:directory].length==0) + + self[:machine] = Command.machine + self[:user] = Command.user self[:start_time]=Time.now timer=Timer.new if self[:input].include?('<%') && self[:input].include?('%>') ruby = self[:input].gsub("<%","").gsub("%>","") @@ -42,20 +44,42 @@ rescue puts "unable to eval(#{ruby})" raise "unable to eval(#{ruby})" end - #puts " " + timer.elapsed_str self[:elapsed] = timer.elapsed_str self[:end_time] = Time.now else self[:output] = `#{self[:input]}` self[:elapsed] = timer.elapsed_str self[:end_time] = Time.now self[:exit_code]=$?.to_i end Dir.chdir(pwd) if pwd != Dir.pwd - #Logger.end_command self end + def self.machine + if !ENV['COMPUTERNAME'].nil? + return ENV['COMPUTERNAME'] + end + + machine = `hostname` + machine = machine.split('.')[0] if machine.include?('.') + return machine.strip + end + + def self.user + ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER'] + end + + def to_html + [ + '<h2>', + self[:input], + '</h2>', + '<ul>', + map { |k, v| ["<li><strong>#{k}</strong>", v.respond_to?(:to_html) ? v.to_html : "<span>#{v}</span></li>"] }, + '</ul>' + ].join + end end \ No newline at end of file