lib: move readFileStrictly to Hledger.Utils.IO

This commit is contained in:
Simon Michael 2023-12-06 21:56:10 -10:00
parent 7899b992b7
commit d1635a55f8
3 changed files with 6 additions and 8 deletions

View File

@ -405,9 +405,6 @@ latestDatesFileFor f = dir </> ".latest" <.> fname
where where
(dir, fname) = splitFileName f (dir, fname) = splitFileName f
readFileStrictly :: FilePath -> IO Text
readFileStrictly f = readFilePortably f >>= \t -> C.evaluate (T.length t) >> return t
-- | Given zero or more latest dates (all the same, representing the -- | Given zero or more latest dates (all the same, representing the
-- latest previously seen transaction date, and how many transactions -- latest previously seen transaction date, and how many transactions
-- were seen on that date), remove transactions with earlier dates -- were seen on that date), remove transactions with earlier dates

View File

@ -80,6 +80,7 @@ module Hledger.Utils.IO (
expandGlob, expandGlob,
sortByModTime, sortByModTime,
readFileOrStdinPortably, readFileOrStdinPortably,
readFileStrictly,
readFilePortably, readFilePortably,
readHandlePortably, readHandlePortably,
-- hereFileRelative, -- hereFileRelative,
@ -91,6 +92,7 @@ module Hledger.Utils.IO (
) )
where where
import qualified Control.Exception as C (evaluate)
import Control.Monad (when, forM) import Control.Monad (when, forM)
import Data.Colour.RGBSpace (RGB(RGB)) import Data.Colour.RGBSpace (RGB(RGB))
import Data.Colour.RGBSpace.HSL (lightness) import Data.Colour.RGBSpace.HSL (lightness)
@ -461,6 +463,10 @@ sortByModTime fs = do
ftimes <- forM fs $ \f -> do {t <- getModificationTime f; return (t,f)} ftimes <- forM fs $ \f -> do {t <- getModificationTime f; return (t,f)}
return $ map snd $ reverse $ sort ftimes return $ map snd $ reverse $ sort ftimes
-- | Like readFilePortably, but read all of the file before proceeding.
readFileStrictly :: FilePath -> IO T.Text
readFileStrictly f = readFilePortably f >>= \t -> C.evaluate (T.length t) >> return t
-- | Read text from a file, -- | Read text from a file,
-- converting any \r\n line endings to \n,, -- converting any \r\n line endings to \n,,
-- using the system locale's text encoding, -- using the system locale's text encoding,

View File

@ -20,7 +20,6 @@ module Hledger.Cli.Utils
openBrowserOn, openBrowserOn,
writeFileWithBackup, writeFileWithBackup,
writeFileWithBackupIfChanged, writeFileWithBackupIfChanged,
readFileStrictly,
pivotByOpts, pivotByOpts,
anonymiseByOpts, anonymiseByOpts,
journalSimilarTransaction, journalSimilarTransaction,
@ -29,7 +28,6 @@ module Hledger.Cli.Utils
) )
where where
import Control.Exception as C
import Control.Monad.Except (ExceptT) import Control.Monad.Except (ExceptT)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Data.List import Data.List
@ -223,9 +221,6 @@ writeFileWithBackupIfChanged f t = do
writeFileWithBackup :: FilePath -> String -> IO () writeFileWithBackup :: FilePath -> String -> IO ()
writeFileWithBackup f t = backUpFile f >> writeFile f t writeFileWithBackup f t = backUpFile f >> writeFile f t
readFileStrictly :: FilePath -> IO T.Text
readFileStrictly f = readFilePortably f >>= \s -> C.evaluate (T.length s) >> return s
-- | Back up this file with a (incrementing) numbered suffix, or give an error. -- | Back up this file with a (incrementing) numbered suffix, or give an error.
backUpFile :: FilePath -> IO () backUpFile :: FilePath -> IO ()
backUpFile fp = do backUpFile fp = do