Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cli/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,23 @@ async function main(): Promise<void> {
// sequence to the tty ('CON' on win32) and toggles stdin raw mode for a
// reply that cannot come. That branch resolves the same env/fallback
// evidence the query path fell back to, without touching stdin.
// Skip the OSC query for non-interactive early-exit invocations and when
// color is disabled: those print and exit before any terminal reply can be
// read, which would leak the OSC 11 response into the shell's input buffer.
const oscNonInteractive =
Boolean(process.env.NO_COLOR) ||
(Array.isArray(process.argv) &&
process.argv.some(
(arg) =>
arg === '--version' ||
arg === '-v' ||
arg === '--help' ||
arg === '-h',
))
const oscQuerySupported =
process.stdin.isTTY === true && process.platform !== 'win32'
!oscNonInteractive &&
process.stdin.isTTY === true &&
process.platform !== 'win32'
const oscThemePromise: Promise<'dark' | 'light' | null> = oscQuerySupported
? detectTerminalTheme()
: Promise.resolve(detectOscEvidenceTheme())
Expand Down
1 change: 1 addition & 0 deletions cli/src/types/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type CliEnv = BaseEnv & {
ZED_TERM?: string
ZED_SHELL?: string
COLORTERM?: string
NO_COLOR?: string

// VS Code family detection
VSCODE_THEME_KIND?: string
Expand Down
1 change: 1 addition & 0 deletions cli/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const getCliEnv = (): CliEnv => ({
ZED_TERM: process.env.ZED_TERM,
ZED_SHELL: process.env.ZED_SHELL,
COLORTERM: process.env.COLORTERM,
NO_COLOR: process.env.NO_COLOR,

// VS Code family detection
VSCODE_THEME_KIND: process.env.VSCODE_THEME_KIND,
Expand Down
21 changes: 21 additions & 0 deletions cli/src/utils/terminal-color-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ const GLOBAL_OSC_TIMEOUT_MS = 2000 // Global timeout for entire detection proces
export function terminalSupportsOSC(
env: CliEnv = getCliEnv(),
): boolean {
// Color is explicitly disabled: never query the terminal (and never risk an
// OSC reply leaking into the shell on early exit).
if (env.NO_COLOR || process.env.NO_COLOR) {
return false
}

// Non-interactive early-exit invocations (version/help) print and exit
// before any terminal reply can be read, so an OSC query would leak its
// response into the shell's input buffer. Skip the query entirely.
if (
Array.isArray(process.argv) &&
process.argv.some((arg) =>
arg === '--version' ||
arg === '-v' ||
arg === '--help' ||
arg === '-h',
)
) {
return false
}

const term = env.TERM || ''
const termProgram = env.TERM_PROGRAM || ''

Expand Down