profiles.service.ts 5.9 KB
Newer Older
E
Eugene Pankov 已提交
1 2 3 4
import { Injectable, Inject } from '@angular/core'
import { NewTabParameters } from './tabs.service'
import { BaseTabComponent } from '../components/baseTab.component'
import { Profile, ProfileProvider } from '../api/profileProvider'
E
Eugene Pankov 已提交
5
import { SelectorOption } from '../api/selector'
E
Eugene Pankov 已提交
6 7
import { AppService } from './app.service'
import { ConfigService } from './config.service'
8 9
import { NotificationsService } from './notifications.service'
import { SelectorService } from './selector.service'
E
Eugene Pankov 已提交
10 11 12 13 14 15

@Injectable({ providedIn: 'root' })
export class ProfilesService {
    constructor (
        private app: AppService,
        private config: ConfigService,
16 17
        private notifications: NotificationsService,
        private selector: SelectorService,
E
Eugene Pankov 已提交
18 19 20 21 22 23 24 25
        @Inject(ProfileProvider) private profileProviders: ProfileProvider[],
    ) { }

    async openNewTabForProfile (profile: Profile): Promise<BaseTabComponent|null> {
        const params = await this.newTabParametersForProfile(profile)
        if (params) {
            const tab = this.app.openNewTab(params)
            ;(this.app.getParentTab(tab) ?? tab).color = profile.color ?? null
26
            tab.setTitle(profile.name)
E
Eugene Pankov 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
            if (profile.disableDynamicTitle) {
                tab['enableDynamicTitle'] = false
            }
            return tab
        }
        return null
    }

    async newTabParametersForProfile (profile: Profile): Promise<NewTabParameters<BaseTabComponent>|null> {
        return this.providerForProfile(profile)?.getNewTabParameters(profile) ?? null
    }

    getProviders (): ProfileProvider[] {
        return [...this.profileProviders]
    }

    async getProfiles (): Promise<Profile[]> {
        const lists = await Promise.all(this.config.enabledServices(this.profileProviders).map(x => x.getBuiltinProfiles()))
        let list = lists.reduce((a, b) => a.concat(b), [])
        list = [
            ...this.config.store.profiles ?? [],
            ...list,
        ]
50 51
        const sortKey = p => `${p.group ?? ''} / ${p.name}`
        list.sort((a, b) => sortKey(a).localeCompare(sortKey(b)))
E
Eugene Pankov 已提交
52 53 54 55 56 57 58
        list.sort((a, b) => (a.isBuiltin ? 1 : 0) - (b.isBuiltin ? 1 : 0))
        return list
    }

    providerForProfile (profile: Profile): ProfileProvider|null {
        return this.profileProviders.find(x => x.id === profile.type) ?? null
    }
E
Eugene Pankov 已提交
59 60 61 62 63 64 65 66

    selectorOptionForProfile <T> (profile: Profile): SelectorOption<T> {
        return {
            icon: profile.icon,
            name: profile.group ? `${profile.group} / ${profile.name}` : profile.name,
            description: this.providerForProfile(profile)?.getDescription(profile),
        }
    }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

    showProfileSelector (): Promise<Profile|null> {
        return new Promise<Profile|null>(async (resolve, reject) => {
            try {
                const recentProfiles: Profile[] = this.config.store.recentProfiles

                let options: SelectorOption<void>[] = recentProfiles.map(p => ({
                    ...this.selectorOptionForProfile(p),
                    icon: 'fas fa-history',
                    callback: async () => {
                        if (p.id) {
                            p = (await this.getProfiles()).find(x => x.id === p.id) ?? p
                        }
                        resolve(p)
                    },
                }))
                if (recentProfiles.length) {
                    options.push({
                        name: 'Clear recent connections',
                        icon: 'fas fa-eraser',
                        callback: async () => {
                            this.config.store.recentProfiles = []
                            this.config.save()
                            resolve(null)
                        },
                    })
                }

                let profiles = await this.getProfiles()

                if (!this.config.store.terminal.showBuiltinProfiles) {
                    profiles = profiles.filter(x => !x.isBuiltin)
                }

                profiles = profiles.filter(x => !x.isTemplate)

                options = [...options, ...profiles.map((p): SelectorOption<void> => ({
                    ...this.selectorOptionForProfile(p),
                    callback: () => resolve(p),
                }))]

                try {
                    const { SettingsTabComponent } = window['nodeRequire']('tabby-settings')
                    options.push({
                        name: 'Manage profiles',
                        icon: 'fas fa-window-restore',
                        callback: () => {
                            this.app.openNewTabRaw({
                                type: SettingsTabComponent,
                                inputs: { activeTab: 'profiles' },
                            })
                            resolve(null)
                        },
                    })
                } catch { }

                if (this.getProviders().some(x => x.supportsQuickConnect)) {
                    options.push({
                        name: 'Quick connect',
                        freeInputPattern: 'Connect to "%s"...',
                        icon: 'fas fa-arrow-right',
                        callback: query => {
                            const profile = this.quickConnect(query)
                            resolve(profile)
                        },
                    })
                }
                await this.selector.show('Select profile', options)
            } catch (err) {
                reject(err)
            }
        })
    }

    async quickConnect (query: string): Promise<Profile|null> {
        for (const provider of this.getProviders()) {
            if (provider.supportsQuickConnect) {
                const profile = provider.quickConnect(query)
                if (profile) {
                    return profile
                }
            }
        }
        this.notifications.error(`Could not parse "${query}"`)
        return null
    }
E
Eugene Pankov 已提交
153
}