Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TTY detection #47

Merged
merged 10 commits into from
Aug 5, 2019
Merged

Add TTY detection #47

merged 10 commits into from
Aug 5, 2019

Conversation

metiulekm
Copy link
Contributor

Closes #46.

Seems to work but please recheck.

Now, 'pPrint' and dark/light background variations will only color the
output if they are writing to a TTY. Also, new functions, the
'pPrintForceColor' family, have been added, that skip this check, like
'pPrint' used to do.
-- Helpers
------------------------------------------

pStringTTYIO :: String -> IO Text
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it but it works, and I couldn't find a better solution quickly, and I wanted to push something out before going to sleep lol

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable. I changed it to check stderr instead of stdout, since it looks like the trace functions output to stderr. But other than that this looks good.

pStringTTYIO v = checkTTY defaultOutputOptionsDarkBg <&> \o -> pStringOpt o v

pStringTTY :: String -> Text
pStringTTY = unsafePerformIO . pStringTTYIO
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in the commit message, unsafePerformIO seems unavoidable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the trace functions are using unsafePerformIO internally, so this seems reasonable:

http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Debug.Trace.html#trace

@cdepillabout
Copy link
Owner

@metiulekm Thanks for this PR!

I cleaned this up a little bit in the last couple of commits. Could you do one final review of this? If you sign off on it, I'll merge it in and make a new release.

src/Text/Pretty/Simple.hs Outdated Show resolved Hide resolved
@metiulekm
Copy link
Contributor Author

I've removed the hCheckTTY export, and it LGTM :)

@cdepillabout cdepillabout merged commit 6047dab into cdepillabout:master Aug 5, 2019
@cdepillabout
Copy link
Owner

Thanks!

Released on Hackage as pretty-simple-3.0.0.0:

http://hackage.haskell.org/package/pretty-simple-3.0.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pPretty detected tty for whether to enable color or not
2 participants