From ca9f11484c0b400313a42f181aeefe7db81560ff Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Tue, 13 Jul 2021 21:47:06 +0200 Subject: [PATCH] move terminal-tab to use .profile --- tabby-core/src/api/profileProvider.ts | 2 +- .../src/components/terminalTab.component.ts | 27 +++++++++++------- tabby-local/src/profiles.ts | 28 +++++++++++++++---- tabby-local/src/recoveryProvider.ts | 13 +++++---- tabby-local/src/services/terminal.service.ts | 13 +-------- tabby-local/src/session.ts | 2 -- tabby-local/src/tabContextMenu.ts | 28 +++++++++++++------ tabby-terminal/src/session.ts | 1 - 8 files changed, 69 insertions(+), 45 deletions(-) diff --git a/tabby-core/src/api/profileProvider.ts b/tabby-core/src/api/profileProvider.ts index f8327ae8..2867669e 100644 --- a/tabby-core/src/api/profileProvider.ts +++ b/tabby-core/src/api/profileProvider.ts @@ -8,7 +8,7 @@ export interface Profile { type: string name: string group?: string - options?: Record + options: Record icon?: string color?: string diff --git a/tabby-local/src/components/terminalTab.component.ts b/tabby-local/src/components/terminalTab.component.ts index 9fe60427..41b414d5 100644 --- a/tabby-local/src/components/terminalTab.component.ts +++ b/tabby-local/src/components/terminalTab.component.ts @@ -1,9 +1,10 @@ import { Component, Input, Injector } from '@angular/core' import { BaseTabProcess, WIN_BUILD_CONPTY_SUPPORTED, isWindowsBuild } from 'tabby-core' import { BaseTerminalTabComponent } from 'tabby-terminal' -import { SessionOptions } from '../api' +import { LocalProfile } from '../api' import { Session } from '../session' import { UACService } from '../services/uac.service' +import { SessionOptions } from 'http2' /** @hidden */ @Component({ @@ -13,7 +14,8 @@ import { UACService } from '../services/uac.service' animations: BaseTerminalTabComponent.animations, }) export class TerminalTabComponent extends BaseTerminalTabComponent { - @Input() sessionOptions: SessionOptions + @Input() sessionOptions: SessionOptions // Deprecated + @Input() profile: LocalProfile session: Session|null = null // eslint-disable-next-line @typescript-eslint/no-useless-constructor @@ -25,6 +27,8 @@ export class TerminalTabComponent extends BaseTerminalTabComponent { } ngOnInit (): void { + this.sessionOptions = this.profile.options + this.logger = this.log.create('terminalTab') this.session = new Session(this.injector) @@ -49,17 +53,17 @@ export class TerminalTabComponent extends BaseTerminalTabComponent { protected onFrontendReady (): void { this.initializeSession(this.size.columns, this.size.rows) - this.savedStateIsLive = this.sessionOptions.restoreFromPTYID === this.session?.getPTYID() + this.savedStateIsLive = this.profile.options.restoreFromPTYID === this.session?.getPTYID() super.onFrontendReady() } initializeSession (columns: number, rows: number): void { - if (this.sessionOptions.runAsAdministrator && this.uac.isAvailable) { - this.sessionOptions = this.uac.patchSessionOptionsForUAC(this.sessionOptions) + if (this.profile.options.runAsAdministrator && this.uac.isAvailable) { + this.profile.options = this.uac.patchSessionOptionsForUAC(this.profile.options) } this.session!.start({ - ...this.sessionOptions, + ...this.profile.options, width: columns, height: rows, }) @@ -72,10 +76,13 @@ export class TerminalTabComponent extends BaseTerminalTabComponent { const cwd = this.session ? await this.session.getWorkingDirectory() : null return { type: 'app:terminal-tab', - sessionOptions: { - ...this.sessionOptions, - cwd: cwd ?? this.sessionOptions.cwd, - restoreFromPTYID: this.session?.getPTYID(), + profile: { + ...this.profile, + options: { + ...this.profile.options, + cwd: cwd ?? this.profile.options.cwd, + restoreFromPTYID: this.session?.getPTYID(), + }, }, savedState: this.frontend?.saveState(), } diff --git a/tabby-local/src/profiles.ts b/tabby-local/src/profiles.ts index 3111fcdd..a4738e77 100644 --- a/tabby-local/src/profiles.ts +++ b/tabby-local/src/profiles.ts @@ -1,3 +1,4 @@ +import deepClone from 'clone-deep' import { Injectable, Inject } from '@angular/core' import { ProfileProvider, Profile, NewTabParameters, ConfigService, SplitTabComponent, AppService } from 'tabby-core' import { TerminalTabComponent } from './components/terminalTab.component' @@ -9,6 +10,21 @@ export class LocalProfilesService extends ProfileProvider { id = 'local' name = 'Local' settingsComponent = LocalProfileSettingsComponent + configDefaults = { + options: { + restoreFromPTYID: null, + command: '', + args: [], + cwd: null, + env: { + __nonStructural: true, + }, + width: null, + height: null, + pauseAfterExit: false, + runAsAdministrator: false, + }, + } constructor ( private app: AppService, @@ -30,17 +46,19 @@ export class LocalProfilesService extends ProfileProvider { } async getNewTabParameters (profile: Profile): Promise> { - const options = { ...profile.options } + profile = deepClone(profile) - if (!options.cwd) { + if (!profile.options?.cwd) { if (this.app.activeTab instanceof TerminalTabComponent && this.app.activeTab.session) { - options.cwd = await this.app.activeTab.session.getWorkingDirectory() + profile.options ??= {} + profile.options.cwd = await this.app.activeTab.session.getWorkingDirectory() } if (this.app.activeTab instanceof SplitTabComponent) { const focusedTab = this.app.activeTab.getFocusedTab() if (focusedTab instanceof TerminalTabComponent && focusedTab.session) { - options.cwd = await focusedTab.session.getWorkingDirectory() + profile.options ??= {} + profile.options.cwd = await focusedTab.session.getWorkingDirectory() } } } @@ -48,7 +66,7 @@ export class LocalProfilesService extends ProfileProvider { return { type: TerminalTabComponent, inputs: { - sessionOptions: options, + profile, }, } } diff --git a/tabby-local/src/recoveryProvider.ts b/tabby-local/src/recoveryProvider.ts index d14a1cd6..2a8fe1c5 100644 --- a/tabby-local/src/recoveryProvider.ts +++ b/tabby-local/src/recoveryProvider.ts @@ -7,14 +7,14 @@ import { TerminalTabComponent } from './components/terminalTab.component' @Injectable() export class RecoveryProvider extends TabRecoveryProvider { async applicableTo (recoveryToken: RecoveryToken): Promise { - return recoveryToken.type === 'app:terminal-tab' + return recoveryToken.type === 'app:local-tab' } async recover (recoveryToken: RecoveryToken): Promise> { return { type: TerminalTabComponent, inputs: { - sessionOptions: recoveryToken.sessionOptions, + profile: recoveryToken.profile, savedState: recoveryToken.savedState, }, } @@ -23,9 +23,12 @@ export class RecoveryProvider extends TabRecoveryProvider duplicate (recoveryToken: RecoveryToken): RecoveryToken { return { ...recoveryToken, - sessionOptions: { - ...recoveryToken.sessionOptions, - restoreFromPTYID: null, + profile: { + ...recoveryToken.profile, + options: { + ...recoveryToken.profile.options, + restoreFromPTYID: null, + }, }, savedState: null, } diff --git a/tabby-local/src/services/terminal.service.ts b/tabby-local/src/services/terminal.service.ts index 12389d06..742f4e1c 100644 --- a/tabby-local/src/services/terminal.service.ts +++ b/tabby-local/src/services/terminal.service.ts @@ -2,7 +2,7 @@ import * as fs from 'mz/fs' import { Injectable } from '@angular/core' import { Logger, LogService, ConfigService, AppService, ProfilesService } from 'tabby-core' import { TerminalTabComponent } from '../components/terminalTab.component' -import { SessionOptions, LocalProfile } from '../api' +import { LocalProfile } from '../api' @Injectable({ providedIn: 'root' }) export class TerminalService { @@ -55,15 +55,4 @@ export class TerminalService { options, })) as TerminalTabComponent } - - /** - * Open a terminal with custom session options - */ - openTabWithOptions (sessionOptions: SessionOptions): TerminalTabComponent { - this.logger.info('Using session options:', sessionOptions) - return this.app.openNewTab({ - type: TerminalTabComponent, - inputs: { sessionOptions }, - }) - } } diff --git a/tabby-local/src/session.ts b/tabby-local/src/session.ts index 27db5c75..61ccad91 100644 --- a/tabby-local/src/session.ts +++ b/tabby-local/src/session.ts @@ -104,8 +104,6 @@ export class Session extends BaseSession { } start (options: SessionOptions): void { - this.name = options.name ?? '' - let pty: PTYProxy|null = null if (options.restoreFromPTYID) { diff --git a/tabby-local/src/tabContextMenu.ts b/tabby-local/src/tabContextMenu.ts index 9d5081aa..7b6ad4d4 100644 --- a/tabby-local/src/tabContextMenu.ts +++ b/tabby-local/src/tabContextMenu.ts @@ -33,8 +33,8 @@ export class SaveAsProfileContextMenu extends TabContextMenuItemProvider { } const profile = { options: { - ...tab.sessionOptions, - cwd: await tab.session?.getWorkingDirectory() ?? tab.sessionOptions.cwd, + ...tab.profile.options, + cwd: await tab.session?.getWorkingDirectory() ?? tab.profile.options.cwd, }, name, type: 'local', @@ -74,7 +74,11 @@ export class NewTabContextMenu extends TabContextMenuItemProvider { { label: 'New terminal', click: () => { - this.terminalService.openTabWithOptions((tab as any).sessionOptions) + if (tab instanceof TerminalTabComponent) { + this.profilesService.openNewTabForProfile(tab.profile) + } else { + this.terminalService.openTab() + } }, }, { @@ -98,9 +102,12 @@ export class NewTabContextMenu extends TabContextMenuItemProvider { submenu: profiles.map(profile => ({ label: profile.name, click: () => { - this.terminalService.openTabWithOptions({ - ...profile.options, - runAsAdministrator: true, + this.profilesService.openNewTabForProfile({ + ...profile, + options: { + ...profile.options, + runAsAdministrator: true, + }, }) }, })), @@ -111,9 +118,12 @@ export class NewTabContextMenu extends TabContextMenuItemProvider { items.push({ label: 'Duplicate as administrator', click: () => { - this.terminalService.openTabWithOptions({ - ...tab.sessionOptions, - runAsAdministrator: true, + this.profilesService.openNewTabForProfile({ + ...tab.profile, + options: { + ...tab.profile.options, + runAsAdministrator: true, + }, }) }, }) diff --git a/tabby-terminal/src/session.ts b/tabby-terminal/src/session.ts index 628f26a4..185f3111 100644 --- a/tabby-terminal/src/session.ts +++ b/tabby-terminal/src/session.ts @@ -8,7 +8,6 @@ import { LoginScriptProcessor, LoginScriptsOptions } from './api/loginScriptProc */ export abstract class BaseSession { open: boolean - name: string truePID: number protected output = new Subject() protected binaryOutput = new Subject() -- GitLab