hcat/src/HCat/FileInfo.hs
Alexander Kobjolke 10057518cb Implement a status line
The status line shows information about the shown file, such as size,
modification time and permissions.
2023-09-10 11:36:35 +02:00

45 lines
1.2 KiB
Haskell

{-# LANGUAGE RecordWildCards #-}
-- | FileInfo defines a data type that captures important information about a
-- file such as timestamps and permissions.
module HCat.FileInfo where
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time.Clock (UTCTime)
import System.Directory (getFileSize, getModificationTime, getPermissions)
import System.Directory qualified as Directory
data FileInfo = FileInfo
{ filePath :: Text
, fileSize :: Integer
, fileMTime :: UTCTime
, fileReadable :: Bool
, fileWritable :: Bool
, fileExecutable :: Bool
}
deriving (Show)
-- | @getFileInfo@ retrieves several meta data about the given file.
--
-- >>> info <- getFileInfo "src/HCat/FileInfo.hs"
-- >>> filePath info
-- "src/HCat/FileInfo.hs"
-- >>> fileSize info > 0
-- True
-- >>> fileReadable info
-- True
-- >>> fileExecutable info
-- False
getFileInfo :: FilePath -> IO FileInfo
getFileInfo path = do
perms <- getPermissions path
fileSize <- getFileSize path
fileMTime <- getModificationTime path
let fileReadable = Directory.readable perms
fileWritable = Directory.writable perms
fileExecutable = Directory.executable perms
filePath = T.pack path
pure FileInfo{..}