Skip to content

attempts to fix ANSI sequences leaking#42

Merged
nxtcoder17 merged 1 commit intomasterfrom
fix/ANSI-sequences-leaking
Feb 17, 2026
Merged

attempts to fix ANSI sequences leaking#42
nxtcoder17 merged 1 commit intomasterfrom
fix/ANSI-sequences-leaking

Conversation

@nxtcoder17
Copy link
Copy Markdown
Owner

@nxtcoder17 nxtcoder17 commented Feb 17, 2026

resolves #41

attempts to fix ANSI sequences leaking because of termenv's background color check

Summary by Sourcery

Detect terminal background once at startup and use an environment variable to control theming, preventing ANSI escape sequence leakage.

New Features:

  • Introduce RUNFILE_THEME environment variable to control light and dark theming for command output and syntax highlighting.

Bug Fixes:

  • Avoid leaking ANSI response sequences by moving terminal background detection to the main entrypoint before subprocesses run.

Enhancements:

  • Simplify theme selection in task rendering by relying on RUNFILE_THEME instead of performing on-demand background checks.

Build:

  • Promote pty and x/sync to direct dependencies in go.mod.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Feb 17, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Centralizes terminal theme detection at process startup using termenv and an environment variable, then switches downstream rendering logic to rely on that environment variable instead of querying termenv directly, promoting creack/pty and x/sync to direct dependencies.

Sequence diagram for centralized terminal theme detection and usage

sequenceDiagram
    actor User
    participant RunMain as cmd_run_main
    participant Termenv as termenv_Output
    participant OS as os_Stdout
    participant Env as os_Environ
    participant Resolver as pkg_runfile_resolver_task

    User->>RunMain: start run command
    RunMain->>Env: getenv RUNFILE_THEME
    alt RUNFILE_THEME not set
        RunMain->>Termenv: NewOutput(os_Stdout)
        Termenv->>OS: query background
        OS-->>Termenv: dark or light
        Termenv-->>RunMain: HasDarkBackground result
        RunMain->>Env: setenv RUNFILE_THEME dark_or_light
    else RUNFILE_THEME already set
        RunMain-->>RunMain: skip theme detection
    end

    User->>Resolver: execute task that prints command
    Resolver->>Env: getenv RUNFILE_THEME
    alt RUNFILE_THEME == light
        Resolver-->>Resolver: set borderColor light
        Resolver-->>Resolver: set colorscheme xcode
    else RUNFILE_THEME != light
        Resolver-->>Resolver: set borderColor dark
        Resolver-->>Resolver: set colorscheme catppuccin_macchiato
    end
    Resolver-->>User: themed command output
Loading

Flow diagram for theme configuration via environment variable

flowchart LR
    subgraph Startup
        A[cmd_run_main] --> B{RUNFILE_THEME set?}
        B -- No --> C[termenv NewOutput os_Stdout]
        C --> D[HasDarkBackground]
        D --> E{Background is dark?}
        E -- Yes --> F[theme = dark]
        E -- No --> G[theme = light]
        F --> H[setenv RUNFILE_THEME]
        G --> H[setenv RUNFILE_THEME]
        B -- Yes --> I[skip detection]
    end

    subgraph Runtime
        J[pkg_runfile_resolver_task printCommand]
        J --> K[getenv RUNFILE_THEME]
        K --> L{RUNFILE_THEME == light?}
        L -- Yes --> M[borderColor light\ncolorscheme xcode]
        L -- No --> N[borderColor dark\ncolorscheme catppuccin_macchiato]
    end

    H --> K
    I --> K
Loading

File-Level Changes

Change Details Files
Replace per-call dark-theme detection in task rendering with an environment-variable–driven theme selection.
  • Remove local isDarkTheme helper and associated sync.Once state in task rendering code.
  • Switch border color selection in printCommand to check RUNFILE_THEME == "light".
  • Switch Chroma colorscheme selection in printCommand to use RUNFILE_THEME == "light" instead of termenv background checks.
pkg/runfile/resolver/task.go
Initialize RUNFILE_THEME at CLI startup using termenv to detect background, avoiding ANSI response leakage.
  • Import termenv in main CLI entrypoint.
  • On startup, if RUNFILE_THEME is unset, detect dark vs light terminal background via termenv.NewOutput(os.Stdout).HasDarkBackground().
  • Set RUNFILE_THEME to "dark" or "light" based on detection result before any subprocesses run.
cmd/run/main.go
Adjust module dependencies to reflect new usage of pty and x/sync as direct requirements.
  • Promote github.com/creack/pty from indirect to direct dependency in go.mod.
  • Promote golang.org/x/sync from indirect to direct dependency in go.mod.
  • Update go.sum accordingly (not shown in diff body).
go.mod
go.sum

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new RUNFILE_THEME logic is now duplicated between printCommand and main; consider centralizing theme detection/access in a small helper (e.g., GetTheme() or IsLightTheme()) to avoid string/env-name drift.
  • Automatically mutating RUNFILE_THEME as a process-wide environment variable in main could be surprising for callers; if this is used as a library entry point elsewhere, consider passing the resolved theme through configuration or context instead of setting env.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `RUNFILE_THEME` logic is now duplicated between `printCommand` and `main`; consider centralizing theme detection/access in a small helper (e.g., `GetTheme()` or `IsLightTheme()`) to avoid string/env-name drift.
- Automatically mutating `RUNFILE_THEME` as a process-wide environment variable in `main` could be surprising for callers; if this is used as a library entry point elsewhere, consider passing the resolved theme through configuration or context instead of setting env.

## Individual Comments

### Comment 1
<location> `cmd/run/main.go:43-48` </location>
<code_context>

+	// Detect terminal theme early, before any subprocesses run.
+	// This prevents ANSI response sequences from leaking onto stdin.
+	if os.Getenv("RUNFILE_THEME") == "" {
+		theme := "dark"
+		if !termenv.NewOutput(os.Stdout).HasDarkBackground() {
+			theme = "light"
+		}
+		os.Setenv("RUNFILE_THEME", theme)
+	}
+
</code_context>

<issue_to_address>
**issue (bug_risk):** Using os.Stdout for theme detection can misbehave when stdout is redirected or piped.

`termenv.NewOutput(os.Stdout).HasDarkBackground()` sends probe escape sequences to stdout. If the command’s stdout is piped or redirected, those sequences will pollute the output and the theme may not match the user’s actual terminal. Prefer probing a TTY-backed stream (e.g., stderr) or first checking `term.IsTerminal(int(os.Stdout.Fd()))` and skipping detection when stdout is non-interactive.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread cmd/run/main.go Outdated
@nxtcoder17 nxtcoder17 force-pushed the fix/ANSI-sequences-leaking branch from d0e9f97 to a36773b Compare February 17, 2026 16:15
@nxtcoder17 nxtcoder17 linked an issue Feb 17, 2026 that may be closed by this pull request
@nxtcoder17 nxtcoder17 merged commit 58e2f5d into master Feb 17, 2026
1 check passed
@nxtcoder17 nxtcoder17 deleted the fix/ANSI-sequences-leaking branch February 17, 2026 16:20
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.

[Bug] ANSI escape sequences bleeding into the terminal stdout

1 participant