summaryrefslogtreecommitdiff
path: root/src/osc-notify-middleware.ts
diff options
context:
space:
mode:
authorJomar Milan <jomarm@jomarm.tech>2023-09-05 21:30:26 -0700
committerJomar Milan <jomarm@jomarm.tech>2023-09-05 21:30:26 -0700
commit392461d253ddc9a5a2adb22a52723111e67ee548 (patch)
tree129d24f87c00c81fc1df181ca25a14406c259cb4 /src/osc-notify-middleware.ts
Initial commit
Diffstat (limited to 'src/osc-notify-middleware.ts')
-rw-r--r--src/osc-notify-middleware.ts56
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();
+ }
+}