From 2a2ee4a51e160406522c8c3136dfbc030ad3dcdd Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 25 Dec 2025 11:29:22 +0530 Subject: Fix directory exists check --- daffm.cabal | 1 + lib/Daffm/State.hs | 15 ++++++++------- notes.org | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/daffm.cabal b/daffm.cabal index 493629d..a717d29 100644 --- a/daffm.cabal +++ b/daffm.cabal @@ -26,6 +26,7 @@ common common-config QuasiQuotes MultiWayIf TemplateHaskell + TupleSections default-language: Haskell2010 build-depends: array, diff --git a/lib/Daffm/State.hs b/lib/Daffm/State.hs index 106fee5..bc91ecb 100644 --- a/lib/Daffm/State.hs +++ b/lib/Daffm/State.hs @@ -1,6 +1,7 @@ {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# HLINT ignore "Redundant multi-way if" #-} +{-# HLINT ignore "Use if" #-} module Daffm.State where import qualified Brick.Widgets.Edit as Editor @@ -19,8 +20,8 @@ import qualified Data.Text.Zipper.Generic as Zipper import qualified Data.Vector as Vec import System.Directory (doesPathExist, getCurrentDirectory, getHomeDirectory, getPermissions, getSymbolicLinkTarget, listDirectory, makeAbsolute, readable, setCurrentDirectory) import System.FilePath (joinPath, takeDirectory) -import qualified System.PosixCompat as Posix import qualified System.Posix.User as Posix +import qualified System.PosixCompat as Posix mkEditor :: (Zipper.GenericTextZipper a) => a -> Editor.Editor a FocusTarget mkEditor = Editor.editor FocusCmdline (Just 1) @@ -59,10 +60,10 @@ normalizePath (Text.stripPrefix "~/" -> (Just rest)) = do pure . Text.pack . joinPath $ [home, Text.unpack rest] normalizePath dir = Text.pack <$> makeAbsolute (Text.unpack dir) -withCwdFallback :: FilePathText -> IO FilePathText +withCwdFallback :: FilePathText -> IO (Bool, FilePathText) withCwdFallback path = do exists <- doesPathExist $ Text.unpack path - if exists then pure path else Text.pack <$> getCurrentDirectory + if exists then pure (True, path) else (False,) . Text.pack <$> getCurrentDirectory stripQuotes :: Text.Text -> Text.Text stripQuotes txt = fromMaybe txt (double <|> single) @@ -79,7 +80,7 @@ textAsString f = Text.pack . f . Text.unpack loadDirToState :: FilePathText -> AppState -> IO AppState loadDirToState dir' appState@(AppState {stateCwd, stateListPositionHistory}) = do - normalizedDir <- (normalizePath . stripTrailingSlash . stripQuotes . trim) dir' >>= withCwdFallback + (pathExists, normalizedDir) <- (normalizePath . stripTrailingSlash . stripQuotes . trim) dir' >>= withCwdFallback stat <- Posix.getFileStatus $ Text.unpack normalizedDir let (dir, targetFilePath) = if Posix.isDirectory stat @@ -90,9 +91,9 @@ loadDirToState dir' appState@(AppState {stateCwd, stateListPositionHistory}) = d then do setCurrentDirectory $ Text.unpack dir newState <- dirToAppState dir targetFilePath - doesPathExist (Text.unpack dir') >>= \case - True -> pure newState - _ -> pure newState {stateMessage = Just $ "No such file or directory: " <> dir'} + pure $ case pathExists of + True -> newState + _ -> newState {stateMessage = Just $ "No such file or directory: " <> dir'} else do let list = L.list FocusMain (Vec.fromList []) 1 pure $ (withNewDir dir list appState) {stateMessage = Just "Unable to read directory"} diff --git a/notes.org b/notes.org index 2ad6eff..c45685a 100644 --- a/notes.org +++ b/notes.org @@ -5,7 +5,7 @@ - [X] fix problems with substitutions (maybe switch to env vars?) - [X] handle permissions errors - [X] Show error messages (invalid commands/shell errors) -- [ ] fix issue with pathExists check for ~ expansion +- [X] fix issue with pathExists check for ~ expansion - [ ] add substution for last directory (or automark on cd) - [ ] allow escaping % in commands - [ ] escape % in file paths while substituting -- cgit v1.3.1