diff --git a/index.js b/index.js index 18dfdb9..745fb31 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,14 @@ const nextMode = mode => { } } +// Matchers for retrieving config JSON from terminal +// Remove ANSI escape code sequences. Visualization: https://goo.gl/IY8vuU +const ansiEscapeCodes = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/gm +// Match hyperlayout config string and ignore lints. Visualization: https://goo.gl/afYmAO +const configMatcher = /(?:.|\s)*\[hyperlayout config]:((?:.|\s)*})(?:.|\s)*/gm +// Cross-os new-line characters. Visualization: https://goo.gl/q501uy +const newlines = /[\n\r]/gm + // Generate Command queue from converted Config function generateQueue(converted, mode = 'TAB', initial) { mode = (mode === 'PANE') ? 'HORIZONTAL' : mode @@ -151,15 +159,16 @@ function focusUid({dispatch}, uid) { // Listens for cli commands and sessions exports.middleware = store => next => action => { - const {type, data} = action + let {type, data} = action const {sessions} = store.getState() const {activeUid} = sessions // Check for hyperlayout config if (type === 'SESSION_ADD_DATA') { - const testedData = /^\[hyperlayout config]:(.*)/.exec(data) + data = data.replace(ansiEscapeCodes, '') + const testedData = configMatcher.exec(data.trim()) if (testedData && testedData[1]) { - const config = JSON.parse(testedData[1]) + const config = JSON.parse(testedData[1].replace(newlines, '')) hyperlayout = new Hyperlayout(config, store) return }