hcdu/app/Main.hs

86 lines
2.5 KiB
Haskell
Raw Normal View History

2023-10-12 13:01:36 +00:00
module Main where
2023-10-14 11:49:13 +00:00
import System.Posix.Directory ( getWorkingDirectory )
import System.Directory
( pathIsSymbolicLink
, doesFileExist
, getFileSize
, doesDirectoryExist )
import Data.Functor ( (<&>) )
import Control.Monad ( filterM )
import System.Directory.Extra ( listContents )
import Brick
import Graphics.Vty
import Data.List.Extra
import qualified Data.Ord
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)
2023-10-14 11:49:13 +00:00
pure $ sortOn (Data.Ord.Down . snd) $ local ++ subby
2023-10-14 11:35:22 +00:00
data AppS = AppS
{
_appCWD :: FilePath
, _appCursor :: Int
, _appFocus :: Maybe FilePath
, _appSubFiles :: [(FilePath, Integer)]
}
data ScrollDirection where
SUp :: ScrollDirection
SDown :: ScrollDirection
ui :: Widget ()
ui = viewport () Vertical $ str "Svamp är gott"
app :: App () e ()
app =
App { appDraw = const [ui]
, appHandleEvent = resizeOrQuit
, appStartEvent = pure ()
, appAttrMap = const $ attrMap (white `on` blue) []
, appChooseCursor = neverShowCursor
}
sizeDir :: AppS -> IO AppS
sizeDir s = do
subFiles <- getSizeSubpaths $ _appCWD s
pure $ s {_appSubFiles = subFiles}
changeDir :: AppS -> FilePath -> IO AppS
changeDir so f =
let s = so {
_appCWD = f
, _appSubFiles = []
} in
sizeDir s
scroll :: ScrollDirection -> AppS -> AppS
scroll d s = s {
_appCursor = newCursor
, _appFocus = maybeNewPath
}
where cursor =
_appCursor s +
case d of
SUp -> (-1)
SDown -> 1
newCursor = max 0 (min ((-) 1 . length $ _appSubFiles s) cursor)
maybeNewPath = fst <$> _appSubFiles s !? newCursor
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
2023-10-14 11:35:22 +00:00
defaultMain app ()