From 392461d253ddc9a5a2adb22a52723111e67ee548 Mon Sep 17 00:00:00 2001 From: Jomar Milan Date: Tue, 5 Sep 2023 21:30:26 -0700 Subject: Initial commit --- src/index.ts | 11 +++++++++ src/osc-notify-decorator.ts | 26 ++++++++++++++++++++ src/osc-notify-middleware.ts | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/index.ts create mode 100644 src/osc-notify-decorator.ts create mode 100644 src/osc-notify-middleware.ts (limited to 'src') 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): void { + // Subscribe to attach to future sessions in this tab + tab.sessionChanged$.subscribe((session) => { + if (session) { + this.attachToSession(session, (tab).title); + } + }); + + // Attach the current session for this tab + if (tab.session) { + this.attachToSession(tab.session, (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 { + return this.cwdReported; + } + get copyRequested$(): Observable { + return this.copyRequested; + } + + private cwdReported = new Subject(); + private copyRequested = new Subject(); + + 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(); + } +} -- cgit v1.3.1