packages feed

A-gent-0.11.0.6: src/Agent/Utils/Common.hs

{-# OPTIONS_GHC -Wall -Werror #-}

{-# LANGUAGE NoGeneralizedNewtypeDeriving #-}
{-# LANGUAGE Safe                         #-}

--------------------------------------------------------------------------------

-- |
-- Copyright  : (c) 2026 SPISE MISU ApS
-- License    : SSPL-1.0 OR AGPL-3.0-only
-- Maintainer : SPISE MISU <mail+hackage@spisemisu.com>
-- Stability  : experimental

--------------------------------------------------------------------------------

module Agent.Utils.Common
  ( -- * Files
    files
  , file
    -- * Helpers
  , leftpad
  , index
  )
where

--------------------------------------------------------------------------------

leftpad
  :: Char
  -> Int
  -> [Char]
  -> [Char]
leftpad chr rep cs =
  replicate (rep - length xs) chr ++ xs
  where
    xs = take rep cs

index
  :: Int
  -> Int
  -> String
index rep idx =
  leftpad '0' rep $ show idx

files
  :: Bool
  -> [ String ]
  -> [ String ]
files nums fs =
  if null fs then
    []
  else
    map (\ (idx, rel) -> f idx rel)
    $ zip [0..] fs
    where
      len   = length $ show $ length fs
      f i x =
        if nums then
          index len i ++ ": " ++ x
        else
          x

file
  :: Bool
  -> String
  -> String
file nums f =
  if null f then
    []
  else
    unlines
    $ map (\ (idx, rel) -> g idx rel)
    $ zip [1..] ls
    where
      ls    = lines f
      len   = length $ show $ length ls
      g i x =
        if nums then
          index len i ++ ": " ++ x
        else
          x