From 52bae02a3599e38d5380cb075b6df5923c0152b2 Mon Sep 17 00:00:00 2001 From: "amshali@google.com" Date: Thu, 27 Aug 2015 12:25:32 -0700 Subject: [PATCH] Removing traling spaces from end of lines. This is useful because when one is copying lines from term.js all those   characters are also copied and what you will see when you paste is a bunch of useless spaces which are usually undesired. This patch fixes this issue by removing traling spaces when we are constructing the lines. There is a caveat for empty lines. We need to leave at least one space there or otherwise the output will lose lines. --- src/term.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/term.js b/src/term.js index f542dd0..f4c9912 100644 --- a/src/term.js +++ b/src/term.js @@ -1410,7 +1410,22 @@ Terminal.prototype.refresh = function(start, end) { out += ''; } - this.children[y].innerHTML = out; + // Removing trailing spaces from end of lines. + // Leaving out one space on empty lines. + var space = ' '; + var spaceCount = 0; + for (var i = out.length - space.length; i >= 0; i = i - space.length) { + if(out.slice(i, i + space.length) === space) { + spaceCount++; + } else { + break; + } + } + var newOut = out.slice(0, out.length - spaceCount * space.length); + if (newOut.length === 0) { + newOut = ' '; + } + this.children[y].innerHTML = newOut; } if (parent) parent.appendChild(this.element);