An open API service indexing awesome lists of open source software.

https://github.com/swivelgames/hack-term

Simple Terminal-style REPL
https://github.com/swivelgames/hack-term

Last synced: 11 months ago
JSON representation

Simple Terminal-style REPL

Awesome Lists containing this project

README

          

# hack-term

Simple, Hackable Terminal-style REPL

* Hackable, via `.use()` and `.registerCommand()`
* Thenable Events (`.when('stdout').then(() => /*something outputted! */)`)

```javascript
import { Terminal } from 'hack-term';

const TerminalInstance = new Terminal({
promptDelim: '$ ',
username: process.env.USER,
motd: 'Welcome to the Hackable REPL',
verbose: false
});
```

```
$ yarn start

Welcome to the Hackable REPL

localuser$ /echo "Hello World"
Hello World

localuser$
```

## Events

In addition `thenable-events` is used to enable promise-compatible events.

- `stdout`: Resolved to output text
- `stdin`: Resolved when data is piped in
- `stderr`: Resolved to output error text
- `command.exec`: Resolved with AST when commands will be executed
- `command.exit`: Resolved or Rejected when a command exits

### .when( event ).then( handler )

Listening for events is done using `.when().then()` syntax.

```javascript
TerminalInstance
.when('stdout')
.then((txt) => {
console.log('Something outputted!', txt)
});
```

### .resolve( event, value ) / .reject( event, error )

Resolving / Rejecting events (similar to `emit`ing in traditional event-based systems) are done through the `.resolve()` and `.reject()` methods:

```javascript
TerminalInstance.resolve('stdout', 'Output something...'); // Output
TerminalInstance.reject('command.exit', 127); // "Command exited with non-zero"
```

## Middleware

This library leverages `meddle` to enable the use of middleware via `TerminalInstance.use()`.

```javascript
const myMiddleware = (Term) => {
Term.registerCommand('myCommand', {
handler: () => {
Term.resolve('stdout', 'Hello World!');
Term.resolve('command.exit', 0);
},
man: `Usage: /myCommand`
});
}

TerminalInstance.use(myMiddleware);
```

See [`API.md`](https://github.com/Swivelgames/hack-term/wiki/Terminal-API) for more details!

### .registerCommand( name, package )

**Parameters**
- name `string` -> `/${name}${...argv}`
- package `object` -> `{ handler: fn(argv), man: 'Usage / Help Info' }`

**Example:**

```javascript
TerminalInstance.registerCommand('myCommand', {
handler: () => {
Term.resolve('stdout', 'Hello World!');
Term.resolve('command.exit', 0);
},
man: `Usage: /myCommand`
});
```