From ccd666dc6b3020aeebcb76383bd814b17a0eca28 Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Tue, 11 Nov 2014 15:19:39 -0800 Subject: [PATCH] Make carriage return portable In unix we can just use '\r'; In Windows the easiest way is to print a lot of '\b'; --- tools/auth.js | 4 ++-- tools/console.js | 13 +++++++++---- tools/run-log.js | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/tools/auth.js b/tools/auth.js index 13165a82b1..a5c73d54aa 100644 --- a/tools/auth.js +++ b/tools/auth.js @@ -930,12 +930,12 @@ exports.registerOrLogIn = withAccountsConnection(function (connection) { var spinner = ['-', '\\', '|', '/']; lastLinePrinted = "Waiting for you to register on the web... " + spinner[animationFrame]; - Console.rawError(lastLinePrinted + "\r"); + Console.rawError(lastLinePrinted + Console.CARRIAGE_RETURN); animationFrame = (animationFrame + 1) % spinner.length; }, 200); var stopSpinner = function () { Console.rawError(new Array(lastLinePrinted.length + 1).join(' ') + - "\r"); + Console.CARRIAGE_RETURN); clearInterval(timer); }; diff --git a/tools/console.js b/tools/console.js index 7db7fa4321..8e93c03080 100644 --- a/tools/console.js +++ b/tools/console.js @@ -69,6 +69,9 @@ var wordwrap = require('wordwrap'); var PROGRESS_DEBUG = !!process.env.METEOR_PROGRESS_DEBUG; var FORCE_PRETTY=undefined; +var CARRIAGE_RETURN = + (process.platform === 'win32' ? new Array(249).join('\b') : '\r'); + if (process.env.METEOR_PRETTY_OUTPUT) { FORCE_PRETTY = process.env.METEOR_PRETTY_OUTPUT != '0'; } @@ -172,7 +175,7 @@ _.extend(ProgressDisplayStatus.prototype, { // clear some characters that we printed with a trailing `\r`. if (self._wroteStatusMessage) { var spaces = spacesString(TEMP_STATUS_LENGTH + 1); - self._stream.write(spaces + '\r'); + self._stream.write(spaces + CARRIAGE_RETURN); self._wroteStatusMessage = false; } }, @@ -203,7 +206,7 @@ _.extend(ProgressDisplayStatus.prototype, { if (text) { // the number of characters besides `text` here must // be accounted for in TEMP_STATUS_LENGTH. - self._stream.write(' ( ' + text + ' ... )\r'); + self._stream.write(' ( ' + text + ' ... )' + CARRIAGE_RETURN); self._wroteStatusMessage = true; } } @@ -320,7 +323,7 @@ _.extend(ProgressDisplayFull.prototype, { depaint: function () { var self = this; - self._stream.write(spacesString(self._printedLength) + "\r"); + self._stream.write(spacesString(self._printedLength) + CARRIAGE_RETURN); }, updateStatus: function (status) { @@ -401,7 +404,7 @@ _.extend(ProgressDisplayFull.prototype, { length += statusColumns; } - line += progressGraphic + "\r"; + line += progressGraphic + CARRIAGE_RETURN; length += progressGraphic.length; self.depaint(); @@ -1263,3 +1266,5 @@ Console.prototype.readLine = function (options) { exports.Console = new Console; +exports.Console.CARRIAGE_RETURN = CARRIAGE_RETURN; + diff --git a/tools/run-log.js b/tools/run-log.js index 8728d20ca2..738dcc4180 100644 --- a/tools/run-log.js +++ b/tools/run-log.js @@ -72,7 +72,7 @@ _.extend(RunLog.prototype, { if (self.temporaryMessageLength) { var spaces = new Array(self.temporaryMessageLength + 1).join(' '); - process.stdout.write(spaces + '\r'); + process.stdout.write(spaces + CARRIAGE_RETURN); self.temporaryMessageLength = null; } }, @@ -136,7 +136,7 @@ _.extend(RunLog.prototype, { var self = this; self._clearSpecial(); - process.stdout.write(msg + "\r"); + process.stdout.write(msg + CARRIAGE_RETURN); self.temporaryMessageLength = msg.length; }, @@ -146,7 +146,7 @@ _.extend(RunLog.prototype, { if (self.consecutiveRestartMessages) { // replace old message in place. this assumes that the new restart message // is not shorter than the old one. - process.stdout.write("\r"); + process.stdout.write(CARRIAGE_RETURN); self.messages.pop(); self.consecutiveRestartMessages ++; } else { @@ -173,7 +173,7 @@ _.extend(RunLog.prototype, { if (self.consecutiveClientRestartMessages) { // replace old message in place. this assumes that the new restart message // is not shorter than the old one. - process.stdout.write("\r"); + process.stdout.write(CARRIAGE_RETURN); self.messages.pop(); self.consecutiveClientRestartMessages ++; } else {