Skip to content

Commit

Permalink
Use bash from $SHELL when it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Jan 17, 2024
1 parent c6cc612 commit e2fd501
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Options/Applicative/Builder/Completer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import Prelude
import Control.Exception (IOException, try)
import Data.List (isPrefixOf)
#ifdef MIN_VERSION_process
import Data.List (isSuffixOf)
import System.Process (readProcess)
import System.Environment (lookupEnv)
#endif

import Options.Applicative.Types
Expand All @@ -40,8 +42,23 @@ bashCompleter :: String -> Completer
#ifdef MIN_VERSION_process
bashCompleter action = Completer $ \word -> do
let cmd = unwords ["compgen", "-A", action, "--", requote word]
result <- tryIO $ readProcess "bash" ["-c", cmd] ""
bash <- getBash
result <- tryIO $ readProcess bash ["-c", cmd] ""
return . lines . either (const []) id $ result

-- | Determines the bash executable. Ideally we'd invoke the same bash that
-- is currently active. If $SHELL does not seem to be set to a bash executable
-- we don't assume $SHELL is bash and we take bash from the $PATH.
-- This fixes file completion in cases where a virtual environment with a
-- non-interactive bash is loaded with direnv, nix-shell or similar.
getBash :: IO String
getBash = do
shellEnv <- lookupEnv "SHELL"
pure (case shellEnv of
Just exe | "/bash" `isSuffixOf` exe -> exe
_ -> "bash"
)

#else
bashCompleter = const $ Completer $ const $ return []
#endif
Expand Down

0 comments on commit e2fd501

Please sign in to comment.