hcdu/app/Main.hs

177 lines
5.0 KiB
Haskell
Raw Normal View History

2023-10-16 09:07:50 +00:00
{-# LANGUAGE BlockArguments #-}
2023-10-12 13:01:36 +00:00
module Main where
2023-10-16 13:57:45 +00:00
import System.Posix.Directory ( getWorkingDirectory, changeWorkingDirectory )
2023-10-14 11:49:13 +00:00
import System.Directory ( pathIsSymbolicLink
, doesFileExist
, getFileSize
, doesDirectoryExist )
import Data.Functor ( (<&>) )
import Control.Monad ( filterM )
2023-10-16 13:57:45 +00:00
import Control.Monad.IO.Class
2023-10-14 11:49:13 +00:00
import System.Directory.Extra ( listContents )
2023-10-16 13:57:45 +00:00
import System.IO.Error
2023-10-14 11:49:13 +00:00
import Brick
2023-10-16 09:07:50 +00:00
import Brick.Widgets.Edit
2023-10-14 11:49:13 +00:00
import Graphics.Vty
import Data.List.Extra
import qualified Data.Ord
2023-10-16 09:07:50 +00:00
import Data.Maybe
2023-10-12 14:37:54 +00:00
2023-10-13 13:54:57 +00:00
listNonSymFiles :: FilePath -> IO [FilePath]
2023-10-16 09:07:50 +00:00
listNonSymFiles dir = filterM ((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]
2023-10-16 09:07:50 +00:00
listNonSymDirectories dir = filterM ((not <$>) . pathIsSymbolicLink) =<< filterM doesDirectoryExist =<< listContents dir
listSym :: FilePath -> IO [FilePath]
listSym dir = filterM pathIsSymbolicLink =<< listContents dir
2023-10-13 13:24:26 +00:00
2023-10-16 13:57:45 +00:00
handlePermissionError :: IOError -> IO Integer
handlePermissionError e = if isPermissionError e then pure 0 else ioError e
2023-10-13 13:54:57 +00:00
getSizeSubpaths :: FilePath -> IO [(FilePath, Integer)]
getSizeSubpaths x = do
sub <- listNonSymDirectories x
2023-10-16 13:57:45 +00:00
subby <- mapM
(\y -> (y,) . sum . map snd <$>
getSizeSubpaths y)
sub
2023-10-16 09:07:50 +00:00
syms <- listSym x
2023-10-16 13:57:45 +00:00
local <- listNonSymFiles x >>=
(\y -> mapM
(flip catchIOError handlePermissionError . getFileSize) y <&>
zip y)
2023-10-16 09:07:50 +00:00
pure $ sortOn (Data.Ord.Down . snd) $ local ++ subby ++ zip syms [0..0]
2023-10-14 11:35:22 +00:00
2023-10-16 13:57:45 +00:00
data ScrollDirection where
SUp :: ScrollDirection
SDown :: ScrollDirection
2023-10-14 11:35:22 +00:00
data AppS = AppS
{
_appCWD :: FilePath
, _appCursor :: Int
, _appFocus :: Maybe FilePath
, _appSubFiles :: [(FilePath, Integer)]
}
2023-10-16 09:07:50 +00:00
app :: App AppS e ()
2023-10-14 11:35:22 +00:00
app =
2023-10-16 09:07:50 +00:00
App { appDraw = pure . browse
2023-10-16 13:57:45 +00:00
, appHandleEvent = eventHandler
2023-10-14 11:35:22 +00:00
, appStartEvent = pure ()
2023-10-16 09:07:50 +00:00
, appAttrMap = attributeMap
, appChooseCursor = showFirstCursor
2023-10-14 11:35:22 +00:00
}
2023-10-16 09:07:50 +00:00
attributeMap :: AppS -> AttrMap
attributeMap = const $ attrMap defAttr
[ (attrName "selected", withStyle defAttr reverseVideo)
, (editAttr, fg brightBlack)
]
select :: Widget () -> Widget ()
select = withAttr (attrName "selected")
browse :: AppS -> Widget ()
browse s =
str "Path" <+> padLeft Max (str "Size") <=>
viewport () Vertical (foldr (widgetCons s) emptyWidget (_appSubFiles s))
widgetCons :: AppS -> (FilePath, Integer) -> Widget () -> Widget ()
2023-10-16 13:57:45 +00:00
widgetCons s w@(f,_) ws =
(<=> ws) if Just f == _appFocus s then
select . visible $ pathWidget w else
pathWidget w
2023-10-16 09:07:50 +00:00
pathWidget :: (FilePath, Integer) -> Widget ()
pathWidget (f, s) = str (show f) <+> padLeft Max (str (show s))
2023-10-14 11:35:22 +00:00
sizeDir :: AppS -> IO AppS
sizeDir s = do
subFiles <- getSizeSubpaths $ _appCWD s
2023-10-16 14:14:18 +00:00
pure $
s {
_appCursor = 0
, _appFocus = map fst subFiles !? 0
, _appSubFiles = subFiles
}
2023-10-14 11:35:22 +00:00
2023-10-16 09:07:50 +00:00
changeDir :: AppS -> IO AppS
changeDir so
2023-10-16 13:57:45 +00:00
| isJust $ _appFocus so = do
allowed <- doesDirectoryExist path
if allowed then do
changeWorkingDirectory path
let s =
so {
_appCWD = path
, _appSubFiles = []
} in
sizeDir s else
pure so
2023-10-16 09:07:50 +00:00
| otherwise = pure so
2023-10-16 13:57:45 +00:00
where path = fromJust $ _appFocus so
overDir :: IO AppS
overDir = do
changeWorkingDirectory ".."
a <- getWorkingDirectory
changeDir $ initialState a
2023-10-14 11:35:22 +00:00
scroll :: ScrollDirection -> AppS -> AppS
scroll d s = s {
_appCursor = newCursor
, _appFocus = maybeNewPath
}
where cursor =
_appCursor s +
case d of
SUp -> (-1)
SDown -> 1
2023-10-16 13:57:45 +00:00
newCursor = max 0 (min (subtract 1 . length $ _appSubFiles s) cursor)
2023-10-14 11:35:22 +00:00
maybeNewPath = fst <$> _appSubFiles s !? newCursor
2023-10-16 13:57:45 +00:00
eventHandler :: BrickEvent () e -> EventM () AppS ()
eventHandler (VtyEvent (EvKey k _)) = do
s <- get
case k of
(KChar 'q') -> halt
KEsc -> halt
(KChar 'j') -> put $ scroll SDown s
(KChar 'k') -> put $ scroll SUp s
KDown -> put $ scroll SDown s
KUp -> put $ scroll SUp s
(KChar 'h') -> put =<< liftIO overDir
KLeft -> put =<< liftIO overDir
2023-10-16 09:07:50 +00:00
2023-10-16 13:57:45 +00:00
(KChar ' ') -> put =<< liftIO (changeDir s)
KEnter -> put =<< liftIO (changeDir s)
(KChar 'l') -> put =<< liftIO (changeDir s)
KRight -> put =<< liftIO (changeDir s)
_ -> continueWithoutRedraw
eventHandler _ = undefined
initialState :: FilePath -> AppS
initialState f =
AppS {
_appCWD = f
, _appCursor = 0
, _appFocus = pure f
, _appSubFiles = []
}
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-16 09:07:50 +00:00
b <- changeDir $ initialState a
_ <- defaultMain app b
return ()