Skip to content

Commit

Permalink
Initial work on microsoft#10023.
Browse files Browse the repository at this point in the history
Modified terminalInstance to expose setTitle and fire the onTitleChanged event when it's called. Added workbench.action.terminal.rename contribution from the workbench and the action that fires when it's run.
  • Loading branch information
jammerware committed May 31, 2017
1 parent e6217c6 commit d7757bd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/vs/workbench/parts/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,9 @@ export interface ITerminalInstance {
* Experimental: Call to enable onData to be passed over IPC to the extension host.
*/
enableApiOnData(): void;

/**
* Sets the title of the terminal instance.
*/
setTitle(title: string): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TERMINAL_DEFAULT_SHELL_LINUX, TERMINAL_DEFAULT_SHELL_OSX, TERMINAL_DEFA
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { KillTerminalAction, CopyTerminalSelectionAction, CreateNewTerminalAction, FocusActiveTerminalAction, FocusNextTerminalAction, FocusPreviousTerminalAction, FocusTerminalAtIndexAction, SelectDefaultShellWindowsTerminalAction, RunSelectedTextInTerminalAction, RunActiveFileInTerminalAction, ScrollDownTerminalAction, ScrollDownPageTerminalAction, ScrollToBottomTerminalAction, ScrollUpTerminalAction, ScrollUpPageTerminalAction, ScrollToTopTerminalAction, TerminalPasteAction, ToggleTerminalAction, ClearTerminalAction, AllowWorkspaceShellTerminalCommand, DisallowWorkspaceShellTerminalCommand } from 'vs/workbench/parts/terminal/electron-browser/terminalActions';
import { KillTerminalAction, CopyTerminalSelectionAction, CreateNewTerminalAction, FocusActiveTerminalAction, FocusNextTerminalAction, FocusPreviousTerminalAction, FocusTerminalAtIndexAction, SelectDefaultShellWindowsTerminalAction, RunSelectedTextInTerminalAction, RunActiveFileInTerminalAction, ScrollDownTerminalAction, ScrollDownPageTerminalAction, ScrollToBottomTerminalAction, ScrollUpTerminalAction, ScrollUpPageTerminalAction, ScrollToTopTerminalAction, TerminalPasteAction, ToggleTerminalAction, ClearTerminalAction, AllowWorkspaceShellTerminalCommand, DisallowWorkspaceShellTerminalCommand, RenameTerminalAction } from 'vs/workbench/parts/terminal/electron-browser/terminalActions';
import { Registry } from 'vs/platform/platform';
import { ShowAllCommandsAction } from 'vs/workbench/parts/quickopen/browser/commandsHandler';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
Expand Down Expand Up @@ -268,5 +268,6 @@ if (platform.isWindows) {
}
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(AllowWorkspaceShellTerminalCommand, AllowWorkspaceShellTerminalCommand.ID, AllowWorkspaceShellTerminalCommand.LABEL), 'Terminal: Allow Workspace Shell Configuration', category);
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(DisallowWorkspaceShellTerminalCommand, DisallowWorkspaceShellTerminalCommand.ID, DisallowWorkspaceShellTerminalCommand.LABEL), 'Terminal: Disallow Workspace Shell Configuration', category);
actionRegistry.registerWorkbenchAction(new SyncActionDescriptor(RenameTerminalAction, RenameTerminalAction.ID, RenameTerminalAction.LABEL), 'Terminal: Rename', category);

registerColors();
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
import { attachSelectBoxStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';

export class ToggleTerminalAction extends TogglePanelAction {

Expand Down Expand Up @@ -533,3 +534,32 @@ export class DisallowWorkspaceShellTerminalCommand extends Action {
return TPromise.as(void 0);
}
}

export class RenameTerminalAction extends Action {

public static ID = 'workbench.action.terminal.rename';
public static LABEL = nls.localize('workbench.action.terminal.rename', "Rename");

constructor(
id: string, label: string,
@IQuickOpenService private quickOpenService: IQuickOpenService,
@ITerminalService private terminalService: ITerminalService
) {
super(id, label);
}

public run(): TPromise<any> {
const terminalInstance = this.terminalService.getActiveInstance();
if (!terminalInstance) {
return TPromise.as(void 0);
}
return this.quickOpenService.input({
prompt: nls.localize('workbench.action.terminal.rename.prompt', "Enter terminal name"),
}).then(name => {
if (name) {
terminalInstance.setTitle(name);
}
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,13 @@ export class TerminalInstance implements ITerminalInstance {
});
if (!shell.name) {
// Only listen for process title changes when a name is not provided
this._process.on('message', (message) => {
if (message.type === 'title') {
this._title = message.content ? message.content : '';
this._onTitleChanged.fire(this._title);
}
});
this._process.on('message', this._onPtyProcessMessageTitleChanged);
// this._process.on('message', (message) => {
// if (message.type === 'title') {
// this._title = message.content ? message.content : '';
// this._onTitleChanged.fire(this._title);
// }
// });
}
this._process.on('message', (message) => {
if (message.type === 'pid') {
Expand Down Expand Up @@ -561,6 +562,13 @@ export class TerminalInstance implements ITerminalInstance {
}
}

private _onPtyProcessMessageTitleChanged(message: any): void {
if (message.type === 'title') {
this._title = message.content ? message.content : '';
this._onTitleChanged.fire(this._title);
}
}

private _attachPressAnyKeyToCloseListener() {
this._processDisposables.push(dom.addDisposableListener(this._xterm.textarea, 'keypress', (event: KeyboardEvent) => {
this.dispose();
Expand Down Expand Up @@ -761,6 +769,19 @@ export class TerminalInstance implements ITerminalInstance {
public static setTerminalProcessFactory(factory: ITerminalProcessFactory): void {
this._terminalProcessFactory = factory;
}

public setTitle(title: string): void {
const oldTitle = this._title;
if (title !== oldTitle) {
this._title = title;
this._onTitleChanged.fire(title);
}

// if the title is set via API, unregister the handler that automatically updates the terminal name
if (this._process) {
this._process.removeListener('message', this._onPtyProcessMessageTitleChanged);
}
}
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
Expand Down

0 comments on commit d7757bd

Please sign in to comment.