    module TextBuilderDev.Domains.Padding where

    import qualified Data.Text as Text
    import TextBuilder
    import TextBuilderDev.Prelude

    -- | Pad a builder from the left side to the specified length with the specified character.
    --
    -- >>> padFromLeft 5 '0' "123"
    -- "00123"
    --
    -- >>> padFromLeft 5 '0' "123456"
    -- "123456"
    {-# INLINEABLE padFromLeft #-}
    padFromLeft :: Int -> Char -> TextBuilder -> TextBuilder
    padFromLeft :: Int -> Char -> TextBuilder -> TextBuilder
    padFromLeft Int
    paddedLength Char
    paddingChar =
      Int -> Char -> Text -> TextBuilder
    textPaddedFromLeft Int
    paddedLength Char
    paddingChar (Text -> TextBuilder)
    -> (TextBuilder -> Text) -> TextBuilder -> TextBuilder
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
    Category cat =>
    cat b c -> cat a b -> cat a c
    . TextBuilder -> Text
    toText

    -- | Pad a builder from the right side to the specified length with the specified character.
    --
    -- >>> padFromRight 5 ' ' "123"
    -- "123  "
    --
    -- >>> padFromRight 5 ' ' "123456"
    -- "123456"
    {-# INLINEABLE padFromRight #-}
    padFromRight :: Int -> Char -> TextBuilder -> TextBuilder
    padFromRight :: Int -> Char -> TextBuilder -> TextBuilder
    padFromRight Int
    paddedLength Char
    paddingChar =
      Int -> Char -> Text -> TextBuilder
    textPaddedFromRight Int
    paddedLength Char
    paddingChar (Text -> TextBuilder)
    -> (TextBuilder -> Text) -> TextBuilder -> TextBuilder
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
    Category cat =>
    cat b c -> cat a b -> cat a c
    . TextBuilder -> Text
    toText

    -- | Pad a text from the left side to the specified length with the specified character.
    --
    -- >>> textPaddedFromLeft 5 '0' "123"
    -- "00123"
    --
    -- >>> textPaddedFromLeft 5 '0' "123456"
    -- "123456"
    {-# INLINEABLE textPaddedFromLeft #-}
    textPaddedFromLeft :: Int -> Char -> Text -> TextBuilder
    textPaddedFromLeft :: Int -> Char -> Text -> TextBuilder
    textPaddedFromLeft Int
    paddedLength Char
    paddingChar Text
    input =
      let actualLength :: Int
    actualLength = Text -> Int
    Text.length Text
    input
       in if Int
    paddedLength Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    actualLength
            then Text -> TextBuilder
    text Text
    input
            else (Char -> TextBuilder) -> [Char] -> TextBuilder
    forall m a. Monoid m => (a -> m) -> [a] -> m
    forall (t :: * -> *) m a.
    (Foldable t, Monoid m) =>
    (a -> m) -> t a -> m
    foldMap Char -> TextBuilder
    char (Int -> Char -> [Char]
    forall a. Int -> a -> [a]
    replicate (Int
    paddedLength Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    actualLength) Char
    paddingChar) TextBuilder -> TextBuilder -> TextBuilder
    forall a. Semigroup a => a -> a -> a
    <> Text -> TextBuilder
    text Text
    input

    -- | Pad a text from the right side to the specified length with the specified character.
    --
    -- >>> textPaddedFromRight 5 '0' "123"
    -- "12300"
    --
    -- >>> textPaddedFromRight 5 '0' "123456"
    -- "123456"
    {-# INLINEABLE textPaddedFromRight #-}
    textPaddedFromRight :: Int -> Char -> Text -> TextBuilder
    textPaddedFromRight :: Int -> Char -> Text -> TextBuilder
    textPaddedFromRight Int
    paddedLength Char
    paddingChar Text
    inputText =
      let actualLength :: Int
    actualLength = Text -> Int
    Text.length Text
    inputText
       in if Int
    paddedLength Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    actualLength
            then Text -> TextBuilder
    text Text
    inputText
            else Text -> TextBuilder
    text Text
    inputText TextBuilder -> TextBuilder -> TextBuilder
    forall a. Semigroup a => a -> a -> a
    <> (Char -> TextBuilder) -> [Char] -> TextBuilder
    forall m a. Monoid m => (a -> m) -> [a] -> m
    forall (t :: * -> *) m a.
    (Foldable t, Monoid m) =>
    (a -> m) -> t a -> m
    foldMap Char -> TextBuilder
    char (Int -> Char -> [Char]
    forall a. Int -> a -> [a]
    replicate (Int
    paddedLength Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    actualLength) Char
    paddingChar)