Permalink
Browse files

add command completion feature

  • Loading branch information...
1 parent 56a97ee commit 9cdf6a31be5f151bedac7e4d1f1ce8c7428a77fd @vietj vietj committed Jul 15, 2012
Showing with 72 additions and 1 deletion.
  1. +8 −0 README
  2. +27 −0 demo.html
  3. +37 −1 jquery.console.js
View
8 README
@@ -18,6 +18,10 @@ Options available:
promptLabel string Prompt string like 'JavaScript> '.
+ cols integer the number of cols, this value is only
+ used by the command completion to format
+ the list of results.
+
commandValidate function When user hits return, validate
whether to trigger commandHandle and
re-prompt.
@@ -30,6 +34,10 @@ Options available:
to report a result of the command
asynchronously.
+ commandComplete function Handle the command completion when the
+ tab key is pressed. It returns a list
+ of string completion suffixes.
+
animateScroll bool Whether to animate the scroll to
top. Currently disabled.
View
27 demo.html
@@ -102,6 +102,33 @@
},
promptHistory:true
});
+ /* Fifth console */
+ var console5 = $('<div class="console1">');
+ $('body').append(console5);
+ var controller5 = console5.console({
+ promptLabel: 'Complete> ',
+ commandHandle:function(line){
+ if (line) {
+ return [{msg:"you typed " + line,className:"jquery-console-message-value"}];
+ } else {
+ var m = "type a color among (" + this.colors.join(", ") + ")";
+ return [{msg:m,className:"jquery-console-message-value"}];
+ }
+ },
+ colors: ["red","blue","green","black","yellow","white","grey"],
+ cols: 40,
+ completeHandle:function(prefix){
+ var colors = this.colors;
+ var ret = [];
+ for (var i=0;i<colors.length;i++) {
+ var color=colors[i];
+ if (color.lastIndexOf(prefix,0) === 0) {
+ ret.push(color.substring(prefix.length));
+ }
+ }
+ return ret;
+ }
+ })
});
</script>
<style type="text/css" media="screen">
View
38 jquery.console.js
@@ -62,7 +62,9 @@
// return
13: commandTrigger,
// tab
- 18: doNothing
+ 18: doNothing,
+ // tab
+ 9: doComplete
};
var ctrlCodes = {
// C-a
@@ -583,6 +585,40 @@
return false;
};
+ function doComplete() {
+ if(typeof config.completeHandle == 'function') {
+ var completions = config.completeHandle(promptText);
+ var len = completions.length;
+ if (len === 1) {
+ extern.promptText(promptText + completions[0]);
+ } else if (len > 1 && config.cols) {
+ var prompt = promptText;
+ // Compute the number of rows that will fit in the width
+ var max = 0;
+ for (var i = 0;i < len;i++) {
+ max = Math.max(max, completions[i].length);
+ }
+ max += 2;
+ var n = Math.floor(config.cols / max);
+ var buffer = "";
+ var col = 0;
+ for (i = 0;i < len;i++) {
+ var completion = completions[i];
+ buffer += completions[i];
+ for (var j = completion.length;j < max;j++) {
+ buffer += " ";
+ }
+ if (++col >= n) {
+ buffer += "\n";
+ col = 0;
+ }
+ }
+ commandResult(buffer,"jquery-console-message-value");
+ extern.promptText(prompt);
+ }
+ }
+ };
+
function doNothing() {};
extern.promptText = function(text){

0 comments on commit 9cdf6a3

Please sign in to comment.