table-layout-1.0.0.0: Format tabular data as grid or table.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Layout.Table.Cell

Synopsis

Documentation

truncateNegative :: Int -> Int Source #

Ensure a value is not negative.

data CellView a Source #

An object along with the amount that its length should be adjusted on both the left and right. Positive numbers are padding and negative numbers are trimming.

Constructors

CellView 

Fields

Instances

Instances details
Applicative CellView Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

pure :: a -> CellView a

(<*>) :: CellView (a -> b) -> CellView a -> CellView b

liftA2 :: (a -> b -> c) -> CellView a -> CellView b -> CellView c

(*>) :: CellView a -> CellView b -> CellView b

(<*) :: CellView a -> CellView b -> CellView a

Functor CellView Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

fmap :: (a -> b) -> CellView a -> CellView b

(<$) :: a -> CellView b -> CellView a

Monad CellView Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

(>>=) :: CellView a -> (a -> CellView b) -> CellView b

(>>) :: CellView a -> CellView b -> CellView b

return :: a -> CellView a

Show a => Show (CellView a) Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

showsPrec :: Int -> CellView a -> ShowS

show :: CellView a -> String

showList :: [CellView a] -> ShowS

Eq a => Eq (CellView a) Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

(==) :: CellView a -> CellView a -> Bool

(/=) :: CellView a -> CellView a -> Bool

Ord a => Ord (CellView a) Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

compare :: CellView a -> CellView a -> Ordering

(<) :: CellView a -> CellView a -> Bool

(<=) :: CellView a -> CellView a -> Bool

(>) :: CellView a -> CellView a -> Bool

(>=) :: CellView a -> CellView a -> Bool

max :: CellView a -> CellView a -> CellView a

min :: CellView a -> CellView a -> CellView a

Cell a => Cell (CellView a) Source # 
Instance details

Defined in Text.Layout.Table.Cell

adjustCell :: Int -> Int -> a -> CellView a Source #

Add an adjustment to the left and right of a Cell. Positive numbers are padding and negative numbers are trimming.

dropLeft :: Int -> a -> CellView a Source #

Drop a number of characters from the left side. Treats negative numbers as zero.

dropRight :: Int -> a -> CellView a Source #

Drop a number of characters from the right side. Treats negative numbers as zero.

dropBoth :: Int -> Int -> a -> CellView a Source #

Drop characters from both sides. Treats negative numbers as zero.

totalAdjustment :: CellView a -> Int Source #

The total amount of adjustment in CellView.

redistributeAdjustment :: Int -> Int -> CellView a -> CellView a Source #

Redistribute padding or trimming using a given ratio.

class Cell a where Source #

Types that can be measured for visible characters, define a sub-string operation and turned into a StringBuilder.

Minimal complete definition

visibleLength, measureAlignment, buildCellView

Methods

visibleLength :: a -> Int Source #

Returns the length of the visible characters as displayed on the output medium.

measureAlignment :: (Char -> Bool) -> a -> AlignInfo Source #

Measure the preceding and following characters for a position where the predicate matches.

buildCell :: StringBuilder b => a -> b Source #

Insert the contents into a StringBuilder.

buildCellView :: StringBuilder b => CellView a -> b Source #

Insert the contents into a StringBuilder, padding or trimming as necessary.

The Cell instance of 'CellView a' means that this can usually be substituted with buildCell, and is only needed for defining the instance.

Instances

Instances details
Cell WideString Source # 
Instance details

Defined in Text.Layout.Table.Cell.WideString

Cell WideText Source # 
Instance details

Defined in Text.Layout.Table.Cell.WideString

Cell Text Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

visibleLength :: Text -> Int Source #

measureAlignment :: (Char -> Bool) -> Text -> AlignInfo Source #

buildCell :: StringBuilder b => Text -> b Source #

buildCellView :: StringBuilder b => CellView Text -> b Source #

Cell String Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

visibleLength :: String -> Int Source #

measureAlignment :: (Char -> Bool) -> String -> AlignInfo Source #

buildCell :: StringBuilder b => String -> b Source #

buildCellView :: StringBuilder b => CellView String -> b Source #

Cell a => Cell (CellView a) Source # 
Instance details

Defined in Text.Layout.Table.Cell

Cell a => Cell (Formatted a) Source # 
Instance details

Defined in Text.Layout.Table.Cell.Formatted

Cell a => Cell (Maybe a) Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

visibleLength :: Maybe a -> Int Source #

measureAlignment :: (Char -> Bool) -> Maybe a -> AlignInfo Source #

buildCell :: StringBuilder b => Maybe a -> b Source #

buildCellView :: StringBuilder b => CellView (Maybe a) -> b Source #

(Cell a, Cell b) => Cell (Either a b) Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

visibleLength :: Either a b -> Int Source #

measureAlignment :: (Char -> Bool) -> Either a b -> AlignInfo Source #

buildCell :: StringBuilder b0 => Either a b -> b0 Source #

buildCellView :: StringBuilder b0 => CellView (Either a b) -> b0 Source #

buildCellViewLRHelper Source #

Arguments

:: StringBuilder b 
=> (a -> b)

Builder function for a.

-> (Int -> a -> a)

Function for trimming on the left.

-> (Int -> a -> a)

Function for trimming on the right.

-> CellView a 
-> b 

Construct buildCellView from a builder function, a function for trimming from the left, and a function for trimming from the right.

Used to define instances of Cell.

buildCellViewBothHelper Source #

Arguments

:: StringBuilder b 
=> (a -> b)

Builder function for a.

-> (Int -> Int -> a -> a)

Function for trimming on the left and right simultaneously.

-> CellView a 
-> b 

Construct buildCellView from a builder function, and a function for trimming from the left and right simultaneously.

Used to define instanced of Cell.

buildCellViewHelper Source #

Arguments

:: StringBuilder b 
=> (a -> b)

Builder function for a.

-> (Int -> a -> b)

Function for trimming on the left.

-> (Int -> a -> b)

Function for trimming on the right.

-> (Int -> Int -> a -> b)

Function for trimming on the left and right simultaneously.

-> CellView a 
-> b 

Construct buildCellView from builder functions and trimming functions.

Used to define instances of Cell.

remSpacesB Source #

Arguments

:: (Cell a, StringBuilder b) 
=> Int

The expected length.

-> a

A cell.

-> b 

Creates a StringBuilder with the amount of missing spaces.

fillRight :: Cell a => Int -> a -> CellMod a Source #

Fill the right side with spaces if necessary.

fillRight' :: Cell a => Int -> Int -> a -> CellMod a Source #

Fill the right side with spaces if necessary. Preconditions that are required to be met (otherwise the function will produce garbage):

visibleLength c == k

fillCenter :: Cell a => Int -> a -> CellMod a Source #

Fill both sides with spaces if necessary.

fillCenter' :: Cell a => Int -> Int -> a -> CellMod a Source #

Fill both sides with spaces if necessary. Preconditions that are required to be met (otherwise the function will produce garbage):

visibleLength c == k

fillLeft :: Cell a => Int -> a -> CellMod a Source #

Fill the left side with spaces if necessary.

fillLeft' :: Cell a => Int -> Int -> a -> CellMod a Source #

Fill the left side with spaces if necessary. Preconditions that are required to be met (otherwise the function will produce garbage):

visibleLength c == k

pad :: Cell a => Position o -> Int -> a -> CellMod a Source #

Pads the given cell accordingly using the position specification.

>>> buildCellMod noCutMark $ pad left 10 "foo" :: String
"foo       "

pad' :: Cell a => Position o -> Int -> Int -> a -> CellMod a Source #

Pads the given cell accordingly using the position specification. Preconditions that are required to be met (otherwise the function will produce garbage):

visibleLength c == k

trimOrPad :: Cell a => Position o -> CutMark -> Int -> a -> CellMod a Source #

If the given text is too long, the String will be shortened according to the position specification. Adds cut marks to indicate that the column has been trimmed in length, otherwise it behaves like pad.

>>> let cm = singleCutMark ".."
>>> buildCellMod cm $ trimOrPad left cm 10 "A longer text." :: String
"A longer.."

trimOrPadBetween Source #

Arguments

:: Cell a 
=> Position o 
-> CutMark 
-> Int

The length lower to pad to if too short

-> Int

The length upper to trim to if too long

-> a 
-> CellMod a 

If the given text is too long, it will be trimmed to length upper according to the position specification, and cut marks will be added to indicate that the column has been trimmed in length. Otherwise, if the given text is too short, it will be padded to length lower.

>>> let cm = singleCutMark ".."
>>> buildCellMod cm $ trimOrPadBetween left cm 7 10 "A longer text." :: String
"A longer.."
>>> buildCellMod cm $ trimOrPadBetween left cm 7 10 "Short" :: String
"Short  "
>>> buildCellMod cm $ trimOrPadBetween left cm 7 10 "A medium" :: String
"A medium"

Preconditions that are required to be met (otherwise the output will be counterintuitive):

lower <= upper

trim :: Cell a => Position o -> CutMark -> Int -> a -> CellMod a Source #

Trim a cell based on the position. Cut marks may be trimmed if necessary.

trim' :: Cell a => Position o -> CutMark -> Int -> Int -> a -> CellMod a Source #

Trim a cell based on the position. Cut marks may be trimmed if necessary.

Preconditions that are required to be met (otherwise the function will produce garbage):

visibleLength c > n
visibleLength c == k

align :: Cell a => OccSpec -> AlignInfo -> a -> CellMod a 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 buildCellMod noCutMark . 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

data CutAction Source #

Constructors

FillCA Int 
CutCA Int 
NoneCA 

Instances

Instances details
Show CutAction Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

showsPrec :: Int -> CutAction -> ShowS

show :: CutAction -> String

showList :: [CutAction] -> ShowS

Eq CutAction Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

(==) :: CutAction -> CutAction -> Bool

(/=) :: CutAction -> CutAction -> Bool

data CutInfo Source #

Constructors

SidesCI CutAction CutAction

Apply a cut action to each side.

MarkLeftCI

Apply a mark to a whitespace string pointing to the left.

MarkRightCI

Apply a mark to a whitespace string pointing to the right.

Instances

Instances details
Show CutInfo Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

showsPrec :: Int -> CutInfo -> ShowS

show :: CutInfo -> String

showList :: [CutInfo] -> ShowS

Eq CutInfo Source # 
Instance details

Defined in Text.Layout.Table.Cell

Methods

(==) :: CutInfo -> CutInfo -> Bool

(/=) :: CutInfo -> CutInfo -> Bool

determineCuts :: Int -> Int -> Int -> Int -> CutInfo Source #

Compares the view range, that represents the visible part, with the cell range, which is the position of the cell relative to the alignment, and determines the actions that should be performed.

numSpacesAfterCut :: CutAction -> Int -> Int -> Int Source #

If the amount to be cut is bigger than the cell length then any missing amount is taken away from any remaining padding.

applyCutInfo :: Cell a => CutInfo -> CutMark -> Int -> Int -> a -> CellMod a Source #

viewRange :: Position o -> Int -> Int -> Int -> (Int, Int) Source #

Given a position, the available width, and the length of an alignment (left and right side, separator is implied) compute a range for the view. The lower bound is inclusive and the upper bound exclusive.

cellRange :: Int -> AlignInfo -> (Int, Int) Source #

Given the maximum left alignment and the alignment of the cell create a range that describes the position of the cell. The lower bound is inclusive and the upper bound exclusive.

alignFixed :: Cell a => Position o -> CutMark -> Int -> OccSpec -> AlignInfo -> a -> CellMod a Source #

Aligns a cell using a fixed width, fitting it to the width by either filling or cutting while respecting the alignment.

buildCellMod :: (Cell c, StringBuilder s) => CutMark -> CellMod c -> s Source #

Interpret CellMod to create a builder.