From 6145dd5c0c47e25087e7f8115f5118ff0c990f9c Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Mon, 2 Sep 2019 09:40:57 -0400 Subject: [PATCH] implemented right --- src/Mtlstats/Format.hs | 10 ++++++++++ test/FormatSpec.hs | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Mtlstats/Format.hs b/src/Mtlstats/Format.hs index 128a979..fe0f6cf 100644 --- a/src/Mtlstats/Format.hs +++ b/src/Mtlstats/Format.hs @@ -22,6 +22,7 @@ along with this program. If not, see . module Mtlstats.Format ( padNum , left + , right ) where -- | Pad an 'Int' with leading zeroes to fit a certain character width @@ -48,3 +49,12 @@ left -- ^ The text to align -> String left n str = take n $ str ++ repeat ' ' + +-- | Aligns text to the right within a field (clipping if necessary) +right + :: Int + -- ^ The width of the field + -> String + -- ^ The text to align + -> String +right n str = reverse $ left n $ reverse str diff --git a/test/FormatSpec.hs b/test/FormatSpec.hs index f25ab5a..113b4c0 100644 --- a/test/FormatSpec.hs +++ b/test/FormatSpec.hs @@ -29,6 +29,7 @@ spec :: Spec spec = describe "Mtlstats.Format" $ do padNumSpec leftSpec + rightSpec padNumSpec :: Spec padNumSpec = describe "padNum" $ do @@ -63,3 +64,14 @@ leftSpec = describe "left" $ do context "overflow" $ it "should truncate the text" $ left 2 "foo" `shouldBe` "fo" + +rightSpec :: Spec +rightSpec = describe "right" $ do + + context "fit" $ + it "should pad the text" $ + right 5 "foo" `shouldBe` " foo" + + context "overflow" $ + it "should truncate the text" $ + right 2 "foo" `shouldBe` "oo"