terminal.service.ts 1.8 KB
Newer Older
E
Eugene Pankov 已提交
1
import * as fs from 'mz/fs'
E
Eugene Pankov 已提交
2 3
import { Injectable } from '@angular/core'
import { Logger, LogService, ConfigService, AppService, ProfilesService } from 'tabby-core'
E
Eugene Pankov 已提交
4
import { TerminalTabComponent } from '../components/terminalTab.component'
5
import { LocalProfile } from '../api'
E
Eugene Pankov 已提交
6

E
Eugene Pankov 已提交
7
@Injectable({ providedIn: 'root' })
E
Eugene Pankov 已提交
8 9 10
export class TerminalService {
    private logger: Logger

E
Eugene Pankov 已提交
11
    /** @hidden */
E
Eugene Pankov 已提交
12
    private constructor (
E
Eugene Pankov 已提交
13
        private app: AppService,
E
Eugene Pankov 已提交
14
        private profilesService: ProfilesService,
E
Eugene Pankov 已提交
15
        private config: ConfigService,
E
Eugene Pankov 已提交
16 17 18
        log: LogService,
    ) {
        this.logger = log.create('terminal')
E
Eugene Pankov 已提交
19 20
    }

E
Eugene Pankov 已提交
21 22 23 24 25 26 27
    async getDefaultProfile (): Promise<LocalProfile> {
        const profiles = await this.profilesService.getProfiles()
        let profile = profiles.find(x => x.id === this.config.store.terminal.profile)
        if (!profile) {
            profile = profiles.filter(x => x.type === 'local' && x.isBuiltin)[0]
        }
        return profile as LocalProfile
E
Eugene Pankov 已提交
28 29
    }

E
Eugene Pankov 已提交
30 31 32 33
    /**
     * Launches a new terminal with a specific shell and CWD
     * @param pause Wait for a keypress when the shell exits
     */
E
Eugene Pankov 已提交
34
    async openTab (profile?: LocalProfile|null, cwd?: string|null, pause?: boolean): Promise<TerminalTabComponent> {
E
Eugene Pankov 已提交
35
        if (!profile) {
E
Eugene Pankov 已提交
36
            profile = await this.getDefaultProfile()
E
Eugene Pankov 已提交
37 38
        }

E
Eugene Pankov 已提交
39
        cwd = cwd ?? profile.options.cwd
E
Eugene Pankov 已提交
40

E
Eugene Pankov 已提交
41 42 43 44
        if (cwd && !fs.existsSync(cwd)) {
            console.warn('Ignoring non-existent CWD:', cwd)
            cwd = null
        }
E
Eugene Pankov 已提交
45

E
Eugene Pankov 已提交
46
        this.logger.info(`Starting profile ${profile.name}`, profile)
E
Eugene Pankov 已提交
47 48
        const options = {
            ...profile.options,
49
            pauseAfterExit: pause,
50
            cwd: cwd ?? undefined,
E
Eugene Pankov 已提交
51
        }
E
Eugene Pankov 已提交
52

E
Eugene Pankov 已提交
53 54 55 56
        return (await this.profilesService.openNewTabForProfile({
            ...profile,
            options,
        })) as TerminalTabComponent
57
    }
E
Eugene Pankov 已提交
58
}