Skip to content

Commit

Permalink
Add search filter when displaying namespaces Fixes #15 and #49
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpoth committed Oct 7, 2019
1 parent 9067c86 commit fa0628d
Showing 1 changed file with 109 additions and 1 deletion.
110 changes: 109 additions & 1 deletion lib/ui/namespaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,114 @@ function namespaces_list(screen) {
}
});

const search = blessed.textbox({
parent : namespaces_list,
label : 'Search',
content : '',
border : 'line',
width : '30%',
height : 3,
right : 2,
top : -1,
keys : true,
mouse : true,
hidden : true,
inputOnFocus : true,
style : {
fg : 'white',
label : { bold: true },
border : { fg: 'white' },
selected : { bg: 'blue' },
}
});

search.__oolistener = search._listener;
search._listener = function(ch, key) {
const word = ['up', 'down','enter'];
if (word.includes(key.name)) {
return namespaces_list.emit('keypress', ch, key);
}
const ret = this.__oolistener(ch, key);
let i = 0;
namespaces_list.items.forEach((element, pos) => {
if (!element.getContent().includes(search.value)){
element.position.top = 0;
element.hidden = true;
} else {
element.position.top = i;
element.hidden = false;
if (i===0) {
namespaces_list.selected = pos;
}
i++;
}
});
namespaces_list.scrollTo(0);
screen.render();
return ret;
};
namespaces_list.on('keypress', (ch, key) => {
const keys = ['escape', 'up', 'down', 'enter', 'q'];
if (keys.includes(key.name) || !search.hidden) {
return;
} else {
search.hidden = false;
screen.saveFocus();
namespaces_list.append(search);
search.focus();
search.readInput();
search._listener(ch,key);
screen.render();
}
});

search.key('escape', (ch, key) => {
search.detach();
search.value = '';
search.hidden = true;
screen.restoreFocus();
namespaces_list.items.forEach((element, pos) => {
element.position.top = pos;
element.hidden = false;
});
namespaces_list.select(0);
screen.render();
});

namespaces_list.up = function(offset) {
if (namespaces_list.items[namespaces_list.selected].hidden)
return;
let i = namespaces_list.selected -(offset || 1);
const len = namespaces_list.items.length;
for(i = mod(i,len); i < len && i >= 0; i = mod(i-1,len)) {
if(!namespaces_list.items[i].hidden) {
namespaces_list.selected = i;
namespaces_list.scrollTo(namespaces_list.items[i].position.top);
screen.render();
return;
}
}
};

namespaces_list.down = function(offset) {
if (namespaces_list.items[namespaces_list.selected].hidden)
return;
let i = namespaces_list.selected +(offset || 1);
const len = namespaces_list.items.length;
for(i = mod(i,len); i < len && i >= 0 ; i = mod(i+1,len)){
if(!namespaces_list.items[i].hidden) {
namespaces_list.selected = i;
namespaces_list.scrollTo(namespaces_list.items[i].position.top);
screen.render();
return;
}
}
};

function mod(a , b){
return (((a)%b)+b)%b;
}

return namespaces_list;
}

Expand Down Expand Up @@ -133,7 +241,7 @@ function prompt(screen, client, { current_namespace, promptAfterRequest } = { pr

list.on('select', (item, i) => {
close_namespaces_list();
if (item) {
if (item && !item.hidden) {
const namespace = namespaces.items[i].metadata.name;
fulfill(namespace);
} else {
Expand Down

0 comments on commit fa0628d

Please sign in to comment.