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

dom-terminal

v1.0.3

Published

Simple command-line terminal for your browser app

Downloads

10

Readme

Build Status

Terminal

Simple command-line terminal for your browser app.

Web apps are great. But sometimes instead of all the double-clicks, mouse pointers, taps and swipes across the screen - you just want good old keyboard input. This terminal runs in a browser, desktop or mobile. It provides a simple and easy way to extend the terminal with your own commands.

Enjoy.

How to use

Include terminal.js and terminal.css in your HTML:

<script src="terminal.min.js"></script>
<link rel="stylesheet" media="all" href="terminal.min.css">

Define an HTML div tag where the terminal will be contained:

<div id="terminal"></div>

Create a new terminal instance and convert the DOM element into a live terminal.

var terminal = new Terminal('terminal', {}, {});

This won't be terribly useful because by default the terminal doesn't define any commands. It's meant to be extended. Let's define some:

var terminal = new Terminal('terminal', {}, {
  execute: function(cmd, args) {
    switch (cmd) {
      case 'clear':
        terminal.clear();
        return '';

      case 'help':
        return 'Commands: clear, help, theme, ver or version<br>More help available <a class="external" href="http://github.com/sasadjolic/dom-terminal" target="_blank">here</a>';

      case 'theme':
        if (args && args[0]) {
          if (args.length > 1) return 'Too many arguments';
          else if (args[0].match(/^interlaced|modern|white$/)) { terminal.setTheme(args[0]); return ''; }
          else return 'Invalid theme';
        }
        return terminal.getTheme();

      case 'ver':
      case 'version':
        return '1.0.2';

      default:
        // Unknown command.
        return false;
    };
  }
});

This gives us 4 commands: clear, help, theme, and ver or version. Go ahead and try it out using our live example.

Features

Welcome message

The default welcome message is empty but you can set it to be anything.

var terminal = new Terminal('terminal', {welcome: 'Welcome to awesome terminal!'}, {});

Command-line history

History is automatically kept track of. You can use up and down arrow keys to go through the command-line history, just like a standard shell.

Prompt and separator

You can change the prompt. The default is an empty prompt with a > separator. Set the prompt option to change.

var terminal = new Terminal('terminal', {prompt: 'C:\\', separator: '&gt;'}, {});

This will display the prompt like this: C:\>

Theme

There are 3 built-in themes:

  • interlaced
  • modern
  • white

modern theme is the default

modern is a clean modern theme that's white on black, while the white theme is black on white.

If you're nostalgic about the old-school CRT monitors then the interlaced theme is for you.

Set the theme in options:

var terminal = new Terminal('terminal', {theme: 'interlaced'}, {});

Or alternatively use the API to change the theme after creating the terminal object.

Command extensions

The default terminal doesn't provide any built-in commands. You must define your own set of commands. This can be done through options when creating the object.

var terminal = new Terminal('terminal', {}, {
  execute: function(cmd, args) {
    switch (cmd) {
      case 'hello':
        return 'world';

      // Place your other commands here.

      default:
        // Unknown command.
        return false;
    };
  }
});

When executing commands you can run whatever code you want. Just return a string that you want to be displayed in the terminal as the output of your command. The example above would produce:

> hello
world
> 

Return false to indicate that this is an unknown command.

You can have more than one command set and you can list them out one after another:

var terminal = new Terminal('terminal', {}, {
  execute: function(cmd, args) {
    switch (cmd) {
      case 'hello': return 'world';
      default: return false;
    };
  }
},{
  execute: function(cmd, args) {
    switch (cmd) {
      case 'echo': return args[0];
      default: return false;
    };
  }
});

You might want to use multiple command sets, for example, if your app has modules that hook into the terminal independently.

Local storage persistence

Command-line history is persisted using HTML5 local storage.

API

Clear the screen

terminal.clear();

Change the theme

terminal.setTheme('interlaced');

Sets CSS class terminal-interlaced on the terminal DOM element. The three built-in themes are interlaced, modern, and white. You can also make your own theme and pass in your theme's name into setTheme.

To retrieve the name of the active theme, use getTheme.

console.log(terminal.getTheme());

Set the prompt

terminal.setPrompt('test');

Because the default separator is > this will display:

test>

You can set the separator in options while creating the Terminal object. At this time it's not possible to change the separator after the terminal is created.

Prompt can be changed at any time.

Thank-yous

I was inspired by the HTML5 Terminal demo for Chrome created by Eric Bidelman. Thanks! You can find his demo and source code here: http://www.htmlfivewow.com/demos/terminal/terminal.html http://code.google.com/p/html5wow/source/browse/#hg%2Fsrc%2Fdemos%2Fterminal

My version of the terminal component is completely rewritten and has no code in common with the source, but it does borrow ideas from Eric's demo. It also works for other browsers, whereas Eric's version is optimized for Chrome.

License

(The MIT License)

Copyright (c) 2012-2017 Sasa Djolic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.