lib/invoker/process_printer.rb in invoker-1.0.4 vs lib/invoker/process_printer.rb in invoker-1.1.0
- old
+ new
@@ -1,53 +1,42 @@
module Invoker
class ProcessPrinter
MAX_COLUMN_WIDTH = 40
- def self.to_json(workers)
- final_json = []
- Invoker::CONFIG.processes.each do |process|
- if worker = workers[process.label]
- final_json << {
- :command => process.cmd, :command_label => process.label,
- :dir => process.dir, :pid => worker.pid
- }
- else
- final_json << {
- :command => process.cmd, :command_label => process.label,
- :dir => process.dir
- }
- end
- end
+ attr_accessor :list_response
- final_json.to_json
+ def initialize(list_response)
+ self.list_response = list_response
end
- def self.print_table(json_data)
- final_json = JSON.parse(json_data)
- if final_json && !final_json.empty?
- json_for_printing = []
- final_json.each do |json_row|
- if json_row["pid"]
- json_for_printing << colorize_hash(json_row, "green")
- else
- json_row["pid"] = "[light_black]not running[/]"
- json_for_printing << colorize_hash(json_row, "light_black")
- end
+ def print_table
+ hash_with_colors = []
+ list_response.processes.each do |process|
+ if process.pid
+ hash_with_colors << colorize_hash(process, "green")
+ else
+ hash_with_colors << colorize_hash(process, "light_black")
end
- Formatador.display_compact_table(json_for_printing)
end
+ Formatador.display_compact_table(hash_with_colors)
end
private
- def self.colorize_hash(hash, color)
- hash.inject({}) do |mem,(key,obj)|
- if obj.to_s.length > MAX_COLUMN_WIDTH
- short_command = "#{obj.to_s[0..MAX_COLUMN_WIDTH]}.."
- mem[key] = "[#{color}]#{short_command}[/]"
- else
- mem[key] = "[#{color}]#{obj}[/]"
- end
- mem
- end
+
+ def colorize_hash(process, color)
+ hash_with_colors = {}
+
+ hash_with_colors['dir'] = colored_string(process.dir, color)
+ hash_with_colors['pid'] = colored_string(process.pid || 'Not Running', color)
+ hash_with_colors['shell_command'] = colored_string(process.shell_command, color)
+ hash_with_colors['process_name'] = colored_string(process.process_name, color)
+ hash_with_colors
end
+ def colored_string(string, color)
+ string = string.to_s
+ if string.length > MAX_COLUMN_WIDTH
+ string = "#{string[0..MAX_COLUMN_WIDTH]}.."
+ end
+ "[#{color}]#{string}[/]"
+ end
end
end