Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Text.Layout.Table
Description
This module provides tools to layout text as grid or table. Besides basic things like specifying column positioning, alignment on the same character and length restriction it also provides advanced features like justifying text and fancy tables with styling support.
Synopsis
- module Data.Default.Class
- data ColSpec
- column :: LenSpec -> Position H -> AlignSpec -> CutMark -> ColSpec
- numCol :: ColSpec
- fixedCol :: Int -> Position H -> ColSpec
- fixedLeftCol :: Int -> ColSpec
- data LenSpec
- expand :: LenSpec
- fixed :: Int -> LenSpec
- expandUntil :: Int -> LenSpec
- fixedUntil :: Int -> LenSpec
- data Position orientation
- data H
- left :: Position H
- right :: Position H
- center :: Position orientation
- data AlignSpec
- noAlign :: AlignSpec
- charAlign :: Char -> AlignSpec
- predAlign :: (Char -> Bool) -> AlignSpec
- dotAlign :: AlignSpec
- data CutMark
- noCutMark :: CutMark
- singleCutMark :: String -> CutMark
- doubleCutMark :: String -> String -> CutMark
- type Row a = [a]
- grid :: Cell a => [ColSpec] -> [Row a] -> [Row String]
- gridLines :: Cell a => [ColSpec] -> [Row a] -> [String]
- gridString :: Cell a => [ColSpec] -> [Row a] -> String
- concatRow :: StringBuilder b => Int -> Row b -> b
- concatLines :: StringBuilder b => [b] -> b
- concatGrid :: StringBuilder b => Int -> [Row b] -> b
- altLines :: [a -> b] -> [a] -> [b]
- checkeredCells :: (a -> b) -> (a -> b) -> [[a]] -> [[b]]
- data RowGroup a
- rowsG :: [Row a] -> RowGroup a
- rowG :: Row a -> RowGroup a
- colsG :: Monoid a => [Position V] -> [Col a] -> RowGroup a
- colsAllG :: Monoid a => Position V -> [Col a] -> RowGroup a
- data HeaderColSpec
- headerColumn :: Position H -> Maybe CutMark -> HeaderColSpec
- data HeaderSpec
- fullH :: [HeaderColSpec] -> [String] -> HeaderSpec
- titlesH :: [String] -> HeaderSpec
- tableLines :: Cell a => [ColSpec] -> TableStyle -> HeaderSpec -> [RowGroup a] -> [String]
- tableString :: Cell a => [ColSpec] -> TableStyle -> HeaderSpec -> [RowGroup a] -> String
- justify :: Int -> [String] -> [String]
- justifyText :: Int -> String -> [String]
- type Col a = [a]
- colsAsRowsAll :: Monoid a => Position V -> [Col a] -> [Row a]
- colsAsRows :: Monoid a => [Position V] -> [Col a] -> [Row a]
- top :: Position V
- bottom :: Position V
- data V
- module Text.Layout.Table.Style
- pad :: (Cell a, StringBuilder b) => Position o -> Int -> a -> b
- trimOrPad :: (Cell a, StringBuilder b) => Position o -> CutMark -> Int -> a -> b
- align :: (Cell a, StringBuilder b) => OccSpec -> AlignInfo -> a -> b
- alignFixed :: (Cell a, StringBuilder b) => Position o -> CutMark -> Int -> OccSpec -> AlignInfo -> a -> b
- data ColModInfo
- widthCMI :: ColModInfo -> Int
- unalignedCMI :: ColModInfo -> ColModInfo
- ensureWidthCMI :: Int -> Position H -> ColModInfo -> ColModInfo
- ensureWidthOfCMI :: String -> Position H -> ColModInfo -> ColModInfo
- columnModifier :: (Cell a, StringBuilder b) => Position H -> CutMark -> ColModInfo -> a -> b
- data AlignInfo
- widthAI :: AlignInfo -> Int
- deriveColModInfos :: Cell a => [(LenSpec, AlignSpec)] -> [Row a] -> [ColModInfo]
- deriveAlignInfo :: Cell a => OccSpec -> a -> AlignInfo
- data OccSpec
Layout combinators
Specify how a column is rendered with the combinators in this
section. Sensible default values are provided with def
.
module Data.Default.Class
Columns
Specifies the layout of a column.
column :: LenSpec -> Position H -> AlignSpec -> CutMark -> ColSpec Source #
Smart constructor to specify a column.
Numbers are positioned on the right and aligned on the floating point dot.
fixedCol :: Int -> Position H -> ColSpec Source #
Fixes the column length and positions according to the given Position
.
fixedLeftCol :: Int -> ColSpec Source #
Fixes the column length and positions on the left.
Length of columns
Determines how long a column will be.
expandUntil :: Int -> LenSpec Source #
The column will expand as long as it is smaller as the given width.
fixedUntil :: Int -> LenSpec Source #
The column will be at least as wide as the given width.
Positional alignment
data Position orientation Source #
Specifies a position relative from a beginning.
Horizontal orientation.
Alignment of cells at characters
Determines whether a column will align at a specific letter.
dotAlign :: AlignSpec Source #
Align all text at the first dot from the left. This is most useful for floating point numbers.
Cut marks
Specifies how the place looks where a String
has been cut. Note that the
cut mark may be cut itself to fit into a column.
singleCutMark :: String -> CutMark Source #
Use the cut mark on both sides by reversing it on the other.
doubleCutMark :: String -> String -> CutMark Source #
Specify two different cut marks, one for cuts on the left and one for cuts on the right.
Basic grid layout
grid :: Cell a => [ColSpec] -> [Row a] -> [Row String] Source #
Modifies cells according to the column specification.
gridLines :: Cell a => [ColSpec] -> [Row a] -> [String] Source #
Behaves like grid
but produces lines by joining with whitespace.
gridString :: Cell a => [ColSpec] -> [Row a] -> String Source #
Behaves like gridLines
but produces a string by joining with the newline
character.
concatRow :: StringBuilder b => Int -> Row b -> b Source #
Concatenates a row with a given amount of spaces.
concatLines :: StringBuilder b => [b] -> b Source #
concatGrid :: StringBuilder b => Int -> [Row b] -> b Source #
Grid modification functions
altLines :: [a -> b] -> [a] -> [b] Source #
Applies functions to given lines in a alternating fashion. This makes it easy to color lines to improve readability in a row.
checkeredCells :: (a -> b) -> (a -> b) -> [[a]] -> [[b]] Source #
Applies functions to cells in a alternating fashion for every line, every other line gets shifted by one. This is useful for distinguishability of single cells in a grid arrangement.
Table layout
Grouping rows
Groups rows together which should not be visually seperated from each other.
colsG :: Monoid a => [Position V] -> [Col a] -> RowGroup a Source #
Create a RowGroup
by aligning the columns vertically. The position is
specified for each column.
colsAllG :: Monoid a => Position V -> [Col a] -> RowGroup a Source #
Create a RowGroup
by aligning the columns vertically. Each column uses
the same vertical positioning.
Headers
data HeaderColSpec Source #
Specifies how a header is rendered.
Instances
Default HeaderColSpec Source # | Header columns are usually centered. |
Defined in Text.Layout.Table.Spec.HeaderColSpec Methods def :: HeaderColSpec # |
headerColumn :: Position H -> Maybe CutMark -> HeaderColSpec Source #
Smart constructor for HeaderColSpec
. By omitting the cut mark it will use
the one specified in the ColSpec
like the
other cells in that column.
data HeaderSpec Source #
Specifies a header.
Instances
Default HeaderSpec Source # | By the default the header is not shown. |
Defined in Text.Layout.Table.Spec.HeaderSpec Methods def :: HeaderSpec # |
fullH :: [HeaderColSpec] -> [String] -> HeaderSpec Source #
Specify a header column for every title.
titlesH :: [String] -> HeaderSpec Source #
Use titles with the default header column specification.
Layout
Arguments
:: Cell a | |
=> [ColSpec] | Layout specification of columns |
-> TableStyle | Visual table style |
-> HeaderSpec | Optional header details |
-> [RowGroup a] | Rows which form a cell together |
-> [String] |
Layouts a pretty table with an optional header. Note that providing fewer layout specifications than columns or vice versa will result in not showing the redundant ones.
Arguments
:: Cell a | |
=> [ColSpec] | Layout specification of columns |
-> TableStyle | Visual table style |
-> HeaderSpec | Optional header details |
-> [RowGroup a] | Rows which form a cell together |
-> String |
Does the same as tableLines
, but concatenates lines.
Text justification
Text can easily be justified and distributed over multiple lines. Such columns can be combined with other columns.
justify :: Int -> [String] -> [String] Source #
Fits as many words on a line as possible depending on the given width. Every line, except the last one, gets equally filled with spaces between the words as far as possible.
Vertical column positioning
colsAsRowsAll :: Monoid a => Position V -> [Col a] -> [Row a] Source #
Merges multiple columns together to a valid grid without holes. For example:
>>>
colsAsRowsAll top [justifyText 10 "This text will not fit on one line.", ["42", "23"]]
[["This text","42"],["will not","23"],["fit on one",""],["line.",""]]
The result is intended to be used with a grid layout function like grid
.
colsAsRows :: Monoid a => [Position V] -> [Col a] -> [Row a] Source #
Works like colsAsRowsAll
but every position can be specified on its
own:
>>>
colsAsRows [top, center, bottom] [["a1"], ["b1", "b2", "b3"], ["c3"]]
[["a1","b1",""],["","b2",""],["","b3","c3"]]
Vertical orientation.
Table styles
module Text.Layout.Table.Style
Column modification functions
pad :: (Cell a, StringBuilder b) => Position o -> Int -> a -> b Source #
Assume the given length is greater or equal than the length of the cell passed. Pads the given cell accordingly using the position specification.
>>>
pad left 10 "foo" :: String
"foo "
align :: (Cell a, StringBuilder b) => OccSpec -> AlignInfo -> a -> b Source #
Align a cell by first locating the position to align with and then padding on both sides. If no such position is found, it will align it such that it gets aligned before that position.
>>>
let { os = predOccSpec (== '.') ; ai = deriveAlignInfo os "iiii.fff" }
>>>
in align os ai <$> ["1.5", "30", ".25"] :: [String]
[" 1.5 "," 30 "," .25 "]
This function assumes that the given String
fits the AlignInfo
. Thus:
ai <> deriveAlignInfo s = ai
alignFixed :: (Cell a, StringBuilder b) => Position o -> CutMark -> Int -> OccSpec -> AlignInfo -> a -> b Source #
Aligns a cell using a fixed width, fitting it to the width by either filling or cutting while respecting the alignment.
Column modifaction primitives
These functions are provided to be reused. For example if someone wants to render their own kind of tables.
data ColModInfo Source #
Specifies how a column should be modified. Values of this type are derived
in a traversal over the input columns by using deriveColModInfos
. Finally,
columnModifier
will interpret them and apply the appropriate modification
function to the cells of the column.
widthCMI :: ColModInfo -> Int Source #
Get the exact width of a ColModInfo
after applying it with
columnModifier
.
unalignedCMI :: ColModInfo -> ColModInfo Source #
Remove alignment from a ColModInfo
. This is used to change alignment of
headers while using the combined width information.
ensureWidthCMI :: Int -> Position H -> ColModInfo -> ColModInfo Source #
Ensures that the modification provides a minimum width but only if it is not limited.
ensureWidthOfCMI :: String -> Position H -> ColModInfo -> ColModInfo Source #
Ensures that the given String
will fit into the modified columns.
columnModifier :: (Cell a, StringBuilder b) => Position H -> CutMark -> ColModInfo -> a -> b Source #
Generates a function which modifies a given cell according to
Position
, CutMark
and ColModInfo
. This is
used to modify a single cell of a column to bring all cells of a column to
the same width.
Specifies the length before and after an alignment position (excluding the alignment character).
deriveColModInfos :: Cell a => [(LenSpec, AlignSpec)] -> [Row a] -> [ColModInfo] Source #
Derive the ColModInfo
by using layout specifications and the actual cells
of a column. This function only needs to know about LenSpec
and AlignInfo
.