tabby/terminus-terminal/src/buttonProvider.ts

65 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-07-05 16:22:44 +02:00
import * as fs from 'mz/fs'
import * as path from 'path'
2017-09-09 21:42:48 +02:00
import { Injectable } from '@angular/core'
2017-08-02 13:47:00 +02:00
import { HotkeysService, ToolbarButtonProvider, IToolbarButton, ConfigService, HostAppService, ElectronService, Logger, LogService } from 'terminus-core'
2017-04-11 02:22:48 +02:00
2017-08-02 13:47:00 +02:00
import { TerminalService } from './services/terminal.service'
2017-03-24 22:24:12 +01:00
@Injectable()
2017-03-25 18:12:43 +01:00
export class ButtonProvider extends ToolbarButtonProvider {
2017-07-30 20:58:31 +02:00
private logger: Logger
2017-03-24 22:24:12 +01:00
constructor (
2017-08-02 13:47:00 +02:00
private terminal: TerminalService,
2017-04-16 20:06:29 +02:00
private config: ConfigService,
2017-07-30 20:58:31 +02:00
log: LogService,
hostApp: HostAppService,
electron: ElectronService,
2017-03-29 14:04:01 +02:00
hotkeys: HotkeysService,
2017-03-24 22:24:12 +01:00
) {
2017-03-25 18:12:43 +01:00
super()
2017-07-30 20:58:31 +02:00
this.logger = log.create('newTerminalButton')
2017-03-29 14:04:01 +02:00
hotkeys.matchedHotkey.subscribe(async (hotkey) => {
2017-05-01 13:35:26 +02:00
if (hotkey === 'new-tab') {
2017-04-08 14:50:10 +02:00
this.openNewTab()
2017-03-29 14:04:01 +02:00
}
})
2017-07-05 16:22:44 +02: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 20:52:06 +02: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 14:04:01 +02:00
}
2017-07-05 16:22:44 +02:00
async openNewTab (cwd?: string): Promise<void> {
2017-09-09 21:42:48 +02:00
let shells = await this.terminal.shells$.first().toPromise()
let shell = shells.find(x => x.id === this.config.store.terminal.shell)
2017-08-02 13:47:00 +02:00
this.terminal.openTab(shell, cwd)
2017-03-24 22:24:12 +01:00
}
provide (): IToolbarButton[] {
return [{
icon: 'plus',
title: 'New terminal',
2017-03-25 21:00:16 +01:00
click: async () => {
2017-04-08 14:50:10 +02:00
this.openNewTab()
2017-03-24 22:24:12 +01:00
}
}]
}
}