| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A fully functional local echo controller for xterm.js
You will be surprised how difficult it is to implement a fully functional local-echo controller for xterm.js (or any other terminal emulator). This project takes this burden off your hands.
The local echo controller tries to replicate most of the bash-like user experience primitives, such as:
Install it using npm:
npm install --save wavesoft/local-echoOr yarn:
yarn add wavesoft/local-echoUse it like so:
import { Terminal } from 'xterm';
import LocalEchoController from 'local-echo';
// Start an xterm.js instance
const term = new Terminal();
term.open(document.getElementById('terminal'));
// Create a local echo controller (xterm.js v3)
const localEcho = new LocalEchoController(term);
// Create a local echo controller (xterm.js >=v4)
const localEcho = new LocalEchoController();
term.loadAddon(localEcho);
// Read a single line from the user
localEcho.read("~$ ")
.then(input => alert(`User entered: ${input}`))
.catch(error => alert(`Error reading: ${error}`));Download local-echo.js from the latest release
Include it in your HTML:
<script src="/js/local-echo.js"></script>
Use it like so:
// Start an xterm.js instance
const term = new Terminal();
term.open(document.getElementById('terminal'));
// Create a local echo controller (xterm.js v3)
const localEcho = new LocalEchoController(term);
// Create a local echo controller (xterm.js >=v4)
const localEcho = new LocalEchoController();
term.loadAddon(localEcho);
// Read a single line from the user
localEcho.read("~$ ")
.then(input => alert(`User entered: ${input}`))
.catch(error => alert(`Error reading: ${error}`));The constructor accepts an xterm.js instance as the first argument and an object with possible options. The options can be:
{
// The maximum number of entries to keep in history
historySize: 10,
// The maximum number of auto-complete entries, after which the user
// will have to confirm before the entries are displayed.
maxAutocompleteEntries: 100
}Reads a single line from the user, using local-echo. Returns a promise that will be resolved with the user input when completed.
localEcho.read("~$", "> ")
.then(input => alert(`User entered: ${input}`))
.catch(error => alert(`Error reading: ${error}`));Reads a single character from the user, without echoing anything. Returns a promise that will be resolved with the user input when completed.
This input can be active in parallel with a .read prompt. A character typed will be handled in priority by this function.
This is particularly helpful if you want to prompt the user for something amidst an input operation. For example, prompting to confirm an expansion of a large number of auto-complete candidates during tab completion.
localEcho.readChar("Display all 1000 possibilities? (y or n)")
.then(yn => {
if (yn === 'y' || yn === 'Y') {
localEcho.print("lots of stuff!");
}
})
.catch(error => alert(`Error reading: ${error}`));Aborts a currently active .read. This function will reject the promise returned from .read, passing the reason as the rejection reason.
localEcho.read("~$", "> ")
.then(input => {})
.catch(error => alert(`Error reading: ${error}`));
localEcho.abortRead("aborted because the server responded");Print a message (and change line) to the terminal. These functions are tailored for writing plain-text messages, performing the appropriate conversions.
For example all new-lines are normalized to \r\n, in order for them to appear correctly on the terminal.
Prints an array of strings, occupying the full terminal width. For example:
localEcho.printWide(["first", "second", "third", "fourth", "fifth", "sixth"]);Will display the following, according to the current width of your terminal:
first second third fourth fifth sixth
Registers an auto-complete handler that will be used by the local-echo controller when the user hits TAB.
The callback has the following signature:
function (index: Number, tokens: Array[String], [args ...]): Array[String] Where:
The function should return an array of possible auto-complete expressions for the current state of the user input.
For example:
// Auto-completes common commands
function autocompleteCommonCommands(index, tokens) {
if (index == 0) return ["cp", "mv", "ls", "chown"];
return [];
}
// Auto-completes known files
function autocompleteCommonFiles(index, tokens) {
if (index == 0) return [];
return [ ".git", ".gitignore", "package.json" ];
}
// Register the handlers
localEcho.addAutocompleteHandler(autocompleteCommonCommands);
localEcho.addAutocompleteHandler(autocompleteCommonFiles);| Back | FazBrowse Home | New Git URL |