diff --git a/README.md b/README.md index d4723a0..501716d 100644 --- a/README.md +++ b/README.md @@ -487,6 +487,7 @@ parameter must be a string. - __getLines()__ - an array containing the content lines. - __getScreenLines()__ - an array containing the lines as they are displayed on the screen. +- __textLength(text)__ - get a string's real length, taking into account tags. #### Box (from Element) diff --git a/lib/widget.js b/lib/widget.js index 40cb793..faa0927 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -2307,7 +2307,10 @@ Element.prototype.textLength = function(text) { if (!this.options.tags && !this.options.parseTags) { return text.length; } - return text.replace(/{(\/?)([\w\-,;!#]*)}/g, '').length; + return text + .replace(/{(\/?)([\w\-,;!#]*)}/g, '') + .replace(/\x1b\[[\d;]*m/g, '') + .length; }; // Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`.