Skip to content

feat: detect first-run terminal theme#5385

Open
vegarsti wants to merge 2 commits into
mainfrom
first-run-theme-detection
Open

feat: detect first-run terminal theme#5385
vegarsti wants to merge 2 commits into
mainfrom
first-run-theme-detection

Conversation

@vegarsti

@vegarsti vegarsti commented Jun 4, 2026

Copy link
Copy Markdown
Member

We want users to get the first Pi theme to match their terminal (light/dark), so detect that by querying the terminal with OSC, and persist it to settings.

To test, check out the branch, set your terminal to a light or dark theme, and do PI_CODING_AGENT_DIR=$(mktemp -d) ./pi-test.sh (this works on macOS, should work on Ubuntu, and on a Windows machine with WSL). Then to verify go to /settings and search for theme to see what the detected theme was for the fresh Pi config.

Tested on

  • Kitty
  • Alacritty
  • Ghostty
  • Terminal.app
  • iTerm2
  • Windows Terminal
  • Ubuntu's default terminal

@vegarsti vegarsti requested a review from badlogic June 4, 2026 06:31
@badlogic badlogic added the inprogress Issue is being worked on label Jun 4, 2026
@badlogic

badlogic commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

I think this query should live in pi-tui rather than interactive-mode.

StdinBuffer already frames OSC responses as complete input events, but once this temporary listener unsubscribes, a late OSC 11 reply can still reach extension input listeners or the focused component. The TUI layer is the right place to own terminal queries and consume their replies before normal input dispatch.

Suggested shape:

  • Add a async TUI.queryTerminalBackgroundColor({ timeoutMs }) helper that writes OSC 11 ; ?.
  • TUI keeps an internal pendingOsc11BackgroundReplies counter.
  • Each query increments the counter.
  • TUI.handleInput() consumes strict OSC 11 background replies before addInputListener() / focused component dispatch when the counter is > 0.
  • Resolve the query promise with the parsed RGB if it arrives before timeout.
  • On timeout, resolve undefined, but do not decrement the counter. Decrement only when a matching OSC 11 reply is actually consumed.

That way late terminal replies are still removed from the input stream without needing a long-lived interactive-mode listener. Coding-agent can then keep only the RGB -> dark/light theme selection and settings persistence logic.

@badlogic

badlogic commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Oh, and please also test this in other terminals. I can't merge this before it's not tested in at least Kitty, Alacritty, Ghostty, Terminal.app, iTerm2, Windows Terminal, and Ubuntu's default terminal

@badlogic

badlogic commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

And we should rename detectTerminalBackground to detectTerminalBackgroundFromEnv

@vegarsti vegarsti force-pushed the first-run-theme-detection branch from 02ddb13 to 9323edb Compare June 5, 2026 08:26
@vegarsti vegarsti changed the title feat(coding-agent): detect first-run terminal theme feat: detect first-run terminal theme Jun 5, 2026
@vegarsti

vegarsti commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

Thank you so much for the review, this was super helpful. I've pushed a new attempt now trying to address your suggestions, and have tested on all the mentioned terminals on macOS. Will get someone to test on a Windows machine and a Ubuntu machine.

@vegarsti

vegarsti commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

I had to move RgbColor from pi-coding-agent to pi-tui - hope you agree with that being in this commit.

@vegarsti vegarsti force-pushed the first-run-theme-detection branch from 9323edb to 5a8316a Compare June 5, 2026 08:49
@badlogic

badlogic commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Looking good! Once we have Windows Terminal and Ubuntu tested, you can merge this.

@vegarsti

vegarsti commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

@windedge @qzsecfr Shamelessly tagging you as you have both submitted Windows issues recently - would any of you be able to help me out in testing this feature?

@windedge

windedge commented Jun 8, 2026

Copy link
Copy Markdown

@windedge @qzsecfr Shamelessly tagging you as you have both submitted Windows issues recently - would any of you be able to help me out in testing this feature?

Sure, I'd like to help out! However, I have some other commitments at the moment and will need about 2–3 days to get to this. If this issue is urgent, you might want to look for someone else to assist in the meantime.

@bugabinga

Copy link
Copy Markdown

hey, stupid question from the side lines: Wouldn't it be simpler to have just one default pi theme, that uses the ANSI color 16 index palette, so that it gets its colors from the terminal?

I attached my personal theme as reference. It seamlessly switches dark/light, whenever my terminal (wezterm) changes its palette.

I am curious, there must be trade-offs here, that I am not aware of.

terminal.json

@vegarsti

vegarsti commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

@windedge @qzsecfr Shamelessly tagging you as you have both submitted Windows issues recently - would any of you be able to help me out in testing this feature?

Sure, I'd like to help out! However, I have some other commitments at the moment and will need about 2–3 days to get to this. If this issue is urgent, you might want to look for someone else to assist in the meantime.

Thanks for the quick response! So far I haven't found anyone else to do it, so I'm totally fine waiting a few days!

@vegarsti

Copy link
Copy Markdown
Member Author

@windedge @qzsecfr Shamelessly tagging you as you have both submitted Windows issues recently - would any of you be able to help me out in testing this feature?

Sure, I'd like to help out! However, I have some other commitments at the moment and will need about 2–3 days to get to this. If this issue is urgent, you might want to look for someone else to assist in the meantime.

Hey @windedge did you have a chance to test this yet? 🤩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inprogress Issue is being worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants