Skip to content

Add TTY detection#47

Merged
cdepillabout merged 10 commits intocdepillabout:masterfrom
metiulekm:master
Aug 5, 2019
Merged

Add TTY detection#47
cdepillabout merged 10 commits intocdepillabout:masterfrom
metiulekm:master

Conversation

@metiulekm
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@metiulekm
Copy link
Copy Markdown
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
Copy Markdown
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