1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-12-27 12:32:10 +03:00
tabby/app/src/terminal/persistenceProviders.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-03-25 23:00:16 +03:00
import * as fs from 'fs-promise'
const exec = require('child-process-promise').exec
2017-04-02 18:33:55 +03:00
const spawn = require('child-process-promise').spawn
2017-03-25 23:00:16 +03:00
import { SessionOptions, SessionPersistenceProvider } from './api'
export class ScreenPersistenceProvider extends SessionPersistenceProvider {
2017-04-02 18:33:55 +03:00
/*
2017-03-25 23:00:16 +03:00
list(): Promise<any[]> {
return exec('screen -list').then((result) => {
return result.stdout.split('\n')
.filter((line) => /\bterm-tab-/.exec(line))
.map((line) => line.trim().split('.')[0])
}).catch(() => {
return []
})
}
2017-04-02 18:33:55 +03:00
*/
async attachSession (recoveryId: any): Promise<SessionOptions> {
let lines = (await exec('screen -list')).stdout.split('\n')
let screenPID = lines
.filter(line => line.indexOf('.' + recoveryId) !== -1)
.map(line => parseInt(line.trim().split('.')[0]))[0]
if (!screenPID) {
return null
}
lines = (await exec(`ps -o pid --ppid ${screenPID}`)).stdout.split('\n')
let recoveredTruePID = parseInt(lines[1].split(/\s/).filter(x => !!x)[0])
2017-03-25 23:00:16 +03:00
return {
recoveryId,
2017-04-02 18:33:55 +03:00
recoveredTruePID,
2017-03-25 23:00:16 +03:00
command: 'screen',
args: ['-r', recoveryId],
}
}
2017-04-02 18:33:55 +03:00
async startSession (options: SessionOptions): Promise<any> {
2017-03-25 23:00:16 +03:00
let configPath = '/tmp/.termScreenConfig'
await fs.writeFile(configPath, `
escape ^^^
vbell off
term xterm-color
bindkey "^[OH" beginning-of-line
bindkey "^[OF" end-of-line
termcapinfo xterm* 'hs:ts=\\E]0;:fs=\\007:ds=\\E]0;\\007'
defhstatus "^Et"
hardstatus off
`, 'utf-8')
let recoveryId = `term-tab-${Date.now()}`
2017-04-02 18:33:55 +03:00
let args = ['-d', '-m', '-c', configPath, '-U', '-S', recoveryId, '--', options.command].concat(options.args || [])
await spawn('screen', args, {
cwd: options.cwd,
env: options.env || process.env,
})
return recoveryId
2017-03-25 23:00:16 +03:00
}
async terminateSession (recoveryId: string): Promise<void> {
await exec(`screen -S ${recoveryId} -X quit`)
}
}