table-layout 0.5.0.0 → 0.5.1.1
raw patch · 7 files changed
+76/−21 lines, 7 filesdep +data-default-instances-base
Dependencies added: data-default-instances-base
Files
- src/Test.hs +2/−1
- src/Text/Layout/Table.hs +23/−10
- src/Text/Layout/Table/Internal.hs +6/−0
- src/Text/Layout/Table/Justify.hs +5/−4
- src/Text/Layout/Table/Primitives/Basic.hs +3/−0
- src/Text/Layout/Table/Style.hs +27/−0
- table-layout.cabal +10/−6
src/Test.hs view
@@ -28,7 +28,8 @@ shortText = "Short" bigNum = "200300400500600.2" smallNum = "4.20000000"- styles = [ asciiRoundS+ styles = [ asciiS+ , asciiRoundS , unicodeS , unicodeRoundS , unicodeBoldS
src/Text/Layout/Table.hs view
@@ -25,7 +25,7 @@ -- , rowGroup [["Short text", "100200.5"]] -- ] -- (Just (["Title", "Length"], repeat def))--- [fixedLeftCol 20, column (fixed 10) center dotAlign ellipsisCutMark]+-- [fixedLeftCol 20, column (fixed 10) center dotAlign def] -- unicodeRoundS -- ╭──────────────────────┬────────────╮ -- │ Title │ Length │@@ -36,7 +36,6 @@ -- ╰──────────────────────┴────────────╯ -- {-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE MultiWayIf #-} module Text.Layout.Table ( -- * Layout combinators -- | Specify how a column is rendered with the combinators in this@@ -76,6 +75,7 @@ , ellipsisCutMark -- * Basic grid and table layout+ , Row , layoutToCells , layoutToLines , layoutToString@@ -96,6 +96,7 @@ -- $justify , justify , justifyText+ , Col , columnsAsGrid , top , bottom@@ -134,10 +135,11 @@ -- TODO ColSpec: add some kind of combinator to construct ColSpec values (e.g. via Monoid, see optparse-applicative) -- TODO move functions not related to direct end-user into Primitives -import qualified Control.Arrow as A+import qualified Control.Arrow as A import Data.List import Data.Maybe import Data.Default.Class+import Data.Default.Instances.Base () import Text.Layout.Table.Justify import Text.Layout.Table.Style@@ -327,6 +329,13 @@ | FillTo Int | FitTo Int (Maybe (OccSpec, AlignInfo)) +-- | Private show function.+showCMI :: ColModInfo -> String+showCMI cmi = case cmi of+ FillAligned oS ai -> "FillAligned .. " ++ showAI ai+ FillTo i -> "FillTo " ++ show i+ FitTo i _ -> "FitTo " ++ show i ++ ".."+ -- | Get the exact width after the modification. widthCMI :: ColModInfo -> Int widthCMI cmi = case cmi of@@ -347,8 +356,8 @@ ensureWidthCMI :: Int -> Position H -> ColModInfo -> ColModInfo ensureWidthCMI w pos cmi = case cmi of FillAligned oS ai@(AlignInfo lw rw) ->- let neededW = widthAI ai - w- in if neededW >= 0+ let neededW = w - widthAI ai+ in if neededW <= 0 then cmi else FillAligned oS $ case pos of Start -> AlignInfo lw (rw + neededW)@@ -375,6 +384,10 @@ -- | Specifies the length before and after a letter. data AlignInfo = AlignInfo Int Int +-- | Private show function.+showAI :: AlignInfo -> String+showAI (AlignInfo l r) = "AlignInfo " ++ show l ++ " " ++ show r+ -- | The column width when using the 'AlignInfo'. widthAI :: AlignInfo -> Int widthAI (AlignInfo l r) = l + r@@ -387,7 +400,7 @@ -- | Derive the 'ColModInfo' by using layout specifications and looking at the -- cells.-deriveColModInfos :: [(LenSpec, AlignSpec)] -> [[String]] -> [ColModInfo]+deriveColModInfos :: [(LenSpec, AlignSpec)] -> [Row String] -> [ColModInfo] deriveColModInfos specs = zipWith ($) (fmap fSel specs) . transpose where fSel (lenSpec, alignSpec) = case alignSpec of@@ -423,7 +436,7 @@ ------------------------------------------------------------------------------- -- | Modifies cells according to the given 'ColSpec'.-layoutToCells :: [[String]] -> [ColSpec] -> [[String]]+layoutToCells :: [Row String] -> [ColSpec] -> [Row String] layoutToCells tab specs = zipWith apply tab . repeat . zipWith (uncurry columnModifier) (map (position A.&&& cutMark) specs)@@ -432,12 +445,12 @@ apply = zipWith $ flip ($) -- | Behaves like 'layoutToCells' but produces lines by joining with whitespace.-layoutToLines :: [[String]] -> [ColSpec] -> [String]+layoutToLines :: [Row String] -> [ColSpec] -> [String] layoutToLines tab specs = map unwords $ layoutToCells tab specs -- | Behaves like 'layoutToCells' but produces a 'String' by joining with the -- newline character.-layoutToString :: [[String]] -> [ColSpec] -> String+layoutToString :: [Row String] -> [ColSpec] -> String layoutToString tab specs = intercalate "\n" $ layoutToLines tab specs -------------------------------------------------------------------------------@@ -460,7 +473,7 @@ ------------------------------------------------------------------------------- -- | Construct a row group from a list of rows.-rowGroup :: [[String]] -> RowGroup+rowGroup :: [Row String] -> RowGroup rowGroup = RowGroup -- | Header columns are usually centered.
src/Text/Layout/Table/Internal.hs view
@@ -14,3 +14,9 @@ -- one specified in the 'Text.Layout.Primitives.Column.ColSpec' like the other -- cells in that column. data HeaderColSpec = HeaderColSpec (Position H) (Maybe CutMark)++-- | An alias for lists, conceptually for values with a horizontal arrangement.+type Row a = [a]++-- | An alias for lists, conceptually for values with a vertical arrangement.+type Col a = [a]
src/Text/Layout/Table/Justify.hs view
@@ -17,18 +17,19 @@ import Control.Arrow import Data.List +import Text.Layout.Table.Internal import Text.Layout.Table.Primitives.Basic import Text.Layout.Table.Position.Internal -- | Justifies texts and presents the resulting lines in a grid structure (each -- text in one column).-justifyTextsAsGrid :: [(Int, String)] -> [[String]]+justifyTextsAsGrid :: [(Int, String)] -> [Row String] justifyTextsAsGrid = justifyWordListsAsGrid . fmap (second words) -- | Justifies lists of words and presents the resulting lines in a grid -- structure (each list of words in one column). This is useful if you don't -- want to split just at whitespaces.-justifyWordListsAsGrid :: [(Int, [String])] -> [[String]]+justifyWordListsAsGrid :: [(Int, [String])] -> [Row String] justifyWordListsAsGrid = columnsAsGrid top . fmap (uncurry justify) {- | Merges multiple columns together and merges them to a valid grid without@@ -40,10 +41,10 @@ The result is intended to be used with 'Text.Layout.Table.layoutToCells' or with 'Text.Layout.Table.rowGroup'. -}-columnsAsGrid :: Position V -> [[[a]]] -> [[[a]]]+columnsAsGrid :: Position V -> [Col [a]] -> [Row [a]] columnsAsGrid vPos = transpose . vpadCols vPos [] --- | Fill all sublists to the same length.+-- | Fill all columns to the same length by aligning at the given position. vpadCols :: Position V -> a -> [[a]] -> [[a]] vpadCols vPos x l = fmap fillToMax l where
src/Text/Layout/Table/Primitives/Basic.hs view
@@ -35,6 +35,8 @@ ) where +-- TODO rename cut marks (they are too long)+ import Data.Default.Class -- | Specifies how the place looks where a 'String' has been cut. Note that the@@ -44,6 +46,7 @@ , rightMark :: String } +-- | A single ellipsis unicode character is used to show cut marks. instance Default CutMark where def = ellipsisCutMark
src/Text/Layout/Table/Style.hs view
@@ -56,6 +56,33 @@ , groupBottomH = '-' } +-- | Uses lines and plus for joints.+asciiS :: TableStyle+asciiS = TableStyle+ { headerSepH = '-'+ , headerSepLC = '+'+ , headerSepRC = '+'+ , headerSepC = '+'+ , headerTopL = '+'+ , headerTopR = '+'+ , headerTopC = '+'+ , headerTopH = '-'+ , headerV = '|'+ , groupV = '|'+ , groupSepH = '-'+ , groupSepC = '+'+ , groupSepLC = '+'+ , groupSepRC = '+'+ , groupTopC = '+'+ , groupTopL = '+'+ , groupTopR = '+'+ , groupTopH = '-'+ , groupBottomC = '+'+ , groupBottomL = '+'+ , groupBottomR = '+'+ , groupBottomH = '-'+ }+ -- | Uses special unicode characters to draw clean thin boxes. unicodeS :: TableStyle unicodeS = TableStyle
table-layout.cabal view
@@ -6,7 +6,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.5.0.0+version: 0.5.1.1 synopsis: Layout text as grid or table. @@ -69,12 +69,13 @@ -- LANGUAGE extensions used by modules in this package.- other-extensions: RecordWildCards, MultiWayIf+ other-extensions: RecordWildCards -- Other library packages from which modules are imported.- build-depends: base >=4.8 && <4.9, data-default-class >=0.0.1 && < 0.1- - -- Directories containing source files.+ build-depends: base >=4.8 && <4.9,+ data-default-class >=0.0.1 && < 0.1,+ data-default-instances-base ==0.1.*+ hs-source-dirs: src -- Base language which the package is written in.@@ -82,7 +83,9 @@ executable table-layout-test-styles main-is: Test.hs- build-depends: base >=4.8 && <4.9, data-default-class >=0.0.1 && < 0.1+ build-depends: base >=4.8 && <4.9,+ data-default-class >=0.0.1 && < 0.1,+ data-default-instances-base ==0.1.* hs-source-dirs: src other-modules: Text.Layout.Table default-language: Haskell2010@@ -96,6 +99,7 @@ QuickCheck >=2.8 && < 2.9, HUnit ==1.3.*, data-default-class >=0.0.1 && < 0.1,+ data-default-instances-base ==0.1.*, hspec other-modules: Text.Layout.Table, Text.Layout.Table.Primitives.Basic