From 262413cc699225bb6aa7506c3667d7a8e1906216 Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 17 Oct 2023 13:21:03 +0200 Subject: [PATCH] Can't go above root --- app/Main.hs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index b4304e0..4f4de4d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -16,6 +16,7 @@ import System.IO.Error import Brick import Brick.Widgets.Edit import Graphics.Vty +import Data.List ( isPrefixOf ) import Data.List.Extra ( sortOn , (!?)) import qualified Data.Ord @@ -35,7 +36,10 @@ listSym dir = filterM pathIsSymbolicLink =<< listContents dir handlePermissionError :: IOError -> IO Integer handlePermissionError e = if isPermissionError e then pure 0 else ioError e -getSizeSubpaths :: FilePath -> IORef (IOHashMap FilePath [(FilePath, Integer)]) -> IO [(FilePath, Integer)] +getSizeSubpaths :: + FilePath -> + IORef (IOHashMap FilePath [(FilePath, Integer)]) -> + IO [(FilePath, Integer)] getSizeSubpaths x i = do memo <- readIORef i val <- lookup x memo @@ -64,11 +68,12 @@ data ScrollDirection where data AppS = AppS { - appCWD :: FilePath - , appCursor :: Int - , appFocus :: Maybe FilePath + appCWD :: FilePath + , appCursor :: Int + , appFocus :: Maybe FilePath , appSubFiles :: [(FilePath, Integer)] - , appMemo :: IORef (IOHashMap FilePath [(FilePath, Integer)]) + , appMemo :: IORef (IOHashMap FilePath [(FilePath, Integer)]) + , appRoot :: FilePath } app :: App AppS e () @@ -150,12 +155,14 @@ overDir :: AppS -> IO AppS overDir s = do changeWorkingDirectory ".." a <- getWorkingDirectory - changeDir $ s { - appCWD = a - , appCursor = 0 - , appFocus = pure a - , appSubFiles = [] - } + if a `isPrefixOf` appRoot s && a /= appRoot s then + pure s else + changeDir $ s { + appCWD = a + , appCursor = 0 + , appFocus = pure a + , appSubFiles = [] + } scroll :: ScrollDirection -> AppS -> AppS scroll d s = s { @@ -194,7 +201,10 @@ eventHandler (VtyEvent (EvKey k _)) = do eventHandler (VtyEvent (EvResize _ _)) = pure () eventHandler _ = undefined -initialState :: FilePath -> IORef (IOHashMap FilePath [(FilePath, Integer)]) -> AppS +initialState :: + FilePath -> + IORef (IOHashMap FilePath [(FilePath, Integer)]) -> + AppS initialState f i = AppS { appCWD = f @@ -202,6 +212,7 @@ initialState f i = , appFocus = pure f , appSubFiles = [] , appMemo = i + , appRoot = f } main :: IO ()