oc-bg-color-sync 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +21 -0
  2. package/index.ts +27 -6
  3. package/package.json +1 -2
package/README.md CHANGED
@@ -44,6 +44,10 @@ This plugin restores the OSC 11 sync as a userland workaround until upstream fin
44
44
  - After the native call, converts the RGBA color to hex and writes `OSC 11;rgb:rr/gg/bb\x07` to stdout
45
45
  - On plugin dispose, calls `renderer.resetTerminalBgColor()` which writes `OSC 111\x07`
46
46
 
47
+ ### Timing fix
48
+
49
+ `ThemeProvider` calls `setBackgroundColor()` during initial render **before** plugins are loaded. If we only patched the method, the terminal would stay out of sync until the theme changed again. The plugin therefore reads `api.theme.current.background` after patching and calls the patched method once to force an immediate sync.
50
+
47
51
  ### OSC sequence reference
48
52
 
49
53
  - **OSC 11** — Set terminal background color
@@ -63,6 +67,23 @@ Add the plugin to your OpenCode config (`tui.json` or equivalent):
63
67
  }
64
68
  ```
65
69
 
70
+ ## Debugging
71
+
72
+ To verify the plugin is emitting OSC sequences, run OpenCode in a terminal that logs escape sequences, or sniff stdout with `script`:
73
+
74
+ ```bash
75
+ # Start a typescript session; all escape sequences are recorded
76
+ script -q /tmp/opencode-osc.log
77
+ opencode
78
+ # quit opencode, then exit the script shell
79
+ exit
80
+ # Search for OSC 11/111 in the log
81
+ grep -o $'\x1b]11;[^\x07]*\x07' /tmp/opencode-osc.log
82
+ grep -o $'\x1b]111\x07' /tmp/opencode-osc.log
83
+ ```
84
+
85
+ You should see an `OSC 11` on startup and an `OSC 111` on exit.
86
+
66
87
  ## Requirements
67
88
 
68
89
  - A terminal emulator that supports OSC 11/111 (most modern terminals do; Ghostty has quirks with theme detection after OSC 11/111 usage)
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { TuiPlugin, TuiPluginModule } from "@opencode-ai/plugin/tui";
2
2
  import { hexToRgb } from "@opentui/core";
3
+ import { writeSync } from "fs";
3
4
 
4
5
  const tui: TuiPlugin = async (api) => {
5
6
  const renderer = api.renderer;
@@ -9,16 +10,36 @@ const tui: TuiPlugin = async (api) => {
9
10
  original(color);
10
11
  const rgba = typeof color === "string" ? hexToRgb(color) : color;
11
12
 
12
- if (rgba.a > 0) {
13
- const hex = (n: number) => n.toString(16).padStart(2, "0");
14
- process.stdout.write(
15
- `\x1b]11;rgb:${hex(rgba.r)}/${hex(rgba.g)}/${hex(rgba.b)}\x07`,
16
- );
13
+ if (rgba && rgba.a > 0) {
14
+ const hex = (n: number) =>
15
+ Math.round(n * 255)
16
+ .toString(16)
17
+ .padStart(2, "0");
18
+ const seq = `\x1b]11;rgb:${hex(rgba.r)}/${hex(rgba.g)}/${hex(rgba.b)}\x07`;
19
+ try {
20
+ writeSync(1, seq);
21
+ } catch {
22
+ process.stdout.write(seq);
23
+ }
17
24
  }
18
25
  };
19
26
 
27
+ // ThemeProvider calls setBackgroundColor during initial render, which happens
28
+ // *before* plugins are loaded. After patching we must force a re-sync with the
29
+ // current theme background so the terminal matches on startup.
30
+ renderer.setBackgroundColor(api.theme.current.background);
31
+
20
32
  api.lifecycle.onDispose(() => {
21
- renderer.resetTerminalBgColor();
33
+ // Use writeSync(1, …) instead of process.stdout.write so the OSC 111
34
+ // sequence bypasses Node.js stream buffering and is flushed to the kernel
35
+ // before the process exits. renderer.resetTerminalBgColor() uses
36
+ // process.stdout.write, which can leave bytes in the stream buffer that
37
+ // never get drained when the event loop shuts down.
38
+ try {
39
+ writeSync(1, "\x1b]111\x07");
40
+ } catch {
41
+ renderer.resetTerminalBgColor();
42
+ }
22
43
  });
23
44
  };
24
45
 
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "oc-bg-color-sync",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
- "main": "index.ts",
6
5
  "exports": {
7
6
  "./tui": {
8
7
  "import": "./index.ts"