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

Improve tabs title #892

Merged
merged 10 commits into from
Oct 22, 2016
29 changes: 0 additions & 29 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,42 +195,13 @@ app.on('ready', () => installDevExtensions(isDev).then(() => {
rpc.emit('session data', {uid, data});
});

session.on('title', title => {
win.setTitle(title);
rpc.emit('session title', {uid, title});
});

session.on('exit', () => {
rpc.emit('session exit', {uid});
sessions.delete(uid);
});
});
});

// TODO: this goes away when we are able to poll
// for the title ourselves, instead of relying
// on Session and focus/blur to subscribe
rpc.on('focus', ({uid}) => {
const session = sessions.get(uid);
if (typeof session !== 'undefined' && typeof session.lastTitle !== 'undefined') {
win.setTitle(session.lastTitle);
}
if (session) {
session.focus();
} else {
console.log('session not found by', uid);
}
});
rpc.on('blur', ({uid}) => {
const session = sessions.get(uid);

if (session) {
session.blur();
} else {
console.log('session not found by', uid);
}
});

rpc.on('exit', ({uid}) => {
const session = sessions.get(uid);
if (session) {
Expand Down
58 changes: 0 additions & 58 deletions app/session.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const {exec} = require('child_process');
const {EventEmitter} = require('events');
const {StringDecoder} = require('string_decoder');

Expand All @@ -21,8 +20,6 @@ try {
);
}

const TITLE_POLL_INTERVAL = 500;

const envFromConfig = config.getConfig().env || {};

module.exports = class Session extends EventEmitter {
Expand Down Expand Up @@ -62,60 +59,6 @@ module.exports = class Session extends EventEmitter {
});

this.shell = shell || defaultShell;
this.getTitle();
}

focus() {
this.subscribed = true;
this.getTitle();
}

blur() {
this.subscribed = false;
clearTimeout(this.titlePoll);
}

getTitle() {
if (process.platform === 'win32') {
return;
}

if (this.fetching) {
return;
}
this.fetching = true;

let tty = this.pty.stdout.ttyname;
tty = tty.replace(/^\/dev\/tty/, '');

// try to exclude grep from the results
// by grepping for `[s]001` instead of `s001`
tty = `[${tty[0]}]${tty.substr(1)}`;

// TODO: limit the concurrency of how many processes we run?
// TODO: only tested on mac
exec(`ps uxac | grep ${tty} | head -n 1`, (err, out) => {
this.fetching = false;
if (this.ended) {
return;
}
if (err) {
return;
}
let title = out.split(' ').pop();
if (title) {
title = title.replace(/^\(/, '');
title = title.replace(/\)?\n$/, '');
if (title !== this.lastTitle) {
this.emit('title', title);
this.lastTitle = title;
}
}

if (this.subscribed) {
this.titlePoll = setTimeout(() => this.getTitle(), TITLE_POLL_INTERVAL);
}
});
}

exit() {
Expand All @@ -142,7 +85,6 @@ module.exports = class Session extends EventEmitter {
}
this.emit('exit');
this.ended = true;
this.blur();
}

};
11 changes: 1 addition & 10 deletions lib/actions/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
SESSION_USER_DATA,
SESSION_URL_SET,
SESSION_URL_UNSET,
SESSION_SET_XTERM_TITLE,
SESSION_SET_PROCESS_TITLE
SESSION_SET_XTERM_TITLE
} from '../constants/sessions';

export function addSession({uid, shell, pid, cols, rows, splitDirection}) {
Expand Down Expand Up @@ -127,14 +126,6 @@ export function clearActiveSession() {
};
}

export function setSessionProcessTitle(uid, title) {
return {
type: SESSION_SET_PROCESS_TITLE,
uid,
title
};
}

export function setSessionXtermTitle(uid, title) {
return {
type: SESSION_SET_XTERM_TITLE,
Expand Down
4 changes: 4 additions & 0 deletions lib/components/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export default class Term extends Component {
this.term.modifierKeys = props.modifierKeys;
// this.term.CursorNode_ is available at this point.
this.term.setCursorShape(props.cursorShape);

// emit onTitle event when hterm instance
// wants to set the title of its tab
this.term.setWindowTitle = props.onTitle;
};
this.term.decorate(this.termRef);
this.term.installKeyboard();
Expand Down
1 change: 0 additions & 1 deletion lib/constants/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ export const SESSION_USER_DATA = 'SESSION_USER_DATA';
export const SESSION_URL_SET = 'SESSION_URL_SET';
export const SESSION_URL_UNSET = 'SESSION_URL_UNSET';
export const SESSION_SET_XTERM_TITLE = 'SESSION_SET_XTERM_TITLE';
export const SESSION_SET_PROCESS_TITLE = 'SESSION_SET_PROCESS_TITLE';
export const SESSION_SET_CWD = 'SESSION_SET_CWD';
4 changes: 0 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ rpc.on('session data send', ({uid, data}) => {
store_.dispatch(sessionActions.sendSessionData(uid, data));
});

rpc.on('session title', ({uid, title}) => {
store_.dispatch(sessionActions.setSessionProcessTitle(uid, title));
});

rpc.on('session exit', ({uid}) => {
store_.dispatch(termGroupActions.ptyExitTermGroup(uid));
});
Expand Down
4 changes: 1 addition & 3 deletions lib/reducers/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
SESSION_URL_SET,
SESSION_URL_UNSET,
SESSION_RESIZE,
SESSION_SET_XTERM_TITLE,
SESSION_SET_PROCESS_TITLE
SESSION_SET_XTERM_TITLE
} from '../constants/sessions';

const initialState = Immutable({
Expand Down Expand Up @@ -94,7 +93,6 @@ const reducer = (state = initialState, action) => {
return deleteSession(state, action.uid);

case SESSION_SET_XTERM_TITLE:
case SESSION_SET_PROCESS_TITLE:
return state.setIn(['sessions', action.uid, 'title'], action.title);

case SESSION_RESIZE:
Expand Down