All Projects β†’ wavesoft β†’ local-echo

wavesoft / local-echo

Licence: Apache-2.0 License
A local-echo controller for xterm.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to local-echo

Wetty
Terminal in browser over http/https. (Ajaxterm/Anyterm alternative, but much better)
Stars: ✭ 3,076 (+3104.17%)
Mutual labels:  xterm, xterm-js
darktile
🌘 Darktile is a GPU rendered terminal emulator designed for tiling window managers.
Stars: ✭ 2,694 (+2706.25%)
Mutual labels:  xterm, terminal-emulators
webterm
web terminal based on xterm.js in rust
Stars: ✭ 20 (-79.17%)
Mutual labels:  xterm, xterm-js
Ttyd
Share your terminal over the web
Stars: ✭ 4,030 (+4097.92%)
Mutual labels:  xterm, terminal-emulators
contour
Modern C++ Terminal Emulator
Stars: ✭ 761 (+692.71%)
Mutual labels:  xterm, terminal-emulators
uniterm
🚧Universal Terminal Emulator, might be a great toy terminal front-end for geeks.
Stars: ✭ 22 (-77.08%)
Mutual labels:  terminal-emulators, xterm-js
Xterm.js
A terminal for the web
Stars: ✭ 12,019 (+12419.79%)
Mutual labels:  xterm, terminal-emulators
Aminal
🌘 Darktile is a GPU rendered terminal emulator designed for tiling window managers.
Stars: ✭ 2,663 (+2673.96%)
Mutual labels:  xterm, terminal-emulators
bashme
πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’» The first command line interface about you
Stars: ✭ 32 (-66.67%)
Mutual labels:  xterm-js
blessed-xterm
XTerm Widget for Blessed Curses Environment
Stars: ✭ 37 (-61.46%)
Mutual labels:  xterm
wcwidth-icons
Support fonts with double-width icons in xterm/rxvt-unicode/zsh/vim/…
Stars: ✭ 36 (-62.5%)
Mutual labels:  xterm
mapbox-assistant-example
Examples of Amazon Echo, Google Home, and other bots interacting with Mapbox services.
Stars: ✭ 15 (-84.37%)
Mutual labels:  echo
echo-template
golang template for echo framework!
Stars: ✭ 39 (-59.37%)
Mutual labels:  echo
PythonAudioEffects
A Python library that can apply: darth vader, echo, radio, robotic, and ghost effects to audio samples.
Stars: ✭ 26 (-72.92%)
Mutual labels:  echo
docker-exec-web-console
A web UI to docker exec from the browser
Stars: ✭ 66 (-31.25%)
Mutual labels:  xterm-js
editor.sh
Yet another live programming environment for collaborative code editing and running.
Stars: ✭ 29 (-69.79%)
Mutual labels:  xterm-js
iCtrl
UofT Engineering Lab Remote
Stars: ✭ 91 (-5.21%)
Mutual labels:  xterm-js
echo-mw
η»ŸδΈ€η§»εˆ°hb-go/echo-web ☞
Stars: ✭ 17 (-82.29%)
Mutual labels:  echo
xterm-js-rs
Rust-WebAssembly bindings to the xterm-js Javascript library
Stars: ✭ 14 (-85.42%)
Mutual labels:  xterm-js
Offensive-Reverse-Shell-Cheat-Sheet
Offensive Reverse Shell (Cheat Sheet)
Stars: ✭ 138 (+43.75%)
Mutual labels:  xterm

πŸ“’ local-echo Travis (.org) Try it in codepen.io

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.

Features

The local echo controller tries to replicate most of the bash-like user experience primitives, such as:

  • Arrow navigation: Use left and right arrows to navigate in your input
  • Word-boundary navigation: Use alt+left and alt+right to jump between words
  • Word-boundary deletion: Use alt+backspace to delete a word
  • Multi-line continuation: Break command to multiple lines if they contain incomplete quotation marks, boolean operators (&& or ||), pipe operator (|), or new-line escape sequence (\).
  • Full-navigation on multi-line command: You are not limited only on the line you are editing, you can navigate and edit all of your lines.
  • Local History: Just like bash, access the commands you previously typed using the up and down arrows.
  • Tab-Completion: Provides support for registering your own tab-completion callbacks.

Usage

As ES6 Module

  1. Install it using npm:

    npm install --save wavesoft/local-echo

    Or yarn:

    yarn add wavesoft/local-echo
  2. Use 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}`));

Directly in the browser

  1. Download local-echo.js from the latest release

  2. Include it in your HTML:

    <script src="https://github.com/js/local-echo.js"></script>
    
  3. 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}`));

API Reference

constructor(term, [options])

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
}

.read(prompt, [continuationPrompt]) -> Promise

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}`));

.readChar(prompt) -> Promise

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}`));

.abortRead([reason])

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([message])

.println([message])

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.

.printWide(strings)

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

.addAutocompleteHandler(callback, [args...])

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:

  • index: represents the current token in the user command that an auto-complete is requested for.
  • tokens : an array with all the tokens in the user command
  • args... : one or more arguments, as given when the callback was registered.

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);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].