diff --git a/lib/widget.js b/lib/widget.js index a0d0b51..d5ca12b 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -2471,10 +2471,7 @@ Element.prototype.parseContent = function(noTags) { Element.prototype.textLength = function(text) { // return unicode.strWidth(text); if (!this.parseTags) return text.length; - return text - .replace(/{(\/?)([\w\-,;!#]*)}/g, '') - .replace(/\x1b\[[\d;]*m/g, '') - .length; + return helpers.cleanTags(text).length; }; // Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`. @@ -5142,7 +5139,7 @@ function List(options) { this.mouse = options.mouse || false; if (options.items) { - this.ritems = options.items; + this.ritems = this.ritemsSet(options.items); options.items.forEach(this.add.bind(this)); } @@ -5282,12 +5279,20 @@ List.prototype.__proto__ = Box.prototype; List.prototype.type = 'list'; +List.prototype.ritemsSet = function(items) { + return this.ritems = items.map(helpers.cleanTags); +}; + +List.prototype.ritemSet = function(item) { + return this.ritems.push(helpers.cleanTags(item)); +}; + List.prototype.add = List.prototype.addItem = List.prototype.appendItem = function(item) { var self = this; - this.ritems.push(item); + this.ritemSet(item); // Note: Could potentially use Button here. var options = { @@ -5382,17 +5387,17 @@ List.prototype.fuzzyFind = function(search, back) { if (!back) { for (var i = start; i < this.ritems.length; i++){ - if (test(this.items[i].getText())) return i; + if (test(this.ritems[i])) return i; } for (var i = 0; i < start; i++){ - if (test(this.items[i].getText())) return i; + if (test(this.ritems[i])) return i; } } else { for (var i = start; i >= 0; i--){ - if (test(this.items[i].getText())) return i; + if (test(this.ritems[i])) return i; } for (var i = this.ritems.length - 1; i > start; i--){ - if (test(this.items[i].getText())) return i; + if (test(this.ritems[i])) return i; } } @@ -5450,10 +5455,10 @@ List.prototype.setItems = function(items) { this.remove(original[i]); } - this.ritems = items; + this.ritemsSet(items); // Try to find our old item if it still exists. - sel = items.indexOf(sel); + sel = this.ritems.indexOf(sel); if (~sel) { this.select(sel); } else if (items.length === original.length) { @@ -7300,7 +7305,7 @@ Listbar.prototype.appendItem = function(item, callback) { cmd.element = el; el._.cmd = cmd; - this.ritems.push(cmd.text); + this.ritemSet(cmd.text); this.items.push(el); this.commands.push(cmd); this.append(el); @@ -9116,6 +9121,12 @@ helpers.merge = merge; helpers.asort = asort; helpers.findFile = findFile; +helpers.cleanTags = function(text) { + return text + .replace(/{(\/?)([\w\-,;!#]*)}/g, '') + .replace(/\x1b\[[\d;]*m/g, ''); +}; + /** * Expose */ diff --git a/test/widget.js b/test/widget.js index 1c8ce5a..bd71bfd 100644 --- a/test/widget.js +++ b/test/widget.js @@ -114,7 +114,8 @@ list.on('keypress', function(ch, key) { }); list.on('select', function(item, select) { - list.setLabel(' ' + item.getText() + ' '); + //list.setLabel(' ' + item.getText() + ' '); + list.setLabel(' ' + list.ritems[select] + ' '); screen.render(); });