Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trailing slash from FileBrowser initial directory specification. #508

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Brick/Widgets/FileBrowser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ import qualified Data.Text as T
import Data.Monoid
#endif
import Data.Int (Int64)
import Data.List (sortBy, isSuffixOf)
import Data.List (sortBy, isSuffixOf, dropWhileEnd)
import qualified Data.Set as Set
import qualified Data.Vector as V
import Lens.Micro
Expand Down Expand Up @@ -282,7 +282,7 @@ newFileBrowser :: (FileInfo -> Bool)
-> IO (FileBrowser n)
newFileBrowser selPredicate name mCwd = do
initialCwd <- FP.normalise <$> case mCwd of
Just path -> return path
Just path -> return $ removeTrailingSlash path
Nothing -> D.getCurrentDirectory

let b = FileBrowser { fileBrowserWorkingDirectory = initialCwd
Expand All @@ -298,6 +298,22 @@ newFileBrowser selPredicate name mCwd = do

setWorkingDirectory initialCwd b

-- | Removes any trailing slash(es) from the supplied FilePath (which should
-- indicate a directory). This does not remove a sole slash indicating the root
-- directory.
--
-- This is done because if the FileBrowser is initialized with an initial working
-- directory that ends in a slash, then selecting the "../" entry to move to the
-- parent directory will cause the removal of the trailing slash, but it will not
-- otherwise cause any change, misleading the user into thinking no action was
-- taken (the disappearance of the trailing slash is unlikely to be noticed).
-- All subsequent parent directory selection operations are processed normally,
-- and the 'fileBrowserWorkingDirectory' never ends in a trailing slash
-- thereafter (except at the root directory).
removeTrailingSlash :: FilePath -> FilePath
removeTrailingSlash "/" = "/"
removeTrailingSlash d = dropWhileEnd (== '/') d

-- | A file entry selector that permits selection of all file entries
-- except directories. Use this if you want users to be able to navigate
-- directories in the browser. If you want users to be able to select
Expand Down
Loading