Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Headless Terminal Multiplexer (htm) #2988

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const commands = {
},
'tab:new': focusedWindow => {
if (focusedWindow) {
focusedWindow.rpc.emit('termgroup add req');
focusedWindow.rpc.emit('termgroup add req', {});
} else {
setTimeout(app.createWindow, 0);
}
},
'pane:splitVertical': focusedWindow => {
focusedWindow && focusedWindow.rpc.emit('split request vertical');
focusedWindow && focusedWindow.rpc.emit('split request vertical', {});
},
'pane:splitHorizontal': focusedWindow => {
focusedWindow && focusedWindow.rpc.emit('split request horizontal');
focusedWindow && focusedWindow.rpc.emit('split request horizontal', {});
},
'pane:close': focusedWindow => {
focusedWindow && focusedWindow.rpc.emit('termgroup close req');
Expand Down
35 changes: 25 additions & 10 deletions app/ui/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {parse: parseUrl} = require('url');
const uuid = require('uuid');
const fileUriToPath = require('file-uri-to-path');
const isDev = require('electron-is-dev');

const updater = require('../updater');
const toElectronBackgroundColor = require('../utils/to-electron-background-color');
const {icon, cfgDir} = require('../config/paths');
Expand Down Expand Up @@ -66,7 +67,7 @@ module.exports = class Window {
// If no callback is passed to createWindow,
// a new session will be created by default.
if (!fn) {
fn = win => win.rpc.emit('termgroup add req');
fn = win => win.rpc.emit('termgroup add req', {});
}

// app.windowCallback is the createWindow callback
Expand Down Expand Up @@ -98,28 +99,28 @@ module.exports = class Window {
options
);

const initSession = (opts, fn_) => {
fn_(uuid.v4(), new Session(opts));
};

initSession(sessionOpts, (uid, session) => {
window.initSession(sessionOpts, (uid, session) => {
sessions.set(uid, session);
rpc.emit('session add', {
rows: sessionOpts.rows,
cols: sessionOpts.cols,
uid,
splitDirection: sessionOpts.splitDirection,
shell: session.shell,
pid: session.pty.pid
pid: typeof session.pty !== 'undefined' ? session.pty.pid : null,
termGroupUid: sessionOpts.termGroupUid,
activeUid: sessionOpts.activeUid
});

session.on('data', data => {
rpc.emit('session data', uid + data);
window.handleSessionData(uid, data, (innerUid, innerData) => {
rpc.emit('session data', {uid: innerUid, data: innerData});
});
});

session.on('exit', () => {
rpc.emit('session exit', {uid});
sessions.delete(uid);
window.deleteSession(uid);
});
});
});
Expand All @@ -146,6 +147,17 @@ module.exports = class Window {
session.resize({cols, rows});
});
rpc.on('data', ({uid, data, escaped}) => {
window.handleSessionInput(uid, data, escaped);
});
window.initSession = (opts, fn_) => {
fn_(uuid.v4(), new Session(opts));
};

window.handleSessionData = (uid, data, handleSessionCallback) => {
// By default, just execute the callback. Plugins can override.
return handleSessionCallback(uid, data);
};
window.handleSessionInput = (uid, data, escaped) => {
const session = sessions.get(uid);

if (escaped) {
Expand All @@ -157,7 +169,10 @@ module.exports = class Window {
} else {
session.write(data);
}
});
};
window.deleteSession = uid => {
sessions.delete(uid);
};
rpc.on('open external', ({url}) => {
shell.openExternal(url);
});
Expand Down
7 changes: 4 additions & 3 deletions lib/actions/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SESSION_SET_XTERM_TITLE
} from '../constants/sessions';

export function addSession({uid, shell, pid, cols, rows, splitDirection}) {
export function addSession({uid, shell, pid, cols, rows, splitDirection, termGroupUid, activeUid}) {
return (dispatch, getState) => {
const {sessions} = getState();
const now = Date.now();
Expand All @@ -30,8 +30,9 @@ export function addSession({uid, shell, pid, cols, rows, splitDirection}) {
cols,
rows,
splitDirection,
activeUid: sessions.activeUid,
now
activeUid: activeUid ? activeUid : sessions.activeUid,
now,
termGroupUid
});
};
}
Expand Down
14 changes: 9 additions & 5 deletions lib/actions/term-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import getRootGroups from '../selectors';
import {setActiveSession, ptyExitSession, userExitSession} from './sessions';

function requestSplit(direction) {
return () => (dispatch, getState) => {
return (sessionUid, sourceUid) => (dispatch, getState) => {
dispatch({
type: SESSION_REQUEST,
effect: () => {
const {ui} = getState();
const {ui, sessions} = getState();
rpc.emit('new', {
splitDirection: direction,
cwd: ui.cwd
cwd: ui.cwd,
sessionUid,
activeUid: sourceUid ? sourceUid : sessions.activeUid
});
}
});
Expand All @@ -37,7 +39,7 @@ export function resizeTermGroup(uid, sizes) {
};
}

export function requestTermGroup() {
export function requestTermGroup(termGroupUid, sessionUid) {
return (dispatch, getState) => {
dispatch({
type: TERM_GROUP_REQUEST,
Expand All @@ -48,7 +50,9 @@ export function requestTermGroup() {
isNewGroup: true,
cols,
rows,
cwd
cwd,
termGroupUid,
sessionUid
});
}
});
Expand Down
17 changes: 7 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ rpc.on('session add', data => {
store_.dispatch(sessionActions.addSession(data));
});

rpc.on('session data', d => {
// the uid is a uuid v4 so it's 36 chars long
const uid = d.slice(0, 36);
const data = d.slice(36);
rpc.on('session data', ({uid, data}) => {
store_.dispatch(sessionActions.addSessionData(uid, data));
});

Expand Down Expand Up @@ -103,16 +100,16 @@ rpc.on('session break req', () => {
store_.dispatch(sessionActions.sendSessionData(null, '\x03'));
});

rpc.on('termgroup add req', () => {
store_.dispatch(termGroupActions.requestTermGroup());
rpc.on('termgroup add req', ({termGroupUid, sessionUid}) => {
store_.dispatch(termGroupActions.requestTermGroup(termGroupUid, sessionUid));
});

rpc.on('split request horizontal', () => {
store_.dispatch(termGroupActions.requestHorizontalSplit());
rpc.on('split request horizontal', ({sessionUid, sourceUid}) => {
store_.dispatch(termGroupActions.requestHorizontalSplit(sessionUid, sourceUid));
});

rpc.on('split request vertical', () => {
store_.dispatch(termGroupActions.requestVerticalSplit());
rpc.on('split request vertical', ({sessionUid, sourceUid}) => {
store_.dispatch(termGroupActions.requestVerticalSplit(sessionUid, sourceUid));
});

rpc.on('reset fontSize req', () => {
Expand Down
5 changes: 4 additions & 1 deletion lib/reducers/term-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ const reducer = (state = initialState, action) => {
return setActiveGroup(state, action);
}

const uid = uuid.v4();
let uid = uuid.v4();
if (action.termGroupUid) {
uid = action.termGroupUid;
}
const termGroup = TermGroup({
uid,
sessionUid: action.uid
Expand Down