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

Commit 4dc0a68

Browse files
committed
Merge pull request #47 from woshilaiceshide/master
add 2 features: "ctrl+u" and "asynchronous completion"
2 parents a2bf49e + 924c733 commit 4dc0a68

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
ehthumbs.db
1919
Icon?
2020
Thumbs.db
21+
/.project

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: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
// C-f
8585
70: moveForward,
8686
// C-k
87-
75: deleteUntilEnd
87+
75: deleteUntilEnd,
88+
// C-u
89+
85 : clearCurrentPrompt
8890
};
8991
if(config.ctrlCodes) {
9092
$.extend(ctrlCodes, config.ctrlCodes);
@@ -161,6 +163,7 @@
161163
extern.typer = typer;
162164
extern.scrollToBottom = scrollToBottom;
163165
extern.report = report;
166+
extern.showCompletion = showCompletion;
164167
})();
165168

166169
////////////////////////////////////////////////////////////////////////
@@ -420,6 +423,10 @@
420423
updatePromptDisplay();
421424
}
422425
};
426+
427+
function clearCurrentPrompt() {
428+
extern.promptText("");
429+
};
423430

424431
function deleteNextWord() {
425432
// A word is defined within this context as a series of alphanumeric
@@ -667,6 +674,14 @@
667674
};
668675

669676
function doComplete() {
677+
if(typeof config.completeHandle == 'function') {
678+
doCompleteDirectly();
679+
} else {
680+
issueComplete();
681+
}
682+
};
683+
684+
function doCompleteDirectly() {
670685
if(typeof config.completeHandle == 'function') {
671686
var completions = config.completeHandle(promptText);
672687
var len = completions.length;
@@ -699,6 +714,44 @@
699714
}
700715
}
701716
};
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+
};
702755

703756
function doNothing() {};
704757

0 commit comments

Comments
 (0)