Merge pull request #54 from bulkan/feature/list-search

Run a `search` function in vi mode for List's
This commit is contained in:
Christopher Jeffrey 2014-06-05 01:13:41 -05:00
commit ba3f0e0815
2 changed files with 23 additions and 0 deletions

View File

@ -547,6 +547,7 @@ A scrollable list which can display selectable items.
- **keys** - use predefined keys for navigating the list. - **keys** - use predefined keys for navigating the list.
- **vi** - use vi keys with the `keys` option. - **vi** - use vi keys with the `keys` option.
- **items** - an array of strings which become the list's items. - **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: ##### Properties:

View File

@ -4157,6 +4157,17 @@ function List(options) {
self.screen.render(); self.screen.render();
return; 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) { List.prototype.getItemIndex = function(child) {
if (typeof child === 'number') { if (typeof child === 'number') {
return child; return child;