@@ -352,6 +352,98 @@ TerminalSize Terminal::TryMeasureTerminalSize() {
352352return result;
353353}
354354
355+ bool ParseTerminalColor (TerminalColor &color,const char *buf,idx_t buflen) {
356+ /* Parse it.*/
357+ // expected format is: rgb:1e1e/1e1e/1e1e
358+ idx_t offset =0 ;
359+ // find "rgb:"
360+ for (; offset +4 < buflen; offset++) {
361+ if (memcmp (buf + offset, (const void *)" rgb:" ,4 ) ==0 ) {
362+ break ;
363+ }
364+ }
365+ // now parse the actual r/g/b values
366+ offset +=4 ;
367+ if (offset >= buflen) {
368+ return false ;
369+ }
370+ uint8_t values[3 ];
371+ memset (values,0 ,sizeof (values));
372+
373+ for (idx_t k =0 ; k <3 ; k++) {
374+ if (k >0 ) {
375+ // expected a "/"
376+ if (offset >= buflen || buf[offset] !=' /' ) {
377+ return false ;
378+ }
379+ offset++;
380+ }
381+ // parse the hexadecimal value
382+ // note that these values are from 0...65535, not from 0...255
383+ uint32_t value =0 ;
384+ idx_t end_pos = offset +4 ;
385+ for (; offset < end_pos; offset++) {
386+ if (offset >= buflen) {
387+ return false ;
388+ }
389+ auto c = buf[offset];
390+ if (c ==' /' ) {
391+ // found a slash early - done
392+ break ;
393+ }
394+ uint32_t current_value;
395+ if (c >=' A' && c <=' F' ) {
396+ current_value =10 + (c -' A' );
397+ }else if (c >=' a' && c <=' f' ) {
398+ current_value =10 + (c -' a' );
399+ }else if (c >=' 0' && c <=' 9' ) {
400+ current_value = c -' 0' ;
401+ }else {
402+ // unsupported hex value
403+ return false ;
404+ }
405+ value = value *16 + current_value;
406+ }
407+ // normalize from
408+ values[k] =static_cast <uint8_t >(value >>8 );
409+ }
410+ // found the r/g/b
411+ color.r = values[0 ];
412+ color.g = values[1 ];
413+ color.b = values[2 ];
414+ return true ;
415+ }
416+
417+ bool Terminal::TryGetBackgroundColor (TerminalColor &color) {
418+ int ifd = STDIN_FILENO;
419+ int ofd = STDOUT_FILENO;
420+
421+ if (Terminal::EnableRawMode () == -1 ) {
422+ return -1 ;
423+ }
424+
425+ bool success =false ;
426+ if (write (ofd," \x1b ]11;?\007 " ,7 ) ==7 ) {
427+ // Read the response: until \a or until we fill up our buffer
428+ char buf[64 ];
429+ idx_t i =0 ;
430+ while (i <sizeof (buf) -1 ) {
431+ if (read (ifd, buf + i,1 ) !=1 ) {
432+ break ;
433+ }
434+ if (buf[i] ==' \a ' ) {
435+ break ;
436+ }
437+ i++;
438+ }
439+ buf[i] =' \0 ' ;
440+
441+ success =ParseTerminalColor (color, buf, i);
442+ }
443+ Terminal::DisableRawMode ();
444+ return success;
445+ }
446+
355447/* Try to get the number of columns in the current terminal, or assume 80
356448 * if it fails.*/
357449TerminalSizeTerminal::GetTerminalSize () {