-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add beginnings of autocomplete functionality, refs #3
- Loading branch information
Showing
3 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
## 0.1.5 (WIP) | ||
|
||
* Add autocomplete (basic for now) | ||
|
||
## 0.1.4 (2016-01-20) | ||
|
||
* Improve multiline string support | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,11 @@ var Blast = require('protoblast')(false), // Protoblast without native mods | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.0 | ||
* @version 0.1.4 | ||
* @version 0.1.5 | ||
*/ | ||
JanewayClass = Blast.Collection.Function.inherits('Informer', function Janeway() {}); | ||
JanewayClass = Blast.Collection.Function.inherits('Informer', function Janeway() { | ||
this.open_popups = {}; | ||
}); | ||
|
||
/** | ||
* Scrolldown on new data (true by default) | ||
|
@@ -462,6 +464,137 @@ JanewayClass.setMethod(function scrollAlong(or_render) { | |
} | ||
}); | ||
|
||
/** | ||
* Show a popup | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.5 | ||
* @version 0.1.5 | ||
* | ||
* @param {String} id The unique id (only 1 can be open at a time) | ||
* @param {Object} options | ||
* | ||
* @return {ListBox} | ||
*/ | ||
JanewayClass.setMethod(function popup(id, options) { | ||
|
||
var list; | ||
|
||
if (!options) { | ||
if (this.open_popups[id]) { | ||
this.open_popups[id].destroy(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (!options.position) { | ||
options.position = {}; | ||
} | ||
|
||
if (options.position.bottom == null) { | ||
options.position.bottom = 2; | ||
} | ||
|
||
if (options.position.height == null) { | ||
options.position.height = 6; | ||
} | ||
|
||
if (this.open_popups[id]) { | ||
this.open_popups[id].destroy(); | ||
} | ||
|
||
// Create a new list | ||
list = this.blessed.list({ | ||
//bottom: 2, | ||
position: options.position, | ||
width: '100%', | ||
items: options.items, | ||
mouse: true, // Allow selecting items with the mouse | ||
scrollbar: { | ||
bg: 'green' | ||
}, | ||
border: { | ||
type: 'line' | ||
}, | ||
shadow: true, | ||
style: { | ||
bg: 'blue', | ||
fg: 'white' | ||
} | ||
}); | ||
|
||
// Store the popup under its unique id | ||
this.open_popups[id] = list; | ||
|
||
// Add it to the screen | ||
this.screen.append(list); | ||
|
||
// Make sure it's in the front | ||
list.setFront(); | ||
|
||
// Render the screen | ||
this.screen.render(); | ||
|
||
return list; | ||
}); | ||
|
||
/** | ||
* Show the autocomplete | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.5 | ||
* @version 0.1.5 | ||
* | ||
* @param {String} cmd The current content of the CLI | ||
* @param {Object} key The last key pressed | ||
*/ | ||
JanewayClass.setMethod(function autocomplete(cmd, key) { | ||
|
||
var pieces, | ||
target, | ||
items, | ||
last, | ||
left, | ||
list, | ||
key; | ||
|
||
if (!cmd && !key) { | ||
return this.autocomplete_list = this.popup('autocomplete', false); | ||
} | ||
|
||
this.autocomplete_prefix = null; | ||
pieces = cmd.split('.'); | ||
items = []; | ||
left = 1 + cmd.length; | ||
|
||
if (pieces.length == 1) { | ||
target = global; | ||
last = cmd; | ||
} else { | ||
last = pieces.pop(); | ||
target = Blast.Bound.Object.path(global, pieces); | ||
|
||
this.autocomplete_prefix = pieces.join('.') + '.'; | ||
} | ||
|
||
if (target) { | ||
for (key in target) { | ||
if (!last || Blast.Bound.String.startsWith(key, last)) { | ||
items.push(key); | ||
} | ||
} | ||
} | ||
|
||
if (cmd.trim() && items.length) { | ||
list = this.popup('autocomplete', {position: {left: left, height: 6}, items: items}); | ||
} else { | ||
list = this.popup('autocomplete', false); | ||
} | ||
|
||
this.autocomplete_list = list; | ||
}); | ||
|
||
/** | ||
* Start Janeway | ||
* | ||
|
@@ -528,7 +661,6 @@ JanewayClass.setMethod(function start() { | |
}, | ||
//inputOnFocus: true, | ||
mouse: true | ||
//keys: true | ||
}); | ||
|
||
output = blessed.box({ | ||
|
@@ -549,6 +681,14 @@ JanewayClass.setMethod(function start() { | |
} | ||
}); | ||
|
||
// Store elements in the object | ||
this.blessed = blessed; | ||
this.screen = screen; | ||
this.output = output; | ||
this.bottom = bottom; | ||
this.form = form; | ||
this.cli = cli; | ||
|
||
// Create the LogList instance | ||
logList = new Janeway.LogList(this, screen, output); | ||
this.logList = logList; | ||
|
@@ -731,7 +871,7 @@ JanewayClass.setMethod(function start() { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.0 | ||
* @version 0.1.4 | ||
* @version 0.1.5 | ||
*/ | ||
cli.on('keypress', function onKeypress(e, key) { | ||
|
||
|
@@ -740,13 +880,32 @@ JanewayClass.setMethod(function start() { | |
errorLine, | ||
evalLine, | ||
scope, | ||
path, | ||
temp, | ||
cmd; | ||
|
||
if (key.name == 'pagedown') { | ||
that.scroll(20); | ||
} else if (key.name == 'pageup') { | ||
that.scroll(-20); | ||
} else if (key.name == 'enter') { | ||
|
||
if (that.autocomplete_list) { | ||
|
||
// Get the selected item | ||
temp = that.autocomplete_list.getItem(that.autocomplete_list.selected); | ||
|
||
// Get the path before the last dot | ||
path = that.autocomplete_prefix || ''; | ||
|
||
if (temp) { | ||
cli.setValue(path + temp.content); | ||
that.autocomplete(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
cmd = cli.getValue().trim(); | ||
|
||
// Return if the cmd is empty | ||
|
@@ -801,6 +960,31 @@ JanewayClass.setMethod(function start() { | |
// Scroll along if needed | ||
that.scrollAlong(); | ||
}); | ||
} else { | ||
cmd = cli.getValue(); | ||
|
||
if (key.code) { | ||
// Ignore keys with codes | ||
|
||
if (that.autocomplete_list) { | ||
if (key.name == 'up') { | ||
that.autocomplete_list.up(1); | ||
that.autocomplete_list.render(); | ||
screen.render(); | ||
} else if (key.name == 'down') { | ||
that.autocomplete_list.down(1); | ||
screen.render(); | ||
} | ||
} | ||
|
||
return; | ||
} else if (key.name === 'backspace') { | ||
cmd = cmd.slice(0, -1); | ||
} else { | ||
cmd += e; | ||
} | ||
|
||
that.autocomplete(cmd, key); | ||
} | ||
}); | ||
|
||
|
@@ -809,6 +993,7 @@ JanewayClass.setMethod(function start() { | |
screen.append(output); | ||
bottom.append(form); | ||
form.append(cli); | ||
|
||
screen.render(); | ||
|
||
// Quit on Escape, q, or Control-C. | ||
|