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:
Define an HTML div tag where the terminal will be contained:
Create a new terminal instance and convert the DOM element into a live terminal.
var 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 = 'terminal' {} { ; };
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 = '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 = 'terminal' prompt: 'C:\\' separator: '>' {};
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 = '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 = 'terminal' {} { ; };
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 = 'terminal' {} { ; } { ; };
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
terminalclear;
Change the theme
terminal;
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;
Set the prompt
terminal;
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.