Skip to content

Commit

Permalink
Update coding styles
Browse files Browse the repository at this point in the history
  • Loading branch information
daltonmenezes committed Aug 24, 2020
1 parent edf5d7f commit 50b64d3
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 89 deletions.
64 changes: 37 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'use strict'

const { rulesHandler } = require('./src/rules/rules-handler')
const { clearBuffer } = require('./src/clear-buffer')
Expand All @@ -9,45 +9,55 @@ 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)
clearCommand = config.getConfig().clearCommand || undefined
commandSeparator = config.getConfig().commandSeparator || undefined

Object.assign(init, config.getConfig().init)
}

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

exports.reduceTermGroups = reducer =>
Object.assign(terminal, reducer.termGroups) && reducer
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 })
exports.middleware = () => (next) => (action) => {
if (action.type === 'SESSION_ADD') {
Object.assign(terminal, { splitDirection: action.splitDirection })
}

next(action)
next(action)
}

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) }
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, commandSeparator)

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

exports.onRendererWindow = app =>
waitFor(app, 'rpc', rpc =>
rpc.on('session add', ({ uid }) =>
rpc.emit('hyper-init execute commands', { uid, terminal })
)
)
exports.onRendererWindow = (app) =>
waitFor(app, 'rpc', (rpc) =>
rpc.on('session add', ({ uid }) =>
rpc.emit('hyper-init execute commands', { uid, terminal })
)
)
4 changes: 2 additions & 2 deletions src/clear-buffer.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exports.clearBuffer = ({ app, uid }, cmd = 'printf "\\033[H"') =>
app.sessions.get(uid).write(`${cmd} \r`)
exports.clearBuffer = ({ app, uid }, cmd = 'printf "\\033[H"') =>
app.sessions.get(uid).write(`${cmd} \r`)
28 changes: 18 additions & 10 deletions src/get-specifics.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
const Path = require('path')

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

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

return KNOWN_SHELLS[shellName]
? 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() || null
return KNOWN_SHELLS[shellName] ? KNOWN_SHELLS[shellName].clearCommand : KNOWN_SHELLS['fallback'].clearCommand // Command not found
}
let shellName =
Path.parse(browserWindow.sessions.get(uid).shell).name.toLowerCase() || null

return KNOWN_SHELLS[shellName]
? KNOWN_SHELLS[shellName].clearCommand
: KNOWN_SHELLS['fallback'].clearCommand // Command not found
}
6 changes: 3 additions & 3 deletions src/is/is-new-window.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { numberOfWindows } = require('../number-of-windows')

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

exports.isOnce = terminal =>
numberOfWindows() <= 2 &&
Object.keys(terminal).length <= 2
exports.isOnce = (terminal) =>
numberOfWindows() <= 2 && Object.keys(terminal).length <= 2
2 changes: 1 addition & 1 deletion src/is/is-rule.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const rules = require('../rules/index')

exports.isRule = rule => rule in rules
exports.isRule = (rule) => rule in rules
9 changes: 4 additions & 5 deletions src/is/is-tab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
exports.isTab = terminal =>
terminal.uid ?
!terminal[terminal.uid].direction &&
Object.keys(terminal.tabs).length > 1 :
''
exports.isTab = (terminal) =>
terminal.uid
? !terminal[terminal.uid].direction && Object.keys(terminal.tabs).length > 1
: ''
3 changes: 2 additions & 1 deletion src/join-commands.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exports.joinCommands = (commands, commandSeparator = ' && ') => commands.join(commandSeparator)
exports.joinCommands = (commands, commandSeparator = ' && ') =>
commands.join(commandSeparator)
3 changes: 1 addition & 2 deletions src/number-of-windows.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { BrowserWindow } = require('electron')

exports.numberOfWindows = () =>
BrowserWindow.getAllWindows().length
exports.numberOfWindows = () => BrowserWindow.getAllWindows().length
12 changes: 6 additions & 6 deletions src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const { onTabsRule } = require('./on-tabs-rule')
const { onAllRule } = require('./on-all-rule')

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

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

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

exports.onceRule = ({ app, uid, terminal, cmd }) =>
isOnce(terminal) ?
app.sessions.get(uid).write(`${cmd}\r`) :
''
isOnce(terminal) ? app.sessions.get(uid).write(`${cmd}\r`) : ''
18 changes: 8 additions & 10 deletions src/rules/rules-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ const rules = require('./index')
const { isRule } = require('../is/is-rule')

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

Array.isArray(rule)
? rule.map(rule => isRule(rule) ? rules[rule](props) : '')
: ''

isRule(rule)
? rules[rule](props)
: ''
}
const rule = props.init[props.key].rule

if (Array.isArray(rule)) {
return rule.map((rule) => (isRule(rule) ? rules[rule](props) : ''))
}

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

0 comments on commit 50b64d3

Please sign in to comment.