Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Text.Layout.Table
Contents
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.
- 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
- ellipsisCutMark :: CutMark
- type Row a = [a]
- layoutToCells :: [Row String] -> [ColSpec] -> [Row String]
- layoutToLines :: [Row String] -> [ColSpec] -> [String]
- layoutToString :: [Row String] -> [ColSpec] -> String
- altLines :: [a -> b] -> [a] -> [b]
- checkeredCells :: (a -> b) -> (a -> b) -> [[a]] -> [[b]]
- data RowGroup
- rowsG :: [Row String] -> RowGroup
- rowG :: Row String -> RowGroup
- colsG :: [Position V] -> [Col String] -> RowGroup
- colsAllG :: Position V -> [Col String] -> RowGroup
- data HeaderColSpec
- headerColumn :: Position H -> Maybe CutMark -> HeaderColSpec
- layoutTableToLines :: [RowGroup] -> Maybe ([String], [HeaderColSpec]) -> [ColSpec] -> TableStyle -> [String]
- layoutTableToString :: [RowGroup] -> Maybe ([String], [HeaderColSpec]) -> [ColSpec] -> TableStyle -> String
- justify :: Int -> [String] -> [String]
- justifyText :: Int -> String -> [String]
- type Col a = [a]
- colsAsRowsAll :: Position V -> [Col [a]] -> [Row [a]]
- colsAsRows :: [Position V] -> [Col [a]] -> [Row [a]]
- top :: Position V
- bottom :: Position V
- data V
- module Text.Layout.Table.Style
- pad :: Position o -> Int -> String -> String
- trimOrPad :: Position o -> CutMark -> Int -> String -> String
- align :: OccSpec -> AlignInfo -> String -> String
- alignFixed :: Position o -> CutMark -> Int -> OccSpec -> AlignInfo -> String -> String
- data ColModInfo
- widthCMI :: ColModInfo -> Int
- unalignedCMI :: ColModInfo -> ColModInfo
- ensureWidthCMI :: Int -> Position H -> ColModInfo -> ColModInfo
- ensureWidthOfCMI :: String -> Position H -> ColModInfo -> ColModInfo
- columnModifier :: Position H -> CutMark -> ColModInfo -> String -> String
- data AlignInfo
- widthAI :: AlignInfo -> Int
- deriveColModInfos :: [(LenSpec, AlignSpec)] -> [Row String] -> [ColModInfo]
- deriveAlignInfo :: OccSpec -> String -> 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
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
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
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.
ellipsisCutMark :: CutMark Source #
Deprecated: Use def instead.
Basic grid and table layout
layoutToCells :: [Row String] -> [ColSpec] -> [Row String] Source #
Modifies cells according to the given ColSpec
.
layoutToLines :: [Row String] -> [ColSpec] -> [String] Source #
Behaves like layoutToCells
but produces lines by joining with whitespace.
layoutToString :: [Row String] -> [ColSpec] -> String Source #
Behaves like layoutToCells
but produces a String
by joining with the
newline character.
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.
Advanced table layout
colsG :: [Position V] -> [Col String] -> RowGroup Source #
Uses the columns to create a row group, using the given vertical positionings.
colsAllG :: Position V -> [Col String] -> RowGroup Source #
Uses the columns to create a row group, using the given vertical positioning.
data HeaderColSpec Source #
Specifies how a header is rendered.
Instances
Default HeaderColSpec Source # | Header columns are usually centered. |
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.
Arguments
:: [RowGroup] | Groups |
-> Maybe ([String], [HeaderColSpec]) | Optional header details |
-> [ColSpec] | Layout specification of columns |
-> TableStyle | Visual table style |
-> [String] |
Layouts a good-looking table with a optional header. Note that specifying fewer layout specifications than columns or vice versa will result in not showing them.
layoutTableToString :: [RowGroup] -> Maybe ([String], [HeaderColSpec]) -> [ColSpec] -> TableStyle -> String Source #
Text justification
Text can easily be justified and distributed over multiple lines. Such columns can easily be combined with other columns.
justify :: Int -> [String] -> [String] Source #
Fits as many words on a line, depending on the given width. Every line, but the last one, gets equally filled with spaces between the words, as far as possible.
Vertical column positioning
colsAsRowsAll :: Position V -> [Col [a]] -> [Row [a]] Source #
Merges multiple columns together and merges them to a valid grid without holes. The following example clarifies this:
>>>
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 layoutToCells
.
colsAsRows :: [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"]]
Table styles
module Text.Layout.Table.Style
Column modification functions
align :: OccSpec -> AlignInfo -> String -> String Source #
Align a column by first finding the position to pad with and then padding the missing lengths to the maximum value. If no such position is found, it will align it such that it gets aligned before that position.
This function assumes:
ai <> deriveAlignInfo s = ai
alignFixed :: Position o -> CutMark -> Int -> OccSpec -> AlignInfo -> String -> String Source #
Aligns a column using a fixed width, fitting it to the width by either filling or cutting while respecting the alignment.
Column modifaction primitives
Render your own kind of tables with the following functions.
data ColModInfo Source #
Specifies how a column should be modified.
widthCMI :: ColModInfo -> Int Source #
Get the exact width after the modification.
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 :: Position H -> CutMark -> ColModInfo -> String -> String Source #
Generates a function which modifies a given String
according to
Position
, CutMark
and ColModInfo
.
Specifies the length before and after a letter.
deriveColModInfos :: [(LenSpec, AlignSpec)] -> [Row String] -> [ColModInfo] Source #
Derive the ColModInfo
by using layout specifications and looking at the
cells.