1.6.0 • Published 10 months ago

ttty v1.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

ttty

A dead simple lightweight TypeScript terminal "emulator" that mimics terminal behaviour in browser.

Features:

  • Tiny, dependency-free and built with modern JS
  • Easy-to-add custom commands
  • Events
  • Command history
  • Command arguments with validation
  • "Foreground processes" imitation

Usage

With module bundler

Add the latest release with:

npm install ttty or yarn add ttty

Initialize the terminal in a particular DOM element with:

import { initTerminal } from 'ttty'

// ...

const terminal = initTerminal({ /* settings */ })

In a browser directly

<!-- As a global JS file -->
<script src="https://unpkg.com/ttty/dist/ttty.iife.js"></script>
ttty.initTerminal({ /* settings */ })

Initialize with parameters:

const settings = {
    host: document.querySelector("#terminal"),
    prompt: "user@ttty:~$ ",
    commands: {
        echo: {
            name: "echo", 
            description: "a test command with one echo arg", 
            argDescriptions: ["a string to be echoed in console"],
            func: ({ print }, argument) => { print(argument) } 
        },
        test: {
            name: "test", 
            description: "a test command with no args", 
            func: ({ print }) => { print("foo") } 
        },
        multiply: {
            name: "multiply",
            description: "Multiply two numbers",
            argDescriptions: ["number one", "number two"],
            func: ({ print }, one, two) => { print(Number(one) * Number(two)) }
        }
    }
}

initTerminal(settings)

Working with the terminal

help - Display a list of all commands with descriptions

command - Execute a command. Will display "Usage: command parameter 1 description, etc.", when it requires arguments but is called without them.

API

initTerminal

MethodDescriptionParameters
init(settings)Initialize a terminal in a given DOM elementsettings object.

terminal

An object that's being passed to every command function & returned by initTerminal

MethodDescriptionParameters
print(text, isCommand)Prints a given text in the terminal (accepts raw HTML)text - String, isCommand - Boolean, optional, defaults to false. Count given string as a command (displays prompt & syntax highlight)
run(text)Emulates a command execution in a terminal (acts the same way as a user would have typed and pressed Enter)text - String
start()Starts a "foreground process": user input is blocked and command prompt never appears.
stop()Stops "foreground process".
type(text, speed, isCommand) => Promise<boolean>Prints a text with "typing" effect. Hides and blocks user input while typing. Resolves to either true or false depending on process interruption by the user.text - String, text to be printed. speed - integer, miliseconds. The higher the number, the slower. isCommand - Boolean, optional, defaults to false. Count given string as a command (displays prompt & syntax highlight)
setPrompt()Set terminal promptnewPrompt - String

settings object

ParameterDescriptionDefault
host: DOM elementA DOM element to initialize terminal in.
welcomeMessage: stringA welcome message that is being printed on initialization
enableHelp: booleanToggle default help command that lists all the available commands and their descriptions.true
prompt: stringTerminal prompt'$: '
historyLength: numberA maximum amount of commands that can be stored in the terminal history50
history: string[]A default value for terminal history (can be used to preserve history across sessions)[]
commands: objectcommands object

commands object

ParameterDescription
name: stringCommand name.
description: stringCommand description, used for the default help command (when enabled).
argDescriptions: string arrayArray of strings that describe command line arguments in order of appearance.
func: function(terminal, ...arguments)Function. Accepts an array of parameters in order of appearance (i.e. function(terminal, one, two) will correspond to two arguments passed as command one two)

Events

EventDescription
onInitTerminal initialization
onCommandExisting command executed
onCommand404Non-existing command executed
onProcessStartProcess started
onProcessStopProcess stopped
onProcessInterruptProcess interrupted with a keyboard

Events are being dispatched by the DOM element passed on init, for example:

const term = document.getElementById('terminal');
term.addEventListener('onCommand', e => console.log("known command executed!"));

Custom styling

You can customize the look defining custom CSS variables. If you have multiple instances, you can even have each instance in its' own style!

VariableDescription
--terminal-bg-colorBackground color
--terminal-fg-colorText color
--terminal-fontTerminal font-family
--terminal-accent-colorAccent color
--terminal-error-colorError color

Browser compatibility

ttty is built and distributed with ES6 in mind (including the minified package). You can always transpile & bundle it targeting your browser set of choice.

Browsers that do not support CSS variables (IE < 11) might not be able to make use of custom themes. In order to use ttty with older browsers please rebuild this with custom properties removed.

1.6.0

10 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.3.0

10 months ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago