2017-07-05 14:22:44 +00:00
|
|
|
import * as fs from 'mz/fs'
|
|
|
|
import * as path from 'path'
|
2018-05-20 14:12:05 +00:00
|
|
|
import { first } from 'rxjs/operators'
|
2017-09-09 19:42:48 +00:00
|
|
|
import { Injectable } from '@angular/core'
|
2018-05-20 14:12:05 +00:00
|
|
|
import { HotkeysService, ToolbarButtonProvider, IToolbarButton, ConfigService, HostAppService, ElectronService } from 'terminus-core'
|
2017-04-11 00:22:48 +00:00
|
|
|
|
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-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
|
|
|
hostApp: HostAppService,
|
2017-07-10 15:57:58 +00:00
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-07-10 15:57:58 +00:00
|
|
|
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])) {
|
2017-07-10 15:57:58 +00:00
|
|
|
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> {
|
2018-05-20 14:12:05 +00:00
|
|
|
let shells = await this.terminal.shells$.pipe(first()).toPromise()
|
2017-09-09 19:42:48 +00:00
|
|
|
let shell = shells.find(x => x.id === this.config.store.terminal.shell)
|
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',
|
2018-04-01 17:50:43 +00:00
|
|
|
touchBarTitle: 'New',
|
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
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|