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
- Host: GitHub
- URL: https://github.com/swivelgames/hack-term
- Owner: Swivelgames
- License: gpl-3.0
- Created: 2018-03-11T09:08:22.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-12T03:33:46.000Z (almost 8 years ago)
- Last Synced: 2025-01-12T17:46:19.610Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 61.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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`
});
```