@@ -368,33 +368,64 @@ def _query_background_color() -> BackgroundType:
368368 # Wait for response with timeout
369369 ready_streams : List [Any ] = select .select ([sys .stdin ], [], [], 0.1 )[0 ]
370370 if ready_streams :
371- response : str = sys .stdin .read (50 ) # Read up to 50 chars
371+ # Read available data without blocking
372+ response : str = ""
373+ try :
374+ # Read character by character with timeout to avoid blocking
375+ import fcntl
376+ import os
377+
378+ # Set stdin to non-blocking mode
379+ fd = sys .stdin .fileno ()
380+ fl = fcntl .fcntl (fd , fcntl .F_GETFL )
381+ fcntl .fcntl (fd , fcntl .F_SETFL , fl | os .O_NONBLOCK )
382+
383+ # Read up to 50 chars with timeout
384+ for _ in range (50 ):
385+ ready = select .select ([sys .stdin ], [], [], 0.01 )[0 ]
386+ if not ready :
387+ break
388+ char = sys .stdin .read (1 )
389+ if not char :
390+ break
391+ response += char
392+ # Stop if we get the expected terminator
393+ if response .endswith ('\033 \\ ' ):
394+ break
395+
396+ # Restore blocking mode
397+ fcntl .fcntl (fd , fcntl .F_SETFL , fl )
398+
399+ except (OSError , ImportError ):
400+ # Fallback to simple read if fcntl is not available
401+ response = sys .stdin .read (50 )
372402
373403 # Parse response: \033]11;rgb:rrrr/gggg/bbbb\033\\
374- rgb_match = re .search (
375- r"rgb:([0-9a-f]+)/([0-9a-f]+)/([0-9a-f]+)" , response
376- )
377- if rgb_match :
378- r : str
379- g : str
380- b : str
381- r , g , b = rgb_match .groups ()
382- # Convert hex to int and calculate brightness
383- red : int = int (r [:2 ], 16 ) if len (r ) >= 2 else 0
384- green : int = int (g [:2 ], 16 ) if len (g ) >= 2 else 0
385- blue : int = int (b [:2 ], 16 ) if len (b ) >= 2 else 0
386-
387- # Calculate perceived brightness using standard formula
388- brightness : float = (red * 299 + green * 587 + blue * 114 ) / 1000
389-
390- # Restore terminal settings
391- termios .tcsetattr (sys .stdin , termios .TCSADRAIN , old_settings )
392-
393- return (
394- BackgroundType .LIGHT
395- if brightness > 127
396- else BackgroundType .DARK
404+ if response : # Only proceed if we got a response
405+ rgb_match = re .search (
406+ r"rgb:([0-9a-f]+)/([0-9a-f]+)/([0-9a-f]+)" , response
397407 )
408+ if rgb_match :
409+ r : str
410+ g : str
411+ b : str
412+ r , g , b = rgb_match .groups ()
413+ # Convert hex to int and calculate brightness
414+ red : int = int (r [:2 ], 16 ) if len (r ) >= 2 else 0
415+ green : int = int (g [:2 ], 16 ) if len (g ) >= 2 else 0
416+ blue : int = int (b [:2 ], 16 ) if len (b ) >= 2 else 0
417+
418+ # Calculate perceived brightness using standard formula
419+ brightness : float = (red * 299 + green * 587 + blue * 114 ) / 1000
420+
421+ # Restore terminal settings
422+ termios .tcsetattr (sys .stdin , termios .TCSADRAIN , old_settings )
423+
424+ return (
425+ BackgroundType .LIGHT
426+ if brightness > 127
427+ else BackgroundType .DARK
428+ )
398429
399430 # Restore terminal settings
400431 if old_settings is not None :
0 commit comments