diff --git a/README.md b/README.md index 91ebe84..61ff3fa 100644 --- a/README.md +++ b/README.md @@ -547,6 +547,7 @@ A scrollable list which can display selectable items. - **keys** - use predefined keys for navigating the list. - **vi** - use vi keys with the `keys` option. - **items** - an array of strings which become the list's items. +- **search** - a function that is called when `vi` mode is enabled and the key `/` is pressed. This function accepts a callback function which should be called with the search string. The search string is then used to jump to an item that is found in `items`. ##### Properties: diff --git a/lib/widget.js b/lib/widget.js index 7b06fec..deec083 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -4157,6 +4157,17 @@ function List(options) { self.screen.render(); return; } + + if (options.vi && key.ch === '/') { + if (typeof self.options.search !== 'function') { + return; + } + + self.options.search(function(searchString){ + self.select(self.fuzzyFind(searchString)); + self.screen.render(); + }) + } }); } @@ -4251,6 +4262,17 @@ List.prototype.appendItem = function(item) { } }; +List.prototype.fuzzyFind = function(searchString) { + var index = this.getItemIndex(this.selected); + + for (var i = 0; i < this.ritems.length; i++){ + if (this.ritems[i].match(new RegExp('^' + searchString))){ + return i; + } + } + return index; +} + List.prototype.getItemIndex = function(child) { if (typeof child === 'number') { return child;