React-React bash: A configurable/extendable bash terminal React component

<Terminal />

[NO LONGER MAINTAINED] ReactBash is a configurable / extendable bash terminal component. It provides an easy way of adding a terminal to your application. The terminal has a few built in base commands, and allows a simple means of extending the understandable commands. It boasts things like autocomplete on tab, previous command navigation, and a test suite you can trust. It is easy to install and get started.

npm install --save react-bash

Try out the DEMO

Props

prop description
extensions An object of bash command extensions
history An array of initial history items
structure An object representing the file system
theme A string representing which theme to use (Terminal.Themes.LIGHT, Terminal.Themes.DARK)
styles An object overriding the theme styles passed to each section
prefix The string used to prefix commands in history: defaults to [email protected]

Currently supported commands and args

command args/flags description
help lists all available commands
clear clears history
ls path lists all file and dirs at path or cwd
cat path/file prints out the contents of a file
mkdir path/dir makes a new dir at path
cd path change directory to relative path
pwd prints out the cwd
echo any prints out all args with env variables
printenv prints out env variables
whoami prints out current user's username

Extending the command list

The extension prop is an easy way to extend the bash commands that can be parsed from the terminal input. In essence, each command is a state reducer returning a new terminal state. This provides a lot of flexibility. Each command has access to the structure, history, and cwd, and expects the object returned to be applied in setState of the React component. Note that each extension should keep the state immutable, otherwise the component will not update. If we were to extend the commands with and existing command like 'clear, here's how we could do it.

import Terminal from 'react-bash';

export const clear = {
    exec: ({ structure, history, cwd }, command) => {
        return { structure, cwd, history: [] };
    },
};

const extensions = { clear };
<Terminal extensions={extensions} />

Each command is given the state and a parsed command object. The command object provides the name, input, args, and flags corresponding to the input. Some commands can use optional or required arguments. There are three types of args: anonymous args, named args (--), and flag args (-). To see how ReactBash parses the input.

For the input foo some/path -baz --hello world, ReactBash would parse the input as:

{
  command = 'foo'
  input: 'foo some/path -baz --hello world',
  args: {
    0: 'some/path',
    hello: 'world',
  },
  flags: { b: true, a: true, z: true },
}

History

The history prop and state arrays are lists of items that will be displayed as history items in the terminal. Essentially, anything that gets 'printed' out onto the terminal is a history item. The prefix prop is available to alter the bash user info that prepends commands in the history. If you'd like to add a welcome message to the initial state of the terminal, it's as easy as passing in a prop.

const history = [{ value: 'Welcome to the terminal!' }];
<Terminal history={history}  />

Structure

The structure object is a representation of the "file system" found within the terminal. It is what is used to navigate into/out of directories, display file contents, and suggesting autocompletions. Each key in the dict is either a directory or a file. If the object has a content field then it is assumed to be a file. This simplified the interface and makes it easier to get started. Here's an example of what a structure might look like.

const structure = {
    src: {
        file1: { content: 'This is the text content for <file1> of <src>' },
        file2: { content: 'This is the text content for <file2> of <src>' },
        childDir1: {
            file: { content: 'This is the text content for <file> of <src/childDir1>' },
        },
        childDir2: {
        }
    },
    '.hiddenDir': {
    },
    '.hiddenFile': { content: 'This is a hidden file' },
    file: { content: 'This is the text content for <file> of the root directory' },
};

Scripts

script description
npm start run the demo on localhost:3000
npm run test run the test suite
npm run lint run the linter

Patrons

Be the second to contribute! ⊂(✰‿✰)つ

Some ideas for contributions:

  • Add grep command that walks/searches the structure
  • Add multiline support / text formatting for cat

License

MIT

Comments

  • Quotes
    Quotes

    Jul 1, 2016

    [email protected] ~ $echo "foo"
    "foo"
    
    Reply
  • `ls -a` should show all entries, including `.` and `..`
    `ls -a` should show all entries, including `.` and `..`

    Nov 14, 2016

    Per the man pages, -a is the equivalent to --all and should include the current directory (.) and parent directory (..) in its listings.

    The current functionality of -a in react-bash is more of -A which is --almost-all and does "not list implied . and .."

    image

    Reply
  • Regex support
    Regex support

    Nov 14, 2016

    Ability to execute commands on the filesystem using a regular expression to specify files. image image

    Reply
  • How to I get my command?
    How to I get my command?

    Feb 14, 2020

    Hi, Help me to get whatever things I am typing on a terminal. In the below I type 'cd list' and I want to console value. How to get value? Screenshot from 2020-02-14 11-00-27

    Reply
  • Support react-native
    Support react-native

    Jun 7, 2020

    It would be best if you can continue this project and make it available for react-native :)

    Reply
  • Removing the title bar.
    Removing the title bar.

    Jul 22, 2020

    I am trying to use react-bash. But, I require it without the title bar.

    Reply
  • Base commands should be overridable
    Base commands should be overridable

    Mar 2, 2017

    It is currently impossible to override the base commands, although it might be desirable for a user to change the behaviour of a given command. One possible use case might be to link a structure modifying command to an actual request to an external API:

    import * as builtin from 'react-bash/commands';
    
    const mkdir = {
        exec: (state, { args }) => {
            someExternalAPI.execute('mkdir', args);
            return builtin.mkdir(state, args);
        },
    };
    
    const extensions = { mkdir };
    <Terminal extensions={extensions} />
    

    I think it should suffice to replace src/bash.js:9 with the following:

    this.commands = Object.assign({}, BaseCommands, extensions);
    
    Reply
  • Allow overriding default styles
    Allow overriding default styles

    Mar 18, 2017

    I'm using this awesome component in my project and need to hide the title bar, as I provide my own ;)

    Reply
  • ls doesn't respect -l option
    ls doesn't respect -l option

    Jun 9, 2016

                                                                                                                                                                                                            enhancement 
    Reply
  • please add
    please add "outline: none" to the style of input

    Jan 14, 2019

    In some cases,it will be: image

    Reply
  • How to create async command?
    How to create async command?

    Apr 13, 2017

    I would like to be able to execute commands asynchronously.

    For example:

    const extensions = {
      asyncCommand1: {
        exec: (state, command) => {
          return Promise.resolve(state)
        }
      }
    }
    

    How can i do it?

    Reply
  • Glitch when scrolling
    Glitch when scrolling

    Jun 7, 2016

    When bottom of the terminal window is reached, the white background scrolls up-

    image

    Reply