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

add 2 features: "ctrl+u" and "asynchronous completion" #47

Merged
merged 3 commits into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
ehthumbs.db
Icon?
Thumbs.db
/.project
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Here are options which can be passed to `console`:
| commandValidate | function | When user hits return, validate whether to trigger commandHandle and re-prompt.
| 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.
| completeHandle | function | Handle the command completion when the tab key is pressed. It returns a list of string completion suffixes.
| 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.
| animateScroll | bool | Whether to animate the scroll to top. Currently disabled.
| charInsertTrigger | function | Predicate for whether to allow character insertion. `charInsertTrigger(char,line)` is called.
| cancelHandle | function | Handle a user-signaled interrupt.
Expand Down
55 changes: 54 additions & 1 deletion jquery.console.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
// C-f
70: moveForward,
// C-k
75: deleteUntilEnd
75: deleteUntilEnd,
// C-u
85 : clearCurrentPrompt
};
if(config.ctrlCodes) {
$.extend(ctrlCodes, config.ctrlCodes);
Expand Down Expand Up @@ -161,6 +163,7 @@
extern.typer = typer;
extern.scrollToBottom = scrollToBottom;
extern.report = report;
extern.showCompletion = showCompletion;
})();

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -420,6 +423,10 @@
updatePromptDisplay();
}
};

function clearCurrentPrompt() {
extern.promptText("");
};

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

function doComplete() {
if(typeof config.completeHandle == 'function') {
doCompleteDirectly();
} else {
issueComplete();
}
};

function doCompleteDirectly() {
if(typeof config.completeHandle == 'function') {
var completions = config.completeHandle(promptText);
var len = completions.length;
Expand Down Expand Up @@ -699,6 +714,44 @@
}
}
};

function issueComplete() {
if (typeof config.completeIssuer == 'function') {
config.completeIssuer(promptText);
}
};

function showCompletion(promptText, completions) {

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() {};

Expand Down