@@ -4,6 +4,8 @@ import { dirname, join } from 'path'
44
55import { detectShell } from './detect-shell'
66import { logger } from './logger'
7+ import { detectTerminalTheme } from './terminal-color-detection'
8+ import { withTerminalInputGuard } from './terminal-input-guard'
79
810import type { MarkdownPalette } from './markdown-renderer'
911import type {
@@ -1143,9 +1145,8 @@ process.on('SIGUSR2', () => {
11431145 * Initialize OSC theme detection with a one-time check
11441146 * Runs in a separate process to avoid blocking and hiding I/O from user
11451147 */
1146- export async function initializeOSCDetection ( ) : Promise < void > {
1147- // Don't await - fire and forget
1148- detectOSCInBackground ( )
1148+ export function initializeOSCDetection ( ) : void {
1149+ void detectOSCInBackground ( )
11491150}
11501151
11511152/**
@@ -1158,48 +1159,18 @@ async function detectOSCInBackground() {
11581159 return
11591160 }
11601161
1161- try {
1162- // Spawn self with internal flag to run OSC detection
1163- // Use stored CLI entry point path
1164- const cliEntryPoint = ( globalThis as any ) . __CLI_ENTRY_POINT || Bun . main
1165- const proc = Bun . spawn ( {
1166- cmd : [ process . execPath , cliEntryPoint , '--internal-osc-detect' ] ,
1167- stdio : [ 'ignore' , 'pipe' , 'ignore' ] , // pipe stdout only, ignore stdin/stderr
1168- timeout : 2000 , // 2 second timeout to allow for module loading
1169- env : {
1170- ...process . env ,
1171- __INTERNAL_OSC_DETECT : '1' , // Suppress console output
1172- } ,
1173- } )
1174-
1175- // Read result from stdout
1176- const text = await new Response ( proc . stdout ) . text ( )
1177-
1178- // Extract JSON from output (ignore any console.log noise)
1179- // Look for the last line that starts with { or contains "theme"
1180- const lines = text . trim ( ) . split ( '\n' )
1181- let jsonLine = ''
1182- for ( let i = lines . length - 1 ; i >= 0 ; i -- ) {
1183- const line = lines [ i ] . trim ( )
1184- if ( line . startsWith ( '{' ) && line . includes ( 'theme' ) ) {
1185- jsonLine = line
1186- break
1162+ await withTerminalInputGuard ( async ( ) => {
1163+ try {
1164+ const theme = await detectTerminalTheme ( )
1165+ if ( theme ) {
1166+ oscDetectedTheme = theme
1167+ recomputeSystemTheme ( 'osc-inline' )
11871168 }
1169+ } catch ( error ) {
1170+ logger . warn (
1171+ { error : error instanceof Error ? error . message : String ( error ) } ,
1172+ 'OSC detection failed' ,
1173+ )
11881174 }
1189-
1190- if ( ! jsonLine ) return
1191-
1192- const result = JSON . parse ( jsonLine ) as { theme : 'dark' | 'light' | null }
1193-
1194- if ( result . theme ) {
1195- oscDetectedTheme = result . theme
1196- // Trigger theme recomputation to apply OSC-detected theme
1197- recomputeSystemTheme ( 'osc-background' )
1198- }
1199- } catch ( error ) {
1200- logger . warn (
1201- { error : error instanceof Error ? error . message : String ( error ) } ,
1202- 'OSC detection failed' ,
1203- )
1204- }
1175+ } )
12051176}
0 commit comments