Skip to content

Commit

Permalink
Add hyper-init extension
Browse files Browse the repository at this point in the history
  • Loading branch information
daltonmenezes committed Oct 23, 2018
1 parent 032d7ea commit 4c87df3
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 0 deletions.
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const { rulesHandler } = require('./src/rules/rules-handler')
const waitFor = require('./src/wait-for')

const init = {}
const terminal = {}

exports.onApp = ({ config }) =>
Object.assign(init, config.getConfig().init)

exports.getTabProps = (uid, parentProps, props) =>
Object.assign(terminal, uid, { tabs: parentProps.tabs }) &&
Object.assign({}, props)

exports.reduceTermGroups = reducer =>
Object.assign(terminal, reducer.termGroups) && 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('execute commands', ({ uid, terminal }) =>
Object.keys(init).map(key =>
init[key].commands.map(cmd =>
rulesHandler({ init, key, cmd, app, uid, terminal }))
)
)

exports.onRendererWindow = app =>
waitFor(app, 'rpc', rpc =>
rpc.on('session add', ({ uid }) =>
rpc.emit('execute commands', { uid, terminal })
)
)
6 changes: 6 additions & 0 deletions src/is/is-new-window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { numberOfWindows } = require('../number-of-windows')

exports.isNewWindow = terminal =>
!terminal.splitDirection &&
Object.keys(terminal).length <= 2 &&
numberOfWindows() > 2
5 changes: 5 additions & 0 deletions src/is/is-once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { numberOfWindows } = require('../number-of-windows')

exports.isOnce = terminal =>
numberOfWindows() <= 2 &&
Object.keys(terminal).length <= 2
5 changes: 5 additions & 0 deletions src/is/is-tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.isTab = terminal =>
terminal.uid ?
!terminal[terminal.uid].direction &&
Object.keys(terminal.tabs).length > 1 :
''
4 changes: 4 additions & 0 deletions src/number-of-windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { BrowserWindow } = require('electron')

exports.numberOfWindows = () =>
BrowserWindow.getAllWindows().length
13 changes: 13 additions & 0 deletions src/rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { onceRule } = require('./once-rule')
const { onNewWindowRule } = require('./on-new-window-rule')
const { onSplittedWindowsRule } = require('./on-splitted-windows-rule')
const { onTabsRule } = require('./on-tabs-rule')
const { onAllRule } = require('./on-all-rule')

module.exports = {
'once': onceRule,
'windows': onNewWindowRule,
'splitted': onSplittedWindowsRule,
'tabs': onTabsRule,
'all': onAllRule
}
2 changes: 2 additions & 0 deletions src/rules/on-all-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.onAllRule = ({ app, uid, cmd }) =>
app.sessions.get(uid).write(`${cmd}\r`)
6 changes: 6 additions & 0 deletions src/rules/on-new-window-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { isNewWindow } = require('../is/is-new-window')

exports.onNewWindowRule = ({ app, uid, terminal, cmd }) =>
isNewWindow(terminal) ?
app.sessions.get(uid).write(`${cmd}\r`) :
''
4 changes: 4 additions & 0 deletions src/rules/on-splitted-windows-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.onSplittedWindowsRule = ({ app, uid, cmd, terminal }) =>
!!terminal.splitDirection ?
app.sessions.get(uid).write(`${cmd}\r`) :
''
7 changes: 7 additions & 0 deletions src/rules/on-tabs-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { isTab } = require('../is/is-tab')

exports.onTabsRule = ({ app, uid, terminal, cmd }) =>
isTab(terminal) &&
!terminal.splitDirection ?
app.sessions.get(uid).write(`${cmd}\r`) :
''
6 changes: 6 additions & 0 deletions src/rules/once-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { isOnce } = require('../is/is-once')

exports.onceRule = ({ app, uid, terminal, cmd }) =>
isOnce(terminal) ?
app.sessions.get(uid).write(`${cmd}\r`) :
''
8 changes: 8 additions & 0 deletions src/rules/rules-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const rules = require('./index')

exports.rulesHandler = (...[props]) => {
const rule = props.init[props.key].rule

if (rule in rules)
rules[rule](props)
}
4 changes: 4 additions & 0 deletions src/wait-for.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = waitFor = (object, key, fn) =>
key in object ?
fn(object[key]) :
setTimeout(() => waitFor(object, key, fn), 10)

0 comments on commit 4c87df3

Please sign in to comment.