-1

Using hints from the answers to this question, I can easily get the color value of the current terminal if I run the command directly in shell

But I need to get this value inside the Go program

Apparently, the problem is that the command from the answer calls a subprocess from which the result should appear in stdout, but I can’t capture it in any way

System info

macOS Monterey 12.6 (21G115)  
Apple M1 Pro  
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
go version go1.19.1 darwin/arm64

I am trying to do it with this code

cmd := exec.Command("sh", "-c", `echo '\033]11;?\007'`)
out := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = stderr
err := cmd.Run()
if err != nil {
   panic(err)
}

println("result", out.String(), "end")

output is

bash-3.2$ go run .
result
end
^\[\]11;rgb:2b2b/2b2b/2b2b^Gbash-3.2$ 11;rgb:2b2b/2b2b/2b2b

I tried to add sleep time in bash and in go code. I tried to use stdout scanning. I tried to use script from the answer as executable

bash-3.2$ ./test.sh 
rgb:2b2b/2b2b/2b2b
bash-3.2$ ./test.sh > test.log; cat test.log 

bash-3.2$ 11;rgb:2b2b/2b2b/2b2b
bash-3.2$ ./test.sh > test.log; cat test.log 

bash-3.2$ ./test.sh > test.log; cat test.log | grep 2b
bash-3.2$ ./test.sh  | grep 2b
bash-3.2$ ./test.sh  | grep rgb
bash-3.2$ cat test.log 

^[]11;rgb:2b2b/2b2b/2b2b^[\bash-3.2$ 11;rgb:2b2b/2b2b/2b2b
bash-3.2$ cat test.log | grep 2b
Konorlevich
  • 65
  • 1
  • 4
  • 2
    You are querying the terminal's capabilities, but there is no guarantee that the subprocess is connected to a terminal. In fact, it is guaranteed not to be. – tripleee Oct 26 '22 at 17:54
  • 4
    If your Go program _is_ connected to a terminal, it could very well output those control codes itself, without `echo` or a shell. Probably look for a `curses` library to manage it for you. – tripleee Oct 26 '22 at 17:56
  • @triplee what exactly do you mean by “Go program is connected to a terminal”? I'm not sure I understand well.. – Konorlevich Oct 27 '22 at 08:28
  • If you run it from a terminal window with standard input and standard output connected to this terminal, the condition is true. Otherwise (you're not running it in a terminal, or you are redirecting standard input or standard output) it's not, and your `echo` (or any similar attempt) will not do anything useful. – tripleee Oct 27 '22 at 08:36
  • @triplee I’ve tried dozens of different approaches And for now I don’t understand, which channel terminal uses for answer delivery stdout/stderr is empty and result appeared in cli just as new command.. – Konorlevich Oct 27 '22 at 10:13

1 Answers1

0

It turned out, that no need to use such bash hacks for term background color quering

package main

import (
    "github.com/muesli/termenv"
)

func main() {
    println(termenv.BackgroundColor().Sequence(true))
}

Works well for bash and zsh

bash-3.2$ go run .
48;2;43;43;43

43;43;43 is RGB color of the background

BUT: It doesn’t work for tmux. The same code returns

> go run .
40

Here is an explanation:

Unavailable as multiplexers (like tmux or screen) can be connected to multiple terminals (with different color settings) at the same time.

Konorlevich
  • 65
  • 1
  • 4