João Paulin
João Paulin

Reputation: 73

Get color of stdout content

I'm making a simple program in Tcl/Tk that uses git stash list, git stash show stash@{n} and git stash show -p stash@{n} to populate a listbox and show the content of the stash in a text widget, when one is selected.

However, this git commands outputs a colored result in stdout.

How to get the color of stdout and reproduce them in the text widget?

Upvotes: 0

Views: 194

Answers (1)

andy mango
andy mango

Reputation: 1551

In my quick read of git commands, it seems that the default configuration is to color the output when it goes to a terminal. Git is apparently smart enough to exclude the color control codes when output is going to a pipe or file since there is nothing there to interpret such codes. It also seems that you can include a --color option to force the color control codes into the output. So as I see it, you have a couple of options:

  1. Take the plain output, without color control codes, off of a pipe and add your own colors in the same style that git does.
  2. Force the color control codes into the output and parse them out to determine how to tag the text given to a text widget.

I think the second option is probably more difficult to implement, but will be more resilient to changes in the way git colors output. Of course, the way the text widget colors text is to annotate the text with a tag and then set the properties for how the tagged text is displayed.

If you wish to post some code showing your approach and how far you have gotten, I'm sure there will be folks here to help you along.

Upvotes: 1

Related Questions