implemented right

This commit is contained in:
Jonathan Lamothe 2019-09-02 09:40:57 -04:00
parent 60c662396f
commit 6145dd5c0c
2 changed files with 22 additions and 0 deletions

View File

@ -22,6 +22,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
module Mtlstats.Format module Mtlstats.Format
( padNum ( padNum
, left , left
, right
) where ) where
-- | Pad an 'Int' with leading zeroes to fit a certain character width -- | Pad an 'Int' with leading zeroes to fit a certain character width
@ -48,3 +49,12 @@ left
-- ^ The text to align -- ^ The text to align
-> String -> String
left n str = take n $ str ++ repeat ' ' 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

View File

@ -29,6 +29,7 @@ spec :: Spec
spec = describe "Mtlstats.Format" $ do spec = describe "Mtlstats.Format" $ do
padNumSpec padNumSpec
leftSpec leftSpec
rightSpec
padNumSpec :: Spec padNumSpec :: Spec
padNumSpec = describe "padNum" $ do padNumSpec = describe "padNum" $ do
@ -63,3 +64,14 @@ leftSpec = describe "left" $ do
context "overflow" $ context "overflow" $
it "should truncate the text" $ it "should truncate the text" $
left 2 "foo" `shouldBe` "fo" 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"