diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-01-31 23:26:13 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-01-31 23:26:13 +0200 |
| commit | e55a21280d255399e9d1f1979f2035fc9ead8666 (patch) | |
| tree | fa2dc2b1bc456470fb8ece0c69133c533b601188 /src/utils.hs | |
| parent | b235c2887ed60a8fd48263f6c53e90ee7e2073fd (diff) | |
Format strings with fmt
Diffstat (limited to 'src/utils.hs')
| -rw-r--r-- | src/utils.hs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/utils.hs b/src/utils.hs index 252804f..96f5fcb 100644 --- a/src/utils.hs +++ b/src/utils.hs @@ -2,6 +2,8 @@ module Utils where import Control.Monad.Except import qualified Data.Bifunctor as B +import Data.Text (Text) +import qualified Data.Text as T (.>) = flip (.) @@ -51,3 +53,14 @@ safeBreak' _ ex _ [] = throwError ex safeBreak' cond ex acc lst@(x : xs) | cond x = pure (reverse acc, lst) | otherwise = safeBreak' cond ex (x : acc) xs + +fmt :: String -> [String] -> String +fmt str values = T.unpack $ fmt' (T.pack str) (map T.pack values) + +fmt' :: Text -> [Text] -> Text +fmt' text [] = text +fmt' text (v : rest) = + let pattern = T.pack "%%" + (front, back) = T.breakOn pattern text + res = T.concat [front, v, T.drop (T.length pattern) back] + in fmt' res rest |
