packages feed

A-gent-0.11.0.13: src/Internal/Utils.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 Internal.Utils
  ( -- * Files
    files
  , file
    -- * History
  , chits
  , chats
    -- * Helpers
  , combine
  , leftpad
  , index
  , split
  , linesOptNums
  )
where

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

import           Prelude hiding ( lines )
import qualified Prelude as Prelude

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

combine
  :: [ (String, String) ]
  -> [ String ]
combine =
  map
  ( \ (x, y) ->
      if null x then
        y
      else
        x ++ ": " ++ y
  )

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

split
  :: Char
  ->  String
  -> [String]
split sep str =
  case dropWhile (== sep) str of
    [] -> []
    cs ->
      b : split sep bs
      where
        (b, bs) = break (== sep) cs

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

files
  :: String
  -> Bool
  -> [ String ]
  -> [ (String, String) ]
files prefix nums fs =
  linesOptNums prefix nums 0 fs

file
  :: Bool
  -> String
  -> [ (String, String) ]
file nums f =
  linesOptNums [] nums 1 $ Prelude.lines f

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

chits
  :: Bool
  -> [ String ]
  -> [ (String, String) ]
chits nums fs =
  linesOptNums [] nums 0 $ reverse fs

chats
  :: Bool
  -> [ String ]
  -> [ (String, String) ]
chats nums fs =
  linesOptNums [] nums 0 $ reverse fs

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

-- HELPERS (private)

linesOptNums
  :: String
  -> Bool
  -> Int
  -> [ String ]
  -> [ (String, String) ]
linesOptNums prefix nums origo xs =
  zipWith (\ idx rel -> (prefix ++ f idx, rel)) [origo..] xs
  where
    len   = length $ show $ length xs
    f i =
      if nums then
        index len i
      else
        []