joerdav / xc

Markdown defined task runner.
https://xcfile.dev/
MIT License
1.04k stars 25 forks source link

Pass colorized output to log from underlying command #108

Closed nsbruce closed 6 months ago

nsbruce commented 7 months ago

If the task being run has a colorized output when run without xc, I think xc should pass that colorized output through.

Trivial example:

20231213_09h58m08s_grim

A more valid example is that when using testing suites like pytest where successful tests are shown in green and failures in red.

joerdav commented 7 months ago

@stephenafamo I think this may be related to the log writer you implemented. Let me know if you have time to look at this :)

stephenafamo commented 6 months ago
image

Colorized output works. I suspect the issue is somehow in checking if the terminal supports colors, and this is likely to be more of an issue with the shell interpreter than with the log writer.

To prevent using the log writer, try adding interactive: true to the task definition and see what it outputs @nsbruce

stephenafamo commented 6 months ago

I got some more time to look at this, and it is indeed the logwriter.

The issue is that when checking if output should be colourized, commands like ls check if the output stream is a terminal.
From my first look around, there is no way to fake this, so I am conflicted about how best to proceed.

The most likely solution is that these tools decide to "FORCE_COLOR".

joerdav commented 6 months ago

I'm also under the impression that we cannot fake a terminal in this case. What I'm thinking is that we could set CLICOLOR_FORCE=1 so long as NO_COLOR is not set.

stephenafamo commented 6 months ago

I'm also under the impression that we cannot fake a terminal in this case. What I'm thinking is that we could set CLICOLOR_FORCE=1 so long as NO_COLOR is not set.

I think we should set both CLICOLOR_FORCE and FORCE_COLOR but first check if the caller is a terminal.

joerdav commented 6 months ago

Good point, will look into that.

joerdav commented 6 months ago

Created a fix branch for this, would love to see if it works for people before merging!

nsbruce commented 6 months ago

I just cloned main, switched to term-colors branch, ran go install ./cmd/xc and am getting the same result as when I posted the issue.

joerdav commented 6 months ago

That's unfortunate, is this still the ls command?

nsbruce commented 6 months ago

Hey I apologize but it is working. To make troubleshooting easier I just spun up a MWE to show you it not working but must have erred the other day. It is properly colorizing now.

from ubuntu:22.04                                                                                                            

run apt-get update && apt-get -y install git curl                                                                            

run rm -rf /usr/local/go && \                                                                                                
  curl -L -o go1.21.6.linux-386.tar.gz https://go.dev/dl/go1.21.6.linux-386.tar.gz && \                                
  tar -C /usr/local -xzf go1.21.6.linux-386.tar.gz                                                                     

env PATH $PATH:/usr/local/go/bin                                                                                             
env GOBIN /usr/local/go/bin                                                                                                  

run git clone https://github.com/joerdav/xc && \                                                                             
cd xc && \                                                                                                           
  git switch term-colours && \                                                                                         
   git pull && \                                                                                                        
   go install ./cmd/xc                                                                                                  

run echo '## Tasks\n\n### ls\n\n```bash\nls --color\n```' > /README.md                                                       

entrypoint cd / && ls --color && xc ls

Then running docker build -t xc-test . && docker run --rm -it xc-test

20240115_08h01m49s_grim

Thanks for the work on this! I look forward to the merge.