Skip to content
This repository was archived by the owner on Nov 28, 2018. It is now read-only.

Commit 7071212

Browse files
add a new config named "completeIssuer".
This new config handles the command completion when the tab key is pressed. It differs from `'completeHandle'`. `'completeIssuer'` will just trigger the calculation for completion, and the result is returned asynchronously, after which the controller's `'showCompletion(promptText, completions)'` can be invoked with the result. `'completeHandle'` will retrieve the result synchronously, and show the result. If `'completeHandle'` exists, `'completeIssuer'` is ignored. A typical usage of `'completeIssuer'` is that the completion is retrieved from the server using ajax or WebSocket asynchronously.
1 parent 0f2ee06 commit 7071212

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Here are options which can be passed to `console`:
8080
| commandValidate | function | When user hits return, validate whether to trigger commandHandle and re-prompt.
8181
| commandHandle | function | Handle the command line, return a string, boolean, or list of `{msg:"foo",className:"my-css-class"}`. `commandHandle(line,report)` is called. Report function is for you to report a result of the command asynchronously.
8282
| completeHandle | function | Handle the command completion when the tab key is pressed. It returns a list of string completion suffixes.
83+
| completeIssuer | function | Handle the command completion when the tab key is pressed. It differs from `'completeHandle'`. `'completeIssuer'` will just trigger the calculation for completion, and the result is returned asynchronously, after which the controller's `'showCompletion(promptText, completions)'` can be invoked with the result. `'completeHandle'` will retrieve the result synchronously, and show the result. If `'completeHandle'` exists, `'completeIssuer'` is ignored. A typical usage of `'completeIssuer'` is that the completion is retrieved from the server using ajax or WebSocket asynchronously.
8384
| animateScroll | bool | Whether to animate the scroll to top. Currently disabled.
8485
| charInsertTrigger | function | Predicate for whether to allow character insertion. `charInsertTrigger(char,line)` is called.
8586
| cancelHandle | function | Handle a user-signaled interrupt.

jquery.console.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
extern.typer = typer;
164164
extern.scrollToBottom = scrollToBottom;
165165
extern.report = report;
166+
extern.showCompletion = showCompletion;
166167
})();
167168

168169
////////////////////////////////////////////////////////////////////////
@@ -673,6 +674,14 @@
673674
};
674675

675676
function doComplete() {
677+
if(typeof config.completeHandle == 'function') {
678+
doCompleteDirectly();
679+
} else {
680+
issueComplete();
681+
}
682+
};
683+
684+
function doCompleteDirectly() {
676685
if(typeof config.completeHandle == 'function') {
677686
var completions = config.completeHandle(promptText);
678687
var len = completions.length;
@@ -705,6 +714,44 @@
705714
}
706715
}
707716
};
717+
718+
function issueComplete() {
719+
if (typeof config.completeIssuer == 'function') {
720+
config.completeIssuer(promptText);
721+
}
722+
};
723+
724+
function showCompletion(promptText, completions) {
725+
726+
var len = completions.length;
727+
if (len === 1) {
728+
extern.promptText(promptText + completions[0]);
729+
} else if (len > 1 && config.cols) {
730+
var prompt = promptText;
731+
// Compute the number of rows that will fit in the width
732+
var max = 0;
733+
for (var i = 0; i < len; i++) {
734+
max = Math.max(max, completions[i].length);
735+
}
736+
max += 2;
737+
var n = Math.floor(config.cols / max);
738+
var buffer = "";
739+
var col = 0;
740+
for (i = 0; i < len; i++) {
741+
var completion = completions[i];
742+
buffer += completions[i];
743+
for (var j = completion.length; j < max; j++) {
744+
buffer += " ";
745+
}
746+
if (++col >= n) {
747+
buffer += "\n";
748+
col = 0;
749+
}
750+
}
751+
commandResult(buffer, "jquery-console-message-value");
752+
extern.promptText(prompt);
753+
}
754+
};
708755

709756
function doNothing() {};
710757

0 commit comments

Comments
 (0)