From 60c662396f4d1e9da93f88e8ac79a09a531b9cd6 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Mon, 2 Sep 2019 09:30:49 -0400 Subject: [PATCH] implemented left --- src/Mtlstats/Format.hs | 14 +++++++++++++- test/FormatSpec.hs | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Mtlstats/Format.hs b/src/Mtlstats/Format.hs index 34f8c73..128a979 100644 --- a/src/Mtlstats/Format.hs +++ b/src/Mtlstats/Format.hs @@ -19,7 +19,10 @@ along with this program. If not, see . -} -module Mtlstats.Format (padNum) where +module Mtlstats.Format + ( padNum + , left + ) where -- | Pad an 'Int' with leading zeroes to fit a certain character width padNum @@ -36,3 +39,12 @@ padNum size n pLen = size - sLen pad = replicate pLen '0' in pad ++ str + +-- | Aligns text to the left within a field (clipping if necessary) +left + :: Int + -- ^ The width of the field + -> String + -- ^ The text to align + -> String +left n str = take n $ str ++ repeat ' ' diff --git a/test/FormatSpec.hs b/test/FormatSpec.hs index 1ad965c..f25ab5a 100644 --- a/test/FormatSpec.hs +++ b/test/FormatSpec.hs @@ -26,8 +26,9 @@ import Test.Hspec (Spec, context, describe, it, shouldBe) import Mtlstats.Format spec :: Spec -spec = describe "Mtlstats.Format" +spec = describe "Mtlstats.Format" $ do padNumSpec + leftSpec padNumSpec :: Spec padNumSpec = describe "padNum" $ do @@ -51,3 +52,14 @@ padNumSpec = describe "padNum" $ do context "-1234, four digits" $ it "should be -1234" $ padNum 4 (-1234) `shouldBe` "-1234" + +leftSpec :: Spec +leftSpec = describe "left" $ do + + context "fit" $ + it "should pad the text" $ + left 5 "foo" `shouldBe` "foo " + + context "overflow" $ + it "should truncate the text" $ + left 2 "foo" `shouldBe` "fo"