From eedeaed8fcf81e5868eb1d423d4efd8ece5273ec Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Tue, 26 Nov 2019 01:33:33 -0500 Subject: [PATCH] implemented complexTable --- src/Mtlstats/Format.hs | 41 +++++++++++++++++++++++++++++++++-------- test/FormatSpec.hs | 27 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/Mtlstats/Format.hs b/src/Mtlstats/Format.hs index 5dfbcd1..9825d69 100644 --- a/src/Mtlstats/Format.hs +++ b/src/Mtlstats/Format.hs @@ -19,6 +19,8 @@ along with this program. If not, see . -} +{-# LANGUAGE LambdaCase #-} + module Mtlstats.Format ( padNum , left @@ -29,10 +31,13 @@ module Mtlstats.Format , labelTable , numTable , tableWith + , complexTable ) where import Data.List (transpose) +import Mtlstats.Types + -- | Pad an 'Int' with leading zeroes to fit a certain character width padNum :: Int @@ -138,12 +143,32 @@ tableWith -> [[String]] -- ^ The cells -> [String] -tableWith func tdata = let - widths = map (map length) tdata +tableWith pFunc tData = complexTable + (repeat pFunc) + (map (map CellText) tData) + +-- | Creates a complex table +complexTable + :: [Int -> String -> String] + -- ^ The padding function for each column + -> [[TableCell]] + -- ^ The table cells (an array of rows) + -> [String] +complexTable pFuncs tData = let + widths = map + (map $ \case + CellText str -> length str + CellFill _ -> 0) + tData colWidths = map maximum $ transpose widths - fitted = map - (\row -> map - (\(str, len) -> func len str) $ - zip row colWidths) - tdata - in map unwords fitted + + bFunc = \case + [] -> "" + [(f, len, CellText str)] -> f len str + [(_, len, CellFill ch)] -> replicate len ch + (f, len, CellText str) : cells -> f len str ++ " " ++ bFunc cells + (_, len, CellFill ch) : cells -> replicate (succ len) ch ++ bFunc cells + + in map + (bFunc . zip3 pFuncs colWidths) + tData diff --git a/test/FormatSpec.hs b/test/FormatSpec.hs index 6c80913..e53249a 100644 --- a/test/FormatSpec.hs +++ b/test/FormatSpec.hs @@ -24,6 +24,7 @@ module FormatSpec (spec) where import Test.Hspec (Spec, context, describe, it, shouldBe) import Mtlstats.Format +import Mtlstats.Types spec :: Spec spec = describe "Mtlstats.Format" $ do @@ -36,6 +37,7 @@ spec = describe "Mtlstats.Format" $ do labelTableSpec numTableSpec tableWithSpec + complexTableSpec padNumSpec :: Spec padNumSpec = describe "padNum" $ do @@ -174,3 +176,28 @@ tableWithSpec = describe "tableWith" $ let ] ) ] + +complexTableSpec :: Spec +complexTableSpec = describe "complexTable" $ mapM_ + (\(label, pFuncs, cells, expected) -> context label $ + it "should format correctly" $ + complexTable pFuncs cells `shouldBe` expected) + [ ( "no fill" + , [left, right] + , [ [ CellText "foo", CellText "bar" ] + , [ CellText "baaz", CellText "quux" ] + ] + , [ "foo bar" + , "baaz quux" + ] + ) + , ( "with fill" + , [left, left, left] + , [ [ CellText "foo", CellText "bar", CellText "baz" ] + , [ CellText "quux", CellFill '-', CellFill '@' ] + ] + , [ "foo bar baz" + , "quux ----@@@" + ] + ) + ]