From c5c9172240cd0ac07b4b9159ef0aa3af07ee62e4 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 22 Jun 2011 02:18:55 -0400 Subject: [PATCH] [api] Update `forever list` to use cliff --- lib/forever.js | 63 ++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/forever.js b/lib/forever.js index 6935431e..e661a854 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -12,6 +12,7 @@ var fs = require('fs'), exec = require('child_process').exec, async = require('async'), colors = require('colors'), + cliff = require('cliff'), daemon = require('daemon'), nconf = require('nconf'), timespan = require('timespan'), @@ -375,7 +376,7 @@ forever.stopAll = function (format) { // Returns the list of all process data managed by forever. // forever.list = function (format, procs) { - var formatted = []; + var formatted; procs = procs || getAllProcesses(); if (!procs) { @@ -383,24 +384,40 @@ forever.list = function (format, procs) { } if (format) { - var index = 0, maxLen = 0; - // Iterate over the procs to see which has the longest options string - procs.forEach(function (proc) { - proc.length = [proc.command || 'node', proc.file].concat(proc.options).join(' ').length; - if (proc.length > maxLen) { - maxLen = proc.length; - } - }); + var index = 0, rows = [ + [' ', 'command ', 'script', 'forever ', 'pid', 'logfile', 'uptime'] + ]; + // + // Iterate over the procs to see which has the + // longest options string + // procs.forEach(function (proc) { - // Create padding string to keep output aligned - var padding = new Array(maxLen - proc.length + 1).join(' '); - formatted.push(formatProcess(proc, index, padding)); + rows.push([ + '[' + index + ']', + (proc.command || 'node').grey, + [proc.file].concat(proc.options).join(' ').grey, + proc.foreverPid, + proc.pid, + proc.logFile ? proc.logFile.magenta : '', + timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow + ]); + index++; }); + + formatted = cliff.stringifyRows(rows, [ + 'white', + 'grey', + 'grey', + 'white', + 'white', + 'magenta', + 'yellow' + ]); } - return format ? formatted.join('\n') : procs; + return format ? formatted : procs; }; // @@ -582,26 +599,6 @@ forever.checkProcess = function (pid, callback) { }); }; -// -// ### function formatProcess (proc index, padding) -// #### @proc {Object} Process to format -// #### @index {Number} Index of the process in the set of all processes -// #### @padding {string} Padding to add to the formatted output -// Returns a formatted string for the process @proc at -// the specified index. -// -function formatProcess (proc, index, padding) { - var command = proc.command || 'node'; - - // Create an array of the output we can later join - return ['[' + index + ']', command.grey, proc.file.grey] - .concat(proc.options.map(function (opt) { return opt.grey })) - .concat([padding + '[' + proc.pid + ',', proc.foreverPid + ']']) - .concat(proc.logFile ? proc.logFile.magenta : '') - .concat(timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow) - .join(' '); -}; - // // ### function getAllProcess ([findDead]) // #### @findDead {boolean} Optional parameter that indicates to return dead procs