packages feed

A-gent-0.11.0.7: 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
    -- * History
  , chits
  , chats
    -- * Helpers
  , leftpad
  , index
  , split
  )
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

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

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

files
  :: Bool
  -> [ String ]
  -> [ String ]
files nums fs =
  linesOptNums nums 0 fs

file
  ::  Bool
  ->  String
  -> [String]
file nums f =
  linesOptNums nums 1 $ lines f

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

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

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

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

-- HELPERS (private)

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