diff options
| author | Jomar Milan <jomarm@jomarm.tech> | 2023-09-05 21:30:26 -0700 |
|---|---|---|
| committer | Jomar Milan <jomarm@jomarm.tech> | 2023-09-05 21:30:26 -0700 |
| commit | 392461d253ddc9a5a2adb22a52723111e67ee548 (patch) | |
| tree | 129d24f87c00c81fc1df181ca25a14406c259cb4 /src/osc-notify-middleware.ts | |
Initial commit
Diffstat (limited to 'src/osc-notify-middleware.ts')
| -rw-r--r-- | src/osc-notify-middleware.ts | 56 |
1 files changed, 56 insertions, 0 deletions
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(); + } +} |
