From a0daecc8bb4301dad324801126d0a503b9e38d61 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 19 Apr 2015 19:50:53 -0700 Subject: [PATCH] fuzzyFind improvement. --- example/ansi-viewer/index.js | 1 - lib/widget.js | 26 +++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/example/ansi-viewer/index.js b/example/ansi-viewer/index.js index 95ad630..70f7697 100644 --- a/example/ansi-viewer/index.js +++ b/example/ansi-viewer/index.js @@ -125,7 +125,6 @@ var prompt = blessed.prompt({ vi: true, mouse: true, tags: true, - content: 'Label:', border: 'line', hidden: true }); diff --git a/lib/widget.js b/lib/widget.js index 1f102f9..a5360a0 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -5250,10 +5250,11 @@ List.prototype.appendItem = function(item) { }; List.prototype.fuzzyFind = function(search, back) { - var index = this.getItemIndex(this.selected); var start = this.selected + (back ? -1 : 1); - if (search[0] === '/' && search[search.length - 1] === '/') { + if (typeof search === 'number') search += ''; + + if (search && search[0] === '/' && search[search.length - 1] === '/') { try { search = new RegExp(search.slice(1, -1)); } catch (e) { @@ -5261,27 +5262,34 @@ List.prototype.fuzzyFind = function(search, back) { } } - var finder = typeof search === 'string' + var test = typeof search === 'string' ? function(item) { return !!~item.indexOf(search); } - : search.test.bind(search); + : (search.test ? search.test.bind(search) : search); + + if (typeof test !== 'function') { + if (this.screen.options.debug) { + throw new Error('fuzzyFind(): `test` is not a function.'); + } + return this.selected; + } if (!back) { for (var i = start; i < this.ritems.length; i++){ - if (finder(this.ritems[i])) return i; + if (test(this.ritems[i])) return i; } for (var i = 0; i < start; i++){ - if (finder(this.ritems[i])) return i; + if (test(this.ritems[i])) return i; } } else { for (var i = start; i >= 0; i--){ - if (finder(this.ritems[i])) return i; + if (test(this.ritems[i])) return i; } for (var i = this.ritems.length - 1; i > start; i--){ - if (finder(this.ritems[i])) return i; + if (test(this.ritems[i])) return i; } } - return index; + return this.selected; }; List.prototype.getItemIndex = function(child) {