Skip to content

Commit 4382437

Browse files
Merge pull request #4 from Nightmarlin/master
Add inference of clearCommand and separator
2 parents dc70db4 + 9983094 commit 4382437

File tree

7 files changed

+118
-40
lines changed

7 files changed

+118
-40
lines changed

README.md

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<p align="center">The ultimate and most complete extension to initialize commands before and after <a href="https://hyper.is/">Hyper terminal</a> starts
66
<br/><br/>
7-
<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>
7+
<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>
88
<a href="https://paypal.me/daltonmenezes"><img src="https://img.shields.io/badge/Donate-green.svg" alt="Donate" /></a>
99
<a href="https://www.npmjs.com/package/hyper-init"><img src="https://img.shields.io/npm/v/hyper-init.svg" alt="npm version"/></a>
1010
<img src="https://img.shields.io/npm/dm/hyper-init.svg?label=Downloads" alt="downloads" />
@@ -14,24 +14,27 @@
1414
</a>
1515
</p>
1616

17-
> 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.
17+
> 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.
1818
1919
<p align="center"><img src="https://github.com/daltonmenezes/hyper-init/blob/master/img/hyper-init.gif?raw=true" alt="hyper-init gif"/></p>
2020

2121
## Table of Contents
2222

23+
- [Table of Contents](#table-of-contents)
2324
- [Installation](#installation)
2425
- [Configuration](#configuration)
2526
- [init](#init)
27+
- [Rules](#rules)
28+
- [Commands](#commands)
2629
- [clearCommand](#clearcommand)
27-
- [init Options](#init-options)
28-
- [Rules](#rules)
29-
- [Commands](#commands)
30+
- [commandSeparator](#commandseparator)
31+
- [Contributing](#contributing)
32+
- [License](#license)
3033

3134

3235
## Installation
3336

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

3639
So, type the following on Hyper:
3740

@@ -41,11 +44,11 @@ hyper i hyper-init
4144

4245
## Configuration
4346

44-
## init
47+
### init
4548

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

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

5053
```js
5154
init: [
@@ -56,11 +59,11 @@ init: [
5659
]
5760
```
5861

59-
Your ```~/.hyper.js``` configuration file should look like this:
62+
Your `~/.hyper.js` configuration file should look like this:
6063
```js
6164
module.exports = {
6265
config: {
63-
66+
6467
// add hyper-init configuration like this:
6568
init: [
6669
{
@@ -71,46 +74,95 @@ module.exports = {
7174
rule: 'windows',
7275
commands: ['echo This is only executed on New Windows!']
7376
}
74-
]
77+
]
7578
},
7679

7780
plugins: ['hyper-init']
7881

7982
}
8083
```
8184

82-
## clearCommand
85+
#### Rules
86+
A string that defines when you want your commands to run.
87+
88+
Rule | Description
89+
--- | ---
90+
`once` | executes your commands only at Hyper starts
91+
`windows` | executes your commands only when a new Hyper window opens
92+
`tabs` | executes your commands only when a new tab is opened
93+
`splitted` | executes your commands only when a new pane is opened
94+
`all` | executes your commands every time a terminal opens
95+
96+
#### Commands
97+
An array with your shell commands to run.<br/>
98+
You can perform as many commands as you would like.
99+
100+
Example:
101+
```js
102+
commands: ['cd ~/Desktop', 'ls']
103+
```
104+
83105

84-
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:
106+
### clearCommand
107+
108+
`hyper-init` can infer the command to clear the screen for a small number of terminals.
109+
If it can't infer the command, `hyper-init` clears the terminal buffer using `printf "\\033[H"`.
110+
You can set it manually adding the `clearCommand: ''` property within the `config` object.
111+
For example:
85112

86113
```js
87114
module.exports = {
88-
config: {
89-
clearCommand: 'reset'
90-
}
115+
config: {
116+
clearCommand: 'reset'
117+
}
91118
}
92119
```
93120

94-
## init: Options
121+
### commandSeparator
95122

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

99-
Rule | Description
100-
--- | ---
101-
`once` | executes your commands only at Hyper starts
102-
`windows` | executes your commands only at new windows
103-
`tabs` | executes your commands only at new tabs
104-
`splitted` | executes your commands only at splitted windows
105-
`all` | executes your commands with all described states previously
129+
```js
130+
module.exports = {
131+
config: {
132+
commandSeparator: ' ++ ' // For an arbitrary terminal that uses `++`
133+
}
134+
}
135+
```
106136

107-
### Commands
108-
An array with your shell commands to run.<br/>
109-
You can perform as many commands as you would like to do.
137+
## Contributing
138+
139+
`hyper-init`'s ability to infer the `clearCommand` and `commandSeparator` is based on its relatively small dictionary.
140+
Feel free to add more definitions for terminals not listed in `get-specifics.js`.
110141

111-
Example:
112142
```js
113-
commands: ['cd ~/Desktop', 'ls']
143+
KNOWN_SHELLS = {
144+
[...]
145+
shellName: {
146+
separator: '',
147+
clearCommand: ''
148+
}
149+
[...]
150+
}
151+
```
152+
153+
- `shellName` should be replaced with the name of the shell you want to target (lowercase)
154+
- The value of `separator` should be the separator for multiple statements on one line (IE `' && '`) as a string
155+
- The value of `clearCommand` should be the command to clear the target shell (IE `'cls'`) as a string
156+
157+
```js
158+
KNOWN_SHELLS = {
159+
[...]
160+
powershell: {
161+
separator: '; ',
162+
clearCommand: 'Clear-Host'
163+
}
164+
[...]
165+
}
114166
```
115167

116168
## License

img/hyper_test_cmd.png

8.11 KB
Loading

img/hyper_test_node.png

14.2 KB
Loading

img/hyper_test_powershell.png

10.5 KB
Loading

index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
const { rulesHandler } = require('./src/rules/rules-handler')
44
const { clearBuffer } = require('./src/clear-buffer')
55
const { joinCommands } = require('./src/join-commands')
6+
const { getSeparator } = require('./src/get-specifics')
7+
const { getClearCommand } = require('./src/get-specifics')
68
const waitFor = require('./src/wait-for')
79

810
const init = {}
911
const terminal = {}
1012
let clearCommand
13+
let commandSeparator
1114

1215
exports.onApp = ({ config }) => {
1316
clearCommand = config.getConfig().clearCommand || undefined
17+
commandSeparator = config.getConfig().commandSeparator || undefined
1418
Object.assign(init, config.getConfig().init)
1519
}
1620

@@ -24,18 +28,22 @@ exports.reduceTermGroups = reducer =>
2428
exports.middleware = store => next => action => {
2529
if (action.type === 'SESSION_ADD')
2630
Object.assign(terminal, { splitDirection: action.splitDirection })
27-
31+
2832
next(action)
2933
}
3034

31-
exports.onWindow = app =>
32-
app.rpc.on('hyper-init execute commands', ({ uid, terminal }) => {
33-
clearBuffer({ app, uid }, clearCommand)
35+
exports.onWindow = browserWindow => {
36+
browserWindow.rpc.on('hyper-init execute commands', ({ uid, terminal }) => {
37+
if (commandSeparator === undefined) { commandSeparator = getSeparator(browserWindow, uid) }
38+
if (clearCommand === undefined) { clearCommand = getClearCommand(browserWindow, uid) }
39+
40+
clearBuffer({ app: browserWindow, uid }, clearCommand)
3441
Object.keys(init).map(key => {
35-
let cmd = joinCommands(init[key].commands)
36-
rulesHandler({ init, key, cmd, app, uid, terminal })
42+
let cmd = joinCommands(init[key].commands, commandSeparator)
43+
rulesHandler({ init, key, cmd, app: browserWindow, uid, terminal })
3744
})
38-
})
45+
})
46+
}
3947

4048
exports.onRendererWindow = app =>
4149
waitFor(app, 'rpc', rpc =>

src/get-specifics.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Path = require('path')
2+
3+
const KNOWN_SHELLS = {
4+
powershell: { separator: '; ', clearCommand: 'Clear-Host' },
5+
node: { separator: '; ', clearCommand: 'console.clear();' },
6+
cmd: { separator: ' & ', clearCommand: 'cls' },
7+
fallback: { separator: ' && ', clearCommand: 'printf "\\033[H"' },
8+
}
9+
10+
exports.getSeparator = (browserWindow, uid) => {
11+
let shellName = Path.parse(browserWindow.sessions.get(uid).shell).name.toLowerCase()
12+
return KNOWN_SHELLS[shellName].separator || KNOWN_SHELLS['fallback'].separator // Separator not found
13+
}
14+
15+
exports.getClearCommand = (browserWindow, uid) => {
16+
let shellName = Path.parse(browserWindow.sessions.get(uid).shell).name.toLowerCase()
17+
return KNOWN_SHELLS[shellName].clearCommand || KNOWN_SHELLS['fallback'].clearCommand // Command not found
18+
}

src/join-commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
exports.joinCommands = commands => commands.join(' && ')
1+
exports.joinCommands = (commands, commandSeparator = ' && ') => commands.join(commandSeparator)

0 commit comments

Comments
 (0)