npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tfo-ttty

v0.5.7

Published

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

Downloads

70

Readme

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] [parameter 2 description], etc.", when it requires arguments but is called without them.

API

initTerminal

| Method | Description | Parameters | | ------------- | ------------- | ------------- | | init(settings) | Initialize a terminal in a given DOM element | settings object. |

terminal

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

| Method | Description | Parameters | |----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------| | print(text, isCommand, scrollIntoView) | 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 prompt | newPrompt - String |

settings object

| Parameter | Description | Default | | ------------- | ------------- | ------------- | | host: DOM element | A DOM element to initialize terminal in. | | | welcomeMessage: string | A welcome message that is being printed on initialization | | | enableHelp: boolean | Toggle default help command that lists all the available commands and their descriptions. | true | | prompt: string | Terminal prompt | '$: ' | | historyLength: number | A maximum amount of commands that can be stored in the terminal history | 50 | | history: string[] | A default value for terminal history (can be used to preserve history across sessions) | [] | | commands: object | commands object | |

commands object

| Parameter | Description | | ------------- | ------------- | | name: string | Command name. | | description: string | Command description, used for the default help command (when enabled). | | argDescriptions: string array | Array 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

| Event | Description | ------------- | ------------- | | onInit | Terminal initialization | | onCommand | Existing command executed | | onCommand404 | Non-existing command executed | | onProcessStart | Process started | | onProcessStop | Process stopped | | onProcessInterrupt | Process 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!

| Variable | Description | ------------- | ------------- | | --terminal-bg-color | Background color | | --terminal-fg-color | Text color | | --terminal-font | Terminal font-family | | --terminal-accent-color | Accent color | | --terminal-error-color | Error 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.