From 8ebbbbac657c108bc1ecdd09c69ea3e42105ed4a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 4 Aug 2013 04:50:13 -0500 Subject: [PATCH] add setText and getText methods. expose element and node. --- README.md | 4 ++++ lib/widget.js | 27 +++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 01e91c7..3453441 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,10 @@ parameter must be a string. and tags will be replaced with SGR codes (if enabled). - **getContent()** - return content, slightly different from `el.content`. assume the above formatting. +- **setText(text)** - similar to `setContent`, but ignore tags and remove escape + codes. +- **getText()** - similar to `getContent`, but return content with tags and + escape codes removed. - **insertLine(i, lines)** - insert a line into the box's content. - **deleteLine(i)** - delete a line from the box's content. - **getLine(i)** - get a line from the box's content. diff --git a/lib/widget.js b/lib/widget.js index fb076f6..c1c47bc 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -1948,17 +1948,27 @@ Element.prototype.focus = function() { return this.screen.focused = this; }; -Element.prototype.setContent = function(content, noClear) { +Element.prototype.setContent = function(content, noClear, noTags) { if (!noClear) this.clearPos(); this.content = content || ''; - this.parseContent(); + this.parseContent(noTags); }; Element.prototype.getContent = function() { return this._clines.fake.join('\n'); }; -Element.prototype.parseContent = function() { +Element.prototype.setText = function(content, noClear) { + content = content || ''; + content = content.replace(/\x1b\[[\d;]*m/g, ''); + return this.setContent(content, noClear, true); +}; + +Element.prototype.getText = function() { + return this.getContent().replace(/\x1b\[[\d;]*m/g, ''); +}; + +Element.prototype.parseContent = function(noTags) { if (this.detached) return false; var width = this.width - this.iwidth; @@ -1966,13 +1976,18 @@ Element.prototype.parseContent = function() { || this._clines.width !== width || this._clines.content !== this.content) { var content = this.content; + content = content .replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g, '') .replace(/\x1b(?!\[[\d;]*m)/g, '') .replace(/\r\n|\r/g, '\n') .replace(/\t/g, this.screen.tabc) .replace(dwidthChars, '?'); - content = this._parseTags(content); + + if (!noTags) { + content = this._parseTags(content); + } + this._clines = this._wrapContent(content, width); this._clines.width = width; this._clines.content = this.content; @@ -1982,8 +1997,10 @@ Element.prototype.parseContent = function() { this._clines.ci.push(total); return total + line.length + 1; }.bind(this), 0); + this._pcontent = this._clines.join('\n'); this.emit('parsed content'); + return true; } @@ -5781,7 +5798,9 @@ var dwidthChars = new RegExp('([' * Expose */ +exports.Node = exports.node = Node; exports.Screen = exports.screen = Screen; +exports.Element = exports.element = Element; exports.Box = exports.box = Box; exports.Text = exports.text = Text; exports.Line = exports.line = Line;