1.4.0 • Published 2 years ago

brsh v1.4.0

Weekly downloads
8
License
GPL-3.0
Repository
github
Last release
2 years ago

BRowserSHell (brsh)

npm version

An extensible shell written in Javascript with an accompanying terminal emulator

Demo

What is it?

A Unix like Shell backend and Terminal Emulator that allows you top create a Terminal within you webapp.

Why?

No idea, you tell me

Known Issues

  • Input on android is not working
  • scripting isn't complete yet, if, while etc not implemented
  • commands can't currently stream output, it is only returned once the app process finishes

How?

To create a new terminal first add both Terminal and Shell to your page like so:

<script src="//unpkg.com/brsh@1/dist/shell.min.js"></script>
<script src="//unpkg.com/brsh@1/dist/terminal.min.js"></script>

You man also use terminal as a node module if you are bundling your code with something like browserify etc Install the module via NPM:

npm i brsh

require the module in your code:

const Terminal = require('brsh/lib/Terminal');

you may then implement the terminal like so:

const term = new Terminal({
    el: document.body,
    profile: '/test/.profile',
    cwd: '/test',
    filesystem: {"test": {".profile": "echo \"hi there!\""}},
    cursor: 'blink',
    outputAnimation: 'type',
    animateSpeed: 2
});

The Terminal constructor Options extend those of the Shell:

Terminal Specific options

optionrequireddefaultdescription
elYesa HTMLElement to fill with the terminal emulator
fontNoRoboto Monothe name of a google webfont to load for the emulators console font, a monospace font is recommended
cursorNononeOne of: none, block, bink if block or blink a cursor block character will show at the end of the current line, if bink this will animate with a simple blink
outputAnimationNononeOut of: none, type if type is set each character will be appended in order as to animate a more retro style of console output
animateSpeedNo1number of frames to append per browser frame, the higher this number the faster the animation, i would recommend setting this higher than the default value of 1
onExitNonullfunction to run should the Shell session be destroyed, whist no required, if this is not set and the user types exit, the terminal will then hang in an ended state
mobileInputNoclickOne of none or click, set to click to enable focus on the terminal (and show the onscreen keyboard on mobile

Shell options

optionrequireddefaultdescription
pathNo'/bin'path in which to look for (and add default entries) binaries
profileNoa script to run before user interactivity (see scripts)
hostnameNo'browser'the hostname of the instance, by default the included terminal emulator shows this as the prompt text
filesystemNo{}an object to represent the file system, any classes that extend Command will be treated as binaries it is probably best to use the included exportFileSystem cli application to convert a directory to a filesystem object
cwdNo'/'the starting CWD for the shell, this affects relative paths to files, binaries and directories just like in any other shell

you may also wish to implement your own terminal emulator, are use the Shell directly within node CLI in which case you can use just Shell by requiring:

const Shell = require('brsh');

the Shell class can be implemented like so:

const shell = new Shell({
    profile: '/root/.profile',
    cwd: '/root',
    filesystem: require('./filesystem')
});

the following methods and Events are available to interact with the shell instance

Shell Public Methods

Methodvariablesdescription
onCommandString commanda command to run in the shell e.g. ls -al ./root
tabCompletionString completion candidatea candidate for completion, for instance ./test/compl, an array will be returned of possible completion strings
getPromptreturns the current hostname and cwd
destroydestroys the instance of the Shell
clearcases the clear event to fire

Shell Events

Eventvariablesdescription
stdOutString linea single line of standard output from the shell to display to the user
stdErrString linea single line of standard error from the shell to display to the user
exitCodeNumber codethe exit code of any command run in the shell
statusString StatusShell status, this should be listened to to check if commands can be accepted, state can be one of: Shell.STATUS_READY or Shell.STATUS_WORKING = 'WORKING', listen for Shell.STATUS_READY to show a prompt to use the user
clearthis event is fired to tell the Terminal emulator to clear the screen
exitNumber codethis event if fired when the terminal is destroyed, the code contains the final status code from the shell instance

an example implementation of this shell running as a CLI can be found here

Builtin and Local commands

these are in general simplified versions of what you will find in a standard unix shell

  • cat
  • echo
  • ls

  • alias

  • cd
  • clear
  • export
  • source
  • which
  • exit

adding new commands:

new commands can be added by creating a class that extends Command, you can either access command as a Static property of Shell:

class Test extends Shell.Command {
    ...
}

or Require Command directly:

const Command = require('shell.js/lib/Command');

class Test extends Command {
    ...
}

for more on writing commands see the docs here

Writing Scripts

to create a script you must first set the Shebang interpreter to #!/sh.js as the first line of the file

you may then run any commands in the system which will run as part of the script.

it is important to note that scripts run in a sub-shell and do not share context, if you wish from context changes from your script to persist you will need to use the source command like in Unix.

a final note, if you are using exportFileSystem you must not set the file extension of a script to .js as this will cause to to be interpreted as javascript rather than a script string

More Examples

On my website I have extended the functionality adding some simple extra utilities such as an ANSI image viewer and a markdown parser based on opensource node cli utilites.

See the Site in action here

Github Source Code here

1.4.0

2 years ago

1.3.0

2 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.2.1

3 years ago

1.1.2

3 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago