charmbracelet / lipgloss

Style definitions for nice terminal layouts 👄
MIT License
7.86k stars 222 forks source link

VS Code test-output terminal does not render styles #74

Closed NiloCK closed 1 year ago

NiloCK commented 2 years ago

Like any reasonable person with a three-year-old, I'm making a TUI Connect-4 clone for local network play (toddlers cannot physically prevent you from making a blocking move when they are in another room).

Testing my work-in-progress had me seriously confused:

c4Colors

Turns out that the terminal which displays the output from this inline test-running button (maybe the entire integrated test runner) doesn't display any colours. (You would certainly know better than me how to characterize the behaviour).

Note that this is not the case for VSCode's more general terminal:

c4colorsWorking

Suggestions:

Nice library. Enjoying the renewed interest and vigor in modernizing cli / tui tooling. Good luck!

meowgorithm commented 2 years ago

Thanks for the detailed report. We can definitely use some better docs around this.

The reason this happens is because Lip Gloss automatically degrades colors to the best available option in the given terminal. Because Go tests run in a sub-process, they're not attached to a TTY and thus Lip Gloss strips color output entirely.

However! You can force a color profile in your tests with SetColorProfile.

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

lipgloss.SetColorProfile(termenv.TrueColor)

P.S. This project looks really cool and we absolutely love it when people make games with our stuff. Please do let us know when it's available (twitter, fediverse, email, slack).

mfridman commented 2 years ago

+1 on updating the docs. I had the opposite problem where everything worked fine locally (terminal and vs code), but when pushed to GitHub there was no color output in CI (GitHub Actions)

Adding an explicit color profile fixed the issue, although I suspect this may be an issue on my end.

CleanShot 2022-05-21 at 14 00 45@2x CleanShot 2022-05-21 at 14 01 08@2x
till commented 1 year ago

Hey 👋🏼 — I think I am in a similar situation like @mfridman.

When I let lipgloss auto-detect the environment, then I have problems on GHA to use the tool in the steps. It stops and hangs — for as long as the workflow can run.

I changed the output of my tool to return json, which bypasses lipgloss and bubbletea entirely, then the steps complete within seconds. So it seems like detection to check for a TTY (or terminal) is needed.

I am not sure if this detection needs to be in lipgloss or bubbletea (or both).

I am currently trying this code to configure it:

    if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
        // we can be extra fancy since this is a terminal
    } else {
        // probably a non-interactive session in runner, pipe or CI tool
        lipgloss.SetColorProfile(termenv.Ascii)
    }

I guess at the very least, I hope SetColorProfile doesn't go away in future versions. :)