Code Monkey home page Code Monkey logo

terminal.js's Introduction

terminal.js

An extensible pseudo-terminal for your website (or whatever you do in the browser)

What's this?

Terminal.js is a Javascript terminal emulation for use in the browser. And it's available as a web component, too.

I have a live demo here and the component version of the demo can be found here

You can do a bunch of things with it:

  • Create a CLI-style API interface that runs in the browser
  • Create a remote terminal emulator for something that exposes an interface in a browser-consumable way (CORS, Websocket, WebRTC, ...)
  • Create a text-based adventure game in the browser
  • whatever you can come up with, where a command line interface is useful.

How do I use it?

It's really easy:

  1. You include the terminal.css and terminal.js files and have a container element (e.g. a div) with a child element holding a contenteditable element of class input and another span with the actual prompt line you wanna display.
  2. You create an object with methods that will be your commands (see below for the details of how this works)
  3. Call terminal.init and pass the container element and your commands object - Ready to roll!

Here's a minimal example:

  <div id="myterminal">
    <p>
      <span>$> </span>
      <span contenteditable="true" class="input"></span>
    </p>    
  </div>
  <script src="terminal.js"></script>
  <script>
    var commands = {
      hi: function() {
        return '<p>Hi there!</p>'
      }
    }
    
    Terminal.init(document.getElementById("myterminal"), commands)
  </script>

Extensible command interface

The terminal is only a way to interact with "commands" and "commands" are a bundles of functionality.
So to use the terminal, you'll need to create a bunch of functions that actually do something - and that's not hard.

A greeting command

So let's build a command that greets the user with the name she enters, like this:

$ hello Alice
Hi there, Alice

in Terminal.js this is done by creating a commands object and add a "hello" method to it.
That method will take one parameter, which will be the array of arguments (separated by spaces) entered to call the command and returns HTML to be displayed in the terminal.

var commands = {
  hello: function(args) {
    if(args.length < 2) return "<div>Please tell me your name like this: <pre>hello Alice</pre></div>";
    return "<div>Hi there, " + args[1] + "</div>";
  }
};

Note that the args array's first element is the name of the command itself.

Now we can make our terminal (I left parts of the HTML out, see index.html for a full example):

<div id="terminal">
  <p>
    <span>$ </span>
    <span contenteditable="true" class="input"></span>
  </p>
</div>
<script>
var commands = {
  hello: function(args) {
    if(args.length < 2) return "<div>Please tell me your name like this: <pre>hello Alice</pre></div>";
    return "<div>Hi there, " + args[1] + "</div>";
  }
};

terminal.init(document.getElementById("terminal"), commands);
</script>

and we're done. We have a terminal that can greet the user :)

A full example

The code for the live demo lives in index.html and another example is at my website and its github repository in console.html and javascripts/terminal.js.

License

MIT License - basically: Do whatever you feel like, but don't sue me when it blows up.

terminal.js's People

Contributors

avgp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.