Search code examples
goasciistdin

Ouput escaped sequences on Windows


Is there something like colorama (python) for golang? Not just to output color, but to be able to just write print("\033[1;32mMy text in green\033[0m") and get the colored text?

I pipe a string from a program (say git log --color=always) which output escaped sequences, and I want to be able to output it with the colors too.

$ git log --color=always | go run prog.go

How can I make the escaped sequences work? (I get these kinds of thing ?[1;32 where the color was supposed to be set).

Is there no better solution than parsing the string and use color for example?


Solution

  • https://github.com/shiena/ansicolor did what I needed. Almost perfect.

    writer := ansicolor.NewAnsiColorWriter(os.Stdout)
    fmt.Fprint(writer, "\033[1;32mMy text in green\033[0m")
    # that works!