From c0c2373ed613e98eda9ba2f278c9837e419bc0ea Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Wed, 5 Jul 2017 12:49:01 +0200 Subject: [PATCH] hopefully fixed #2 --- terminus-terminal/src/buttonProvider.ts | 37 ++---------- terminus-terminal/src/index.ts | 2 + .../src/services/shells.service.ts | 58 +++++++++++++++++++ 3 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 terminus-terminal/src/services/shells.service.ts diff --git a/terminus-terminal/src/buttonProvider.ts b/terminus-terminal/src/buttonProvider.ts index 26f0c6f9..eeec98f9 100644 --- a/terminus-terminal/src/buttonProvider.ts +++ b/terminus-terminal/src/buttonProvider.ts @@ -1,10 +1,8 @@ -import * as path from 'path' -import { exec } from 'mz/child_process' -import * as fs from 'mz/fs' import { Injectable } from '@angular/core' -import { HotkeysService, ToolbarButtonProvider, IToolbarButton, AppService, ConfigService, ElectronService, HostAppService, Platform } from 'terminus-core' +import { HotkeysService, ToolbarButtonProvider, IToolbarButton, AppService, ConfigService } from 'terminus-core' import { SessionsService } from './services/sessions.service' +import { ShellsService } from './services/shells.service' import { TerminalTabComponent } from './components/terminalTab.component' @Injectable() @@ -13,8 +11,7 @@ export class ButtonProvider extends ToolbarButtonProvider { private app: AppService, private sessions: SessionsService, private config: ConfigService, - private electron: ElectronService, - private hostApp: HostAppService, + private shells: ShellsService, hotkeys: HotkeysService, ) { super() @@ -32,35 +29,11 @@ export class ButtonProvider extends ToolbarButtonProvider { } let command = this.config.store.terminal.shell let args = [] - // TODO move this? if (command === '~clink~') { - command = 'cmd.exe' - args = [ - '/k', - path.join( - path.dirname(this.electron.app.getPath('exe')), - (process.platform === 'darwin') ? '../Resources' : 'resources', - 'clink', - `clink_${process.arch}.exe`, - ), - 'inject', - ] + ({ command, args } = this.shells.getClinkOptions()) } if (command === '~default-shell~') { - if (this.hostApp.platform === Platform.Linux) { - let line = (await fs.readFile('/etc/passwd', { encoding: 'utf-8' })) - .split('\n').find(x => x.startsWith(process.env.LOGNAME + ':')) - if (!line) { - console.warn('Could not detect user shell') - command = '/bin/sh' - } else { - command = line.split(':')[6] - } - } - if (this.hostApp.platform === Platform.macOS) { - let shellEntry = (await exec(`dscl . -read /Users/${process.env.LOGNAME} UserShell`))[0].toString() - command = shellEntry.split(':')[1].trim() - } + command = await this.shells.getDefaultShell() } let sessionOptions = await this.sessions.prepareNewSession({ command, args, cwd }) this.app.openNewTab( diff --git a/terminus-terminal/src/index.ts b/terminus-terminal/src/index.ts index 6c0b5241..68e97efa 100644 --- a/terminus-terminal/src/index.ts +++ b/terminus-terminal/src/index.ts @@ -11,6 +11,7 @@ import { TerminalSettingsTabComponent } from './components/terminalSettingsTab.c import { ColorPickerComponent } from './components/colorPicker.component' import { SessionsService } from './services/sessions.service' +import { ShellsService } from './services/shells.service' import { ScreenPersistenceProvider } from './persistenceProviders' import { ButtonProvider } from './buttonProvider' @@ -31,6 +32,7 @@ import { hterm } from './hterm' ], providers: [ SessionsService, + ShellsService, ScreenPersistenceProvider, { provide: ToolbarButtonProvider, useClass: ButtonProvider, multi: true }, { provide: TabRecoveryProvider, useClass: RecoveryProvider, multi: true }, diff --git a/terminus-terminal/src/services/shells.service.ts b/terminus-terminal/src/services/shells.service.ts new file mode 100644 index 00000000..d63a7673 --- /dev/null +++ b/terminus-terminal/src/services/shells.service.ts @@ -0,0 +1,58 @@ +import * as path from 'path' +import { exec } from 'mz/child_process' +import * as fs from 'mz/fs' +import { Injectable } from '@angular/core' +import { ElectronService, HostAppService, Platform, Logger, LogService } from 'terminus-core' + +@Injectable() +export class ShellsService { + private logger: Logger + + constructor ( + log: LogService, + private electron: ElectronService, + private hostApp: HostAppService, + ) { + this.logger = log.create('shells') + } + + getClinkOptions (): { command, args } { + return { + command: 'cmd.exe', + args: [ + '/k', + path.join( + path.dirname(this.electron.app.getPath('exe')), + 'resources', + 'clink', + `clink_${process.arch}.exe`, + ), + 'inject', + ] + } + } + + async getDefaultShell (): Promise { + if (this.hostApp.platform === Platform.macOS) { + return this.getDefaultMacOSShell() + } else { + return this.getDefaultLinuxShell() + } + } + + async getDefaultMacOSShell (): Promise { + let shellEntry = (await exec(`dscl . -read /Users/${process.env.LOGNAME} UserShell`))[0].toString() + return shellEntry.split(' ')[1].trim() + } + + async getDefaultLinuxShell (): Promise { + let line = (await fs.readFile('/etc/passwd', { encoding: 'utf-8' })) + .split('\n').find(x => x.startsWith(process.env.LOGNAME + ':')) + if (!line) { + this.logger.warn('Could not detect user shell') + return '/bin/sh' + } else { + return line.split(':')[6] + } + } +}