shiena / ansicolor

Ansicolor library provides color console in Windows as ANSICON for Golang.
https://pkg.go.dev/github.com/shiena/ansicolor
MIT License
229 stars 40 forks source link

Determine correct window handle #12

Open maja42 opened 5 years ago

maja42 commented 5 years ago

When setting the terminal color, ansicolor uses the file handle syscall.Stdout for the windows system calls.

In my use case, this is not sufficient. I am compiling my go application as a windows GUI application (no terminal) and, if the command-line argument "-console" is provided, I allocate a new console.

I'm using the following syscalls for that:

procAttachConsole = kernel32.MustFindProc("AttachConsole")
procAllocConsole  = kernel32.MustFindProc("AllocConsole")
procFreeConsole   = kernel32.MustFindProc("FreeConsole")

As a result, the syscall.stdout handle (=1) is not correct, but there is no way to configure another window handle.

maja42 commented 5 years ago

I managed to solve this issue by overwriting syscall.Stdout, syscall.Stderr and syscall.Stdin to the streams acquired after creating a new terminal window (Before, my code only overwrote os.Stdout/err/in).

I suppose it is not an intended use case to overwrite these exported variables, but on the other hand go itself doesn't seep to support attaching different terminal windows at runtime.

So this issue can be closed from my side.