diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 11 | ||||
| -rw-r--r-- | src/osc-notify-decorator.ts | 26 | ||||
| -rw-r--r-- | src/osc-notify-middleware.ts | 56 |
3 files changed, 93 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..aeeed14 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,11 @@ +import OscNotifyDecorator from "./osc-notify-decorator"; +import { NgModule } from "@angular/core"; +import { TerminalDecorator } from "tabby-terminal"; + +@NgModule({ + providers: [ + { provide: TerminalDecorator, useClass: OscNotifyDecorator, multi: true } + ] +}) +export default class OscNotifyModule { +} diff --git a/src/osc-notify-decorator.ts b/src/osc-notify-decorator.ts new file mode 100644 index 0000000..7d32f1c --- /dev/null +++ b/src/osc-notify-decorator.ts @@ -0,0 +1,26 @@ +import OscNotifyMiddleware from "./osc-notify-middleware"; +import { Injectable } from "@angular/core"; +import { TerminalDecorator, BaseSession, BaseTerminalTabComponent } from "tabby-terminal"; + +@Injectable() +export default class OscNotifyDecorator extends TerminalDecorator { + attach(tab: BaseTerminalTabComponent<any>): void { + // Subscribe to attach to future sessions in this tab + tab.sessionChanged$.subscribe((session) => { + if (session) { + this.attachToSession(session, (<any>tab).title); + } + }); + + // Attach the current session for this tab + if (tab.session) { + this.attachToSession(tab.session, (<any>tab).title); + } + } + + private attachToSession(session: BaseSession, tabTitle: string) { + const middleware = new OscNotifyMiddleware(tabTitle); + + session.middleware.push(middleware); + } +} diff --git a/src/osc-notify-middleware.ts b/src/osc-notify-middleware.ts new file mode 100644 index 0000000..15548f4 --- /dev/null +++ b/src/osc-notify-middleware.ts @@ -0,0 +1,56 @@ +import { SessionMiddleware } from "tabby-terminal"; +import { Observable, Subject } from "rxjs"; + +const oscPrefix = Buffer.from("\x1b]"); +const oscSuffix = Buffer.from("\x07"); + +export default class OscNotifyMiddleware extends SessionMiddleware { + tabTitle: string; + + constructor(tabTitle: string) { + super(); + + this.tabTitle = tabTitle; + } + + get cwdReported$(): Observable<string> { + return this.cwdReported; + } + get copyRequested$(): Observable<string> { + return this.copyRequested; + } + + private cwdReported = new Subject<string>(); + private copyRequested = new Subject<string>(); + + feedFromSession(data: Buffer): void { + let startIndex = 0; + + while (data.includes(oscPrefix, startIndex) && data.includes(oscSuffix, startIndex)) { + const params = data.subarray(data.indexOf(oscPrefix, startIndex) + oscPrefix.length); + const oscString = params.subarray(0, params.indexOf(oscSuffix)).toString(); + + startIndex = data.indexOf(oscSuffix, startIndex) + oscSuffix.length; + + const [ oscCodeString, ...oscParams ] = oscString.split(";"); + const oscCode = parseInt(oscCodeString); + + if (oscCode === 9) { + // TODO: onclick switch to tab + new Notification(this.tabTitle, { + body: oscParams[0] + }); + } else { + continue; + } + } + + super.feedFromSession(data); + } + + close(): void { + this.cwdReported.complete(); + this.copyRequested.complete(); + super.close(); + } +} |
