hcdu/app/Main.hs

28 lines
986 B
Haskell
Raw Normal View History

2023-10-12 13:01:36 +00:00
module Main where
2023-10-13 13:24:26 +00:00
import System.Posix.Directory ( getWorkingDirectory )
import System.Directory
( pathIsSymbolicLink, doesFileExist, getFileSize, doesDirectoryExist )
import Data.Functor ( (<&>) )
2023-10-13 13:54:57 +00:00
import Control.Monad ( filterM )
2023-10-13 13:24:26 +00:00
import System.Directory.Extra ( listContents )
2023-10-13 13:54:57 +00:00
import Brick
2023-10-12 14:37:54 +00:00
2023-10-13 13:54:57 +00:00
listNonSymFiles :: FilePath -> IO [FilePath]
listNonSymFiles dir = filterM (fmap not . pathIsSymbolicLink) =<< filterM doesFileExist =<< listContents dir
2023-10-12 14:37:54 +00:00
2023-10-13 13:54:57 +00:00
listNonSymDirectories :: FilePath -> IO [FilePath]
listNonSymDirectories dir = filterM (fmap not . pathIsSymbolicLink) =<< filterM doesDirectoryExist =<< listContents dir
2023-10-13 13:24:26 +00:00
2023-10-13 13:54:57 +00:00
getSizeSubpaths :: FilePath -> IO [(FilePath, Integer)]
getSizeSubpaths x = do
sub <- listNonSymDirectories x
subby <- mapM (\y -> (y,) . sum . map snd <$> getSizeSubpaths y) sub
local <- listNonSymFiles x >>= (\y -> mapM getFileSize y <&> zip y)
return $ local ++ subby
2023-10-12 14:37:54 +00:00
2023-10-12 13:01:36 +00:00
main :: IO ()
2023-10-12 14:37:54 +00:00
main = do
2023-10-13 13:24:26 +00:00
a <- getWorkingDirectory
2023-10-13 13:54:57 +00:00
print =<< getSizeSubpaths a