debugger: count space for line numbers correctly
authorAlex Kocharin <alex@kocharin.ru>
Sun, 6 Oct 2013 05:22:41 +0000 (09:22 +0400)
committerBen Noordhuis <info@bnoordhuis.nl>
Sun, 6 Oct 2013 11:15:07 +0000 (13:15 +0200)
lib/_debugger.js

index a68a404..2613d8b 100644 (file)
@@ -1018,28 +1018,13 @@ Interface.prototype.debugEval = function(code, context, filename, callback) {
 
 // Utils
 
-// Returns number of digits (+1)
-function intChars(n) {
-  // TODO dumb:
-  if (n < 50) {
-    return 3;
-  } else if (n < 950) {
-    return 4;
-  } else if (n < 9950) {
-    return 5;
-  } else {
-    return 6;
-  }
-}
-
 // Adds spaces and prefix to number
-function leftPad(n, prefix) {
+// maxN is a maximum number we should have space for
+function leftPad(n, prefix, maxN) {
   var s = n.toString(),
-      nchars = intChars(n),
+      nchars = Math.max(2, String(maxN).length) + 1,
       nspaces = nchars - s.length - 1;
 
-  prefix || (prefix = ' ');
-
   for (var i = 0; i < nspaces; i++) {
     prefix += ' ';
   }
@@ -1160,7 +1145,7 @@ Interface.prototype.list = function(delta) {
         prefixChar = '*';
       }
 
-      self.print(leftPad(lineno, prefixChar) + ' ' + line);
+      self.print(leftPad(lineno, prefixChar, to) + ' ' + line);
     }
     self.resume();
   });
@@ -1322,7 +1307,8 @@ Interface.prototype.watchers = function() {
       if (verbose) self.print('Watchers:');
 
       self._watchers.forEach(function(watcher, i) {
-        self.print(leftPad(i, ' ') + ': ' + watcher + ' = ' +
+        self.print(leftPad(i, ' ', self._watchers.length - 1) +
+                   ': ' + watcher + ' = ' +
                    JSON.stringify(values[i]));
       });