Skip to content

fix(palette): querySpecialColors resistent to term emulators with noi… - #765

Merged
kommander merged 2 commits into
anomalyco:mainfrom
MaxDillon:main
Mar 2, 2026
Merged

kommander merged 2 commits into
anomalyco:mainfrom
MaxDillon:main

Conversation

@MaxDillon

Copy link
Copy Markdown
Contributor

Issue for this PR

#705

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Opentuin getPallette function for querySpecialColors has an idleTimer meant to exit out before the explicit timeout if the terminal does not provide all of the color escape sequences 10-19. The idle timer resets if any escape sequence triggers the onData function again.

This is an issue for terminal emulators like ghostty which are very noisy with the escape sequences they send. Here we only allow the timeout to reset if a response conforming to OSC_SPECIAL_RESPONSE is read in the buffer.

This is possibly overly conservative. If we were being more aggressive, we could only reset the idle timer when an entry in results gets updated from a null value. As it stands, this improves start time of getPallette in ghostty from the max timeout to almost negligable

How did you verify your code works?

  • I tested locally with the following simple script
#!/usr/bin/env bun

import { createCliRenderer, TextRenderable } from "../index"
import { setupCommonDemoKeys } from "./lib/standalone-keys"

const startTime = Date.now()

// 1. Create renderer
const renderer = await createCliRenderer({
  exitOnCtrlC: true,
  targetFps: 60,
})

// 2. Start automatic render loop
renderer.auto()

// 3. Add trivial renderable
const text = new TextRenderable(renderer, {
  id: "placeholder",
  content: "Renderer active… (toggle stdout with `)",
  left: 0,
  top: 0,
})
renderer.root.add(text)

// 4. Hook up common demo keys (including stdout toggle)
setupCommonDemoKeys(renderer)

// 5. Request palette
const palette = await renderer.getPalette({ timeout: 5000, size: 16 })

console.log("time elapsed", Date.now() - startTime)
  • Without fix: [17:54:05] [LOG] 'time elapsed' 5014
  • With fix: [17:54:28] [LOG] 'time elapsed' 220

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@kommander

Copy link
Copy Markdown
Collaborator

For that script without the proposed fix I get ~150ms in Ghostty on main, with all colors resolved.
Screenshot 2026-03-02 at 21 31 16

@kommander

Copy link
Copy Markdown
Collaborator

Mhh... I just ran it again and now it's 5000ms

Screenshot 2026-03-02 at 21 35 03

@kommander

Copy link
Copy Markdown
Collaborator

Okay I consistently get ~200ms with this fix it seems 👍

@pkg-pr-new

pkg-pr-new Bot commented Mar 2, 2026 •

Copy link
Copy Markdown
@opentui/core

npm i https://pkg.pr.new/@opentui/core@cf50aa9

@opentui/react

npm i https://pkg.pr.new/@opentui/react@cf50aa9

@opentui/solid

npm i https://pkg.pr.new/@opentui/solid@cf50aa9

@opentui/core-darwin-arm64

npm i https://pkg.pr.new/@opentui/core-darwin-arm64@cf50aa9

@opentui/core-darwin-x64

npm i https://pkg.pr.new/@opentui/core-darwin-x64@cf50aa9

@opentui/core-linux-arm64

npm i https://pkg.pr.new/@opentui/core-linux-arm64@cf50aa9

@opentui/core-linux-x64

npm i https://pkg.pr.new/@opentui/core-linux-x64@cf50aa9

@opentui/core-win32-arm64

npm i https://pkg.pr.new/@opentui/core-win32-arm64@cf50aa9

@opentui/core-win32-x64

npm i https://pkg.pr.new/@opentui/core-win32-x64@cf50aa9

commit: cf50aa9

@kommander
kommander merged commit 865a84e into anomalyco:main Mar 2, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants