From 6132881b8ab3b2e9a751e511d33ab6fafe7a6abe Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Thu, 6 Feb 2020 14:47:21 +0300 Subject: [PATCH] allow saving terminal state for debugging --- terminus-terminal/src/debug.ts | 69 ++++++++++++++++++++++++++++++++++ terminus-terminal/src/index.ts | 2 + 2 files changed, 71 insertions(+) create mode 100644 terminus-terminal/src/debug.ts diff --git a/terminus-terminal/src/debug.ts b/terminus-terminal/src/debug.ts new file mode 100644 index 00000000..0d630e30 --- /dev/null +++ b/terminus-terminal/src/debug.ts @@ -0,0 +1,69 @@ +import * as fs from 'fs' +import { Injectable } from '@angular/core' +import { TerminalDecorator } from './api/decorator' +import { TerminalTabComponent } from './components/terminalTab.component' +import { ElectronService, HostAppService } from 'terminus-core' + +/** @hidden */ +@Injectable() +export class DebugDecorator extends TerminalDecorator { + constructor ( + private electron: ElectronService, + private hostApp: HostAppService, + ) { + super() + } + + attach (terminal: TerminalTabComponent): void { + terminal.content.nativeElement.addEventListener('keyup', e => { + if (e.which === 49 && e.ctrlKey && e.shiftKey && e.altKey) { + this.doSaveOutput(terminal) + } + if (e.which === 50 && e.ctrlKey && e.shiftKey && e.altKey) { + this.doLoadInput(terminal) + } + if (e.which === 51 && e.ctrlKey && e.shiftKey && e.altKey) { + this.doCopyOutput(terminal) + } + if (e.which === 52 && e.ctrlKey && e.shiftKey && e.altKey) { + this.doPasteOutput(terminal) + } + }) + } + + async doSaveOutput (terminal: TerminalTabComponent) { + const result = await this.electron.dialog.showSaveDialog( + this.hostApp.getWindow(), + { + defaultPath: 'output.txt', + }, + ) + if (result.filePath) { + fs.writeFileSync(result.filePath, terminal.frontend.saveState()) + } + } + + async doCopyOutput (terminal: TerminalTabComponent) { + const data = '```' + JSON.stringify(terminal.frontend.saveState()) + '```' + this.electron.clipboard.writeText(data) + } + + async doLoadInput (terminal: TerminalTabComponent) { + const result = await this.electron.dialog.showOpenDialog( + this.hostApp.getWindow(), + { + buttonLabel: 'Load', + properties: ['openFile', 'treatPackageAsDirectory'], + }, + ) + if (result.filePaths.length) { + const data = fs.readFileSync(result.filePaths[0]) + terminal.frontend.restoreState(data) + } + } + + async doPasteOutput (terminal: TerminalTabComponent) { + const data = JSON.parse(this.electron.clipboard.readText()) + terminal.frontend.restoreState(data) + } +} diff --git a/terminus-terminal/src/index.ts b/terminus-terminal/src/index.ts index 40f918b1..33430a27 100644 --- a/terminus-terminal/src/index.ts +++ b/terminus-terminal/src/index.ts @@ -31,6 +31,7 @@ import { TerminalContextMenuItemProvider } from './api/contextMenuProvider' import { TerminalColorSchemeProvider } from './api/colorSchemeProvider' import { ShellProvider } from './api/shellProvider' import { TerminalSettingsTabProvider, AppearanceSettingsTabProvider, ShellSettingsTabProvider } from './settings' +import { DebugDecorator } from './debug' import { PathDropDecorator } from './pathDrop' import { TerminalConfigProvider } from './config' import { TerminalHotkeyProvider } from './hotkeys' @@ -77,6 +78,7 @@ import { XTermFrontend, XTermWebGLFrontend } from './frontends/xtermFrontend' { provide: TerminalColorSchemeProvider, useClass: HyperColorSchemes, multi: true }, { provide: TerminalDecorator, useClass: PathDropDecorator, multi: true }, { provide: TerminalDecorator, useClass: ZModemDecorator, multi: true }, + { provide: TerminalDecorator, useClass: DebugDecorator, multi: true }, { provide: ShellProvider, useClass: WindowsDefaultShellProvider, multi: true }, { provide: ShellProvider, useClass: MacOSDefaultShellProvider, multi: true },