Skip to content

Commit

Permalink
Merge pull request #4 from Nightmarlin/master
Browse files Browse the repository at this point in the history
Add inference of clearCommand and separator
  • Loading branch information
daltonmenezes authored May 10, 2019
2 parents dc70db4 + 9983094 commit 4382437
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 40 deletions.
116 changes: 84 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<p align="center">The ultimate and most complete extension to initialize commands before and after <a href="https://hyper.is/">Hyper terminal</a> starts
<br/><br/>
<a href="https://www.patreon.com/daltonmenezes"><img src="https://img.shields.io/badge/become%20a-patron%20or%20sponsor-orange.svg" alt="become a patron or sponsor" /></a>
<a href="https://www.patreon.com/daltonmenezes"><img src="https://img.shields.io/badge/become%20a-patron%20or%20sponsor-orange.svg" alt="become a patron or sponsor" /></a>
<a href="https://paypal.me/daltonmenezes"><img src="https://img.shields.io/badge/Donate-green.svg" alt="Donate" /></a>
<a href="https://www.npmjs.com/package/hyper-init"><img src="https://img.shields.io/npm/v/hyper-init.svg" alt="npm version"/></a>
<img src="https://img.shields.io/npm/dm/hyper-init.svg?label=Downloads" alt="downloads" />
Expand All @@ -14,24 +14,27 @@
</a>
</p>

> With **hyper-init** you can perform as many commands as you would like to do, before and after Hyper terminal starts, using rules that defines when your commands should run.
> With **hyper-init** you can perform as many commands as you would like to do, before and after Hyper terminal starts, using rules that define when your commands should run.
<p align="center"><img src="https://github.com/daltonmenezes/hyper-init/blob/master/img/hyper-init.gif?raw=true" alt="hyper-init gif"/></p>

## Table of Contents

- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Configuration](#configuration)
- [init](#init)
- [Rules](#rules)
- [Commands](#commands)
- [clearCommand](#clearcommand)
- [init Options](#init-options)
- [Rules](#rules)
- [Commands](#commands)
- [commandSeparator](#commandseparator)
- [Contributing](#contributing)
- [License](#license)


## Installation

If you don't have Hyper, install it from [here](https://hyper.is/#installation).
If you don't have Hyper, install it [here](https://hyper.is/#installation).

So, type the following on Hyper:

Expand All @@ -41,11 +44,11 @@ hyper i hyper-init

## Configuration

## init
### init

```hyper-init``` can be configured in ```~/.hyper.js``` configuration file within the ```config``` object.
`hyper-init` can be configured within the `config` object in the `~/.hyper.js` configuration file.

All you have to do to get started is to create an array of objects called ```init```.
All you have to do to get started is to create an array of objects called `init`, like this:

```js
init: [
Expand All @@ -56,11 +59,11 @@ init: [
]
```

Your ```~/.hyper.js``` configuration file should look like this:
Your `~/.hyper.js` configuration file should look like this:
```js
module.exports = {
config: {

// add hyper-init configuration like this:
init: [
{
Expand All @@ -71,46 +74,95 @@ module.exports = {
rule: 'windows',
commands: ['echo This is only executed on New Windows!']
}
]
]
},

plugins: ['hyper-init']

}
```

## clearCommand
#### Rules
A string that defines when you want your commands to run.

Rule | Description
--- | ---
`once` | executes your commands only at Hyper starts
`windows` | executes your commands only when a new Hyper window opens
`tabs` | executes your commands only when a new tab is opened
`splitted` | executes your commands only when a new pane is opened
`all` | executes your commands every time a terminal opens

#### Commands
An array with your shell commands to run.<br/>
You can perform as many commands as you would like.

Example:
```js
commands: ['cd ~/Desktop', 'ls']
```


Also `hyper-init` clears the terminal buffer using `printf "\\033[H"` as the default value, but you can set it manually adding the `clearCommand: ''` property within the `config` object. For example:
### clearCommand

`hyper-init` can infer the command to clear the screen for a small number of terminals.
If it can't infer the command, `hyper-init` clears the terminal buffer using `printf "\\033[H"`.
You can set it manually adding the `clearCommand: ''` property within the `config` object.
For example:

```js
module.exports = {
config: {
clearCommand: 'reset'
}
config: {
clearCommand: 'reset'
}
}
```

## init: Options
### commandSeparator

### Rules
A string that defines when you want your commands to run.
`hyper-init` uses ` && ` as the default separator for commands.
For known terminals, `hyper-init` can infer the separator.
You can also set it manually by adding the `commandSeparator: ''` property within the `config` object,
but this overrides for all terminals, even ones that don't support that delimiter.
For example:

Rule | Description
--- | ---
`once` | executes your commands only at Hyper starts
`windows` | executes your commands only at new windows
`tabs` | executes your commands only at new tabs
`splitted` | executes your commands only at splitted windows
`all` | executes your commands with all described states previously
```js
module.exports = {
config: {
commandSeparator: ' ++ ' // For an arbitrary terminal that uses `++`
}
}
```

### Commands
An array with your shell commands to run.<br/>
You can perform as many commands as you would like to do.
## Contributing

`hyper-init`'s ability to infer the `clearCommand` and `commandSeparator` is based on its relatively small dictionary.
Feel free to add more definitions for terminals not listed in `get-specifics.js`.

Example:
```js
commands: ['cd ~/Desktop', 'ls']
KNOWN_SHELLS = {
[...]
shellName: {
separator: '',
clearCommand: ''
}
[...]
}
```

- `shellName` should be replaced with the name of the shell you want to target (lowercase)
- The value of `separator` should be the separator for multiple statements on one line (IE `' && '`) as a string
- The value of `clearCommand` should be the command to clear the target shell (IE `'cls'`) as a string

```js
KNOWN_SHELLS = {
[...]
powershell: {
separator: '; ',
clearCommand: 'Clear-Host'
}
[...]
}
```

## License
Expand Down
Binary file added img/hyper_test_cmd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hyper_test_node.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hyper_test_powershell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
const { rulesHandler } = require('./src/rules/rules-handler')
const { clearBuffer } = require('./src/clear-buffer')
const { joinCommands } = require('./src/join-commands')
const { getSeparator } = require('./src/get-specifics')
const { getClearCommand } = require('./src/get-specifics')
const waitFor = require('./src/wait-for')

const init = {}
const terminal = {}
let clearCommand
let commandSeparator

exports.onApp = ({ config }) => {
clearCommand = config.getConfig().clearCommand || undefined
commandSeparator = config.getConfig().commandSeparator || undefined
Object.assign(init, config.getConfig().init)
}

Expand All @@ -24,18 +28,22 @@ exports.reduceTermGroups = reducer =>
exports.middleware = store => next => action => {
if (action.type === 'SESSION_ADD')
Object.assign(terminal, { splitDirection: action.splitDirection })

next(action)
}

exports.onWindow = app =>
app.rpc.on('hyper-init execute commands', ({ uid, terminal }) => {
clearBuffer({ app, uid }, clearCommand)
exports.onWindow = browserWindow => {
browserWindow.rpc.on('hyper-init execute commands', ({ uid, terminal }) => {
if (commandSeparator === undefined) { commandSeparator = getSeparator(browserWindow, uid) }
if (clearCommand === undefined) { clearCommand = getClearCommand(browserWindow, uid) }

clearBuffer({ app: browserWindow, uid }, clearCommand)
Object.keys(init).map(key => {
let cmd = joinCommands(init[key].commands)
rulesHandler({ init, key, cmd, app, uid, terminal })
let cmd = joinCommands(init[key].commands, commandSeparator)
rulesHandler({ init, key, cmd, app: browserWindow, uid, terminal })
})
})
})
}

exports.onRendererWindow = app =>
waitFor(app, 'rpc', rpc =>
Expand Down
18 changes: 18 additions & 0 deletions src/get-specifics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Path = require('path')

const KNOWN_SHELLS = {
powershell: { separator: '; ', clearCommand: 'Clear-Host' },
node: { separator: '; ', clearCommand: 'console.clear();' },
cmd: { separator: ' & ', clearCommand: 'cls' },
fallback: { separator: ' && ', clearCommand: 'printf "\\033[H"' },
}

exports.getSeparator = (browserWindow, uid) => {
let shellName = Path.parse(browserWindow.sessions.get(uid).shell).name.toLowerCase()
return KNOWN_SHELLS[shellName].separator || KNOWN_SHELLS['fallback'].separator // Separator not found
}

exports.getClearCommand = (browserWindow, uid) => {
let shellName = Path.parse(browserWindow.sessions.get(uid).shell).name.toLowerCase()
return KNOWN_SHELLS[shellName].clearCommand || KNOWN_SHELLS['fallback'].clearCommand // Command not found
}
2 changes: 1 addition & 1 deletion src/join-commands.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports.joinCommands = commands => commands.join(' && ')
exports.joinCommands = (commands, commandSeparator = ' && ') => commands.join(commandSeparator)

0 comments on commit 4382437

Please sign in to comment.