packages feed

A-gent-0.11.0.20: src/Internal/Utils.hs

{-# OPTIONS_GHC -Wall #-}

{-# 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
  , trim
  )
where

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

import           Data.Char ( isSpace )

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

combine
  :: Bool
  -> [ (String, String) ]
  -> [ String ]
combine newline =
  map
  ( \ (x, y) ->
      if null x then
        y
      else
        if newline then
          x ++ ":\n" ++ 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 =
  leftpad '0' rep . show

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 =
  linesOptNums prefix [] nums 0

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

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

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

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

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

-- HELPERS (private)

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

trim :: String -> String
trim =
  f . f
  where
    f = reverse . dropWhile isSpace