tabby/terminus-terminal/src/buttonProvider.ts

73 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-07-30 18:58:31 +00:00
import { AsyncSubject } from 'rxjs'
2017-07-05 14:22:44 +00:00
import * as fs from 'mz/fs'
import * as path from 'path'
2017-07-30 18:58:31 +00:00
import { Injectable, Inject } from '@angular/core'
2017-08-02 11:47:00 +00:00
import { HotkeysService, ToolbarButtonProvider, IToolbarButton, ConfigService, HostAppService, ElectronService, Logger, LogService } from 'terminus-core'
2017-04-11 00:22:48 +00:00
2017-07-30 18:58:31 +00:00
import { IShell, ShellProvider } from './api'
2017-08-02 11:47:00 +00:00
import { TerminalService } from './services/terminal.service'
2017-03-24 21:24:12 +00:00
@Injectable()
2017-03-25 17:12:43 +00:00
export class ButtonProvider extends ToolbarButtonProvider {
2017-07-30 18:58:31 +00:00
private shells$ = new AsyncSubject<IShell[]>()
private logger: Logger
2017-03-24 21:24:12 +00:00
constructor (
2017-08-02 11:47:00 +00:00
private terminal: TerminalService,
2017-04-16 18:06:29 +00:00
private config: ConfigService,
2017-07-30 18:58:31 +00:00
log: LogService,
hostApp: HostAppService,
@Inject(ShellProvider) shellProviders: ShellProvider[],
electron: ElectronService,
2017-03-29 12:04:01 +00:00
hotkeys: HotkeysService,
2017-03-24 21:24:12 +00:00
) {
2017-03-25 17:12:43 +00:00
super()
2017-07-30 18:58:31 +00:00
this.logger = log.create('newTerminalButton')
Promise.all(shellProviders.map(x => x.provide())).then(shellLists => {
this.shells$.next(shellLists.reduce((a, b) => a.concat(b)))
this.shells$.complete()
})
2017-03-29 12:04:01 +00:00
hotkeys.matchedHotkey.subscribe(async (hotkey) => {
2017-05-01 11:35:26 +00:00
if (hotkey === 'new-tab') {
2017-04-08 12:50:10 +00:00
this.openNewTab()
2017-03-29 12:04:01 +00:00
}
})
2017-07-05 14:22:44 +00:00
hostApp.secondInstance$.subscribe(async ({argv, cwd}) => {
if (argv.length === 2) {
let arg = path.resolve(cwd, argv[1])
if (await fs.exists(arg)) {
this.openNewTab(arg)
}
}
})
if (!electron.remote.process.env.DEV) {
setImmediate(async () => {
let argv: string[] = electron.remote.process.argv
2017-07-12 18:52:06 +00:00
for (let arg of argv.slice(1).concat([electron.remote.process.argv0])) {
if (await fs.exists(arg)) {
if ((await fs.stat(arg)).isDirectory()) {
this.openNewTab(arg)
}
}
}
})
}
2017-03-29 12:04:01 +00:00
}
2017-07-05 14:22:44 +00:00
async openNewTab (cwd?: string): Promise<void> {
2017-07-30 18:58:31 +00:00
let shells = await this.shells$.first().toPromise()
let shell = shells.find(x => x.id === this.config.store.terminal.shell) || shells[0]
2017-08-02 11:47:00 +00:00
this.terminal.openTab(shell, cwd)
2017-03-24 21:24:12 +00:00
}
provide (): IToolbarButton[] {
return [{
icon: 'plus',
title: 'New terminal',
2017-03-25 20:00:16 +00:00
click: async () => {
2017-04-08 12:50:10 +00:00
this.openNewTab()
2017-03-24 21:24:12 +00:00
}
}]
}
}