muesli4 / table-layout

Layout data in grids and pretty tables. Provides a lot of tools to get the cell formatting right (positional alignment, alignment on specific characters and limiting of cell width)..
BSD 3-Clause "New" or "Revised" License
37 stars 11 forks source link

Improve information about example usage with ansi-terminal #55

Closed h7x4 closed 1 year ago

h7x4 commented 1 year ago

Hello! This is just a question.

I'm having some problems trying to use this package with ansi-terminal. In the README at Improving Readability of Grids, it says the following:

Big grids are usually not that readable. To improve their readability, two functions are provided:

  • altLines will apply the given function in an alternating pattern. E.g., color every second row grey.
  • checkeredCells will checker cells with 2 different functions. A good way to use this would be the ansi-terminal package, provided you are using a terminal to output your text.

However, ansi-terminal uses IO actions as their means to change color:

main :: IO ()
main = do
  setSGR [ SetColor Foreground Vivid Red
         , SetColor Background Vivid Blue ]
  putStrLn "Red-On-Blue"
  setSGR [Reset]  -- Reset to default colour scheme
  putStrLn "Default colors."

Which in turn causes the type of any colored text to be IO (). Unless there is some kind of table printer where I can provide a function String -> IO () per cell during printing the table (which rules out gridString and gridLines), I'm not able to figure out any other way for these to cooporate.

Am I missing something, or is this just fundamentally impossible? If it does somehow work, I would really like an example in the README.

muesli4 commented 1 year ago

Hello @h7x4,

you can use the following to get character sequences that cause the expected behavior:

> setSGRCode [SetColor Foreground Dull Red]
"\ESC[31m"
> setSGRCode [Reset]
"\ESC[0m"

This should be used in combination with Text.Layout.Cell.Formatted. For an example see my comment here. With the version on GitHub more complex nested formatting is possible. The release is still pending but may be ready soon.

If you don't mind, I will keep this issue open as a reminder to improve the README.md.