terminalTab.component.ts 3.7 KB
Newer Older
E
Eugene Pankov 已提交
1
import { Component, Input, Injector } from '@angular/core'
E
Eugene Pankov 已提交
2 3
import { BaseTabProcess, WIN_BUILD_CONPTY_SUPPORTED, isWindowsBuild } from 'tabby-core'
import { BaseTerminalTabComponent } from 'tabby-terminal'
E
lint  
Eugene Pankov 已提交
4
import { LocalProfile, SessionOptions } from '../api'
5
import { Session } from '../session'
E
Eugene Pankov 已提交
6
import { UACService } from '../services/uac.service'
E
.  
Eugene Pankov 已提交
7

E
Eugene Pankov 已提交
8
/** @hidden */
E
.  
Eugene Pankov 已提交
9
@Component({
E
lint  
Eugene Pankov 已提交
10
    selector: 'terminalTab',
11 12
    template: BaseTerminalTabComponent.template,
    styles: BaseTerminalTabComponent.styles,
E
Eugene Pankov 已提交
13
    animations: BaseTerminalTabComponent.animations,
E
.  
Eugene Pankov 已提交
14
})
15
export class TerminalTabComponent extends BaseTerminalTabComponent {
16 17
    @Input() sessionOptions: SessionOptions // Deprecated
    @Input() profile: LocalProfile
E
Eugene Pankov 已提交
18
    session: Session|null = null
E
Eugene Pankov 已提交
19

E
Eugene Pankov 已提交
20 21 22
    // eslint-disable-next-line @typescript-eslint/no-useless-constructor
    constructor (
        injector: Injector,
E
Eugene Pankov 已提交
23
        private uac: UACService,
E
Eugene Pankov 已提交
24 25 26 27
    ) {
        super(injector)
    }

E
lint  
Eugene Pankov 已提交
28
    ngOnInit (): void {
29 30
        this.sessionOptions = this.profile.options

31
        this.logger = this.log.create('terminalTab')
32
        this.session = new Session(this.injector)
E
Eugene Pankov 已提交
33

E
lint  
Eugene Pankov 已提交
34
        const isConPTY = isWindowsBuild(WIN_BUILD_CONPTY_SUPPORTED) && this.config.store.terminal.useConPTY
35

E
Eugene Pankov 已提交
36
        this.subscribeUntilDestroyed(this.hotkeys.hotkey$, hotkey => {
37 38 39 40
            if (!this.hasFocus) {
                return
            }
            switch (hotkey) {
E
Eugene Pankov 已提交
41 42 43 44 45 46
                case 'home':
                    this.sendInput(isConPTY ? '\x1b[H' : '\x1bOH')
                    break
                case 'end':
                    this.sendInput(isConPTY ? '\x1b[F' : '\x1bOF')
                    break
47 48 49
            }
        })

50
        super.ngOnInit()
E
.  
Eugene Pankov 已提交
51 52
    }

E
Eugene Pankov 已提交
53 54
    protected onFrontendReady (): void {
        this.initializeSession(this.size.columns, this.size.rows)
55
        this.savedStateIsLive = this.profile.options.restoreFromPTYID === this.session?.getPTYID()
E
Eugene Pankov 已提交
56 57 58
        super.onFrontendReady()
    }

E
lint  
Eugene Pankov 已提交
59
    initializeSession (columns: number, rows: number): void {
60 61
        if (this.profile.options.runAsAdministrator && this.uac.isAvailable) {
            this.profile.options = this.uac.patchSessionOptionsForUAC(this.profile.options)
E
Eugene Pankov 已提交
62 63
        }

64
        this.session!.start({
65
            ...this.profile.options,
66 67 68
            width: columns,
            height: rows,
        })
69

70
        this.attachSessionHandlers(true)
E
Eugene Pankov 已提交
71
        this.recoveryStateChangedHint.next()
72 73
    }

E
Eugene Pankov 已提交
74
    async getRecoveryToken (): Promise<any> {
E
lint  
Eugene Pankov 已提交
75
        const cwd = this.session ? await this.session.getWorkingDirectory() : null
E
Eugene Pankov 已提交
76
        return {
77
            type: 'app:local-tab',
78 79 80 81 82 83 84
            profile: {
                ...this.profile,
                options: {
                    ...this.profile.options,
                    cwd: cwd ?? this.profile.options.cwd,
                    restoreFromPTYID: this.session?.getPTYID(),
                },
E
Eugene Pankov 已提交
85
            },
E
Eugene Pankov 已提交
86
            savedState: this.frontend?.saveState(),
E
Eugene Pankov 已提交
87 88 89
        }
    }

E
Eugene Pankov 已提交
90
    async getCurrentProcess (): Promise<BaseTabProcess|null> {
91 92
        const children = await this.session?.getChildProcesses()
        if (!children?.length) {
E
Eugene Pankov 已提交
93 94 95
            return null
        }
        return {
E
Eugene Pankov 已提交
96
            name: children[0].command,
E
Eugene Pankov 已提交
97 98 99
        }
    }

E
Eugene Pankov 已提交
100
    async canClose (): Promise<boolean> {
101 102
        const children = await this.session?.getChildProcesses()
        if (!children?.length) {
E
Eugene Pankov 已提交
103 104
            return true
        }
E
Eugene Pankov 已提交
105
        return (await this.platform.showMessageBox(
E
Eugene Pankov 已提交
106 107 108
            {
                type: 'warning',
                message: `"${children[0].command}" is still running. Close?`,
109 110 111
                buttons: ['Kill', 'Cancel'],
                defaultId: 0,
                cancelId: 1,
E
Eugene Pankov 已提交
112
            }
113
        )).response === 0
E
Eugene Pankov 已提交
114
    }
115

E
lint  
Eugene Pankov 已提交
116
    ngOnDestroy (): void {
117
        super.ngOnDestroy()
118
        this.session?.destroy()
119
    }
E
.  
Eugene Pankov 已提交
120
}