mirror of
https://github.com/xmonad/xmonad-contrib.git
synced 2025-08-13 03:05:57 -07:00
ShellPrompt: traverse $PATH once per invocation. Major speed improvement
This commit is contained in:
@@ -52,22 +52,26 @@ data Shell = Shell
|
|||||||
instance XPrompt Shell where
|
instance XPrompt Shell where
|
||||||
showXPrompt Shell = "Run: "
|
showXPrompt Shell = "Run: "
|
||||||
|
|
||||||
|
|
||||||
shellPrompt :: XPConfig -> X ()
|
shellPrompt :: XPConfig -> X ()
|
||||||
shellPrompt c = mkXPrompt Shell c getShellCompl spawn
|
shellPrompt c = do
|
||||||
|
cmds <- io $ getCommands
|
||||||
|
mkXPrompt Shell c (getShellCompl cmds) spawn
|
||||||
|
|
||||||
getShellCompl :: String -> IO [String]
|
getShellCompl :: [String] -> String -> IO [String]
|
||||||
getShellCompl s
|
getShellCompl cmds s | s == "" || last s == ' ' = return []
|
||||||
| s /= "" && last s /= ' ' = do
|
|
||||||
f <- fmap lines $ runProcessWithInput "/bin/bash" [] ("compgen -A file " ++ s ++ "\n")
|
|
||||||
c <- commandCompletionFunction s
|
|
||||||
return . map escape . sort . (toList . fromList) $ f ++ c
|
|
||||||
| otherwise = return []
|
|
||||||
|
|
||||||
commandCompletionFunction :: String -> IO [String]
|
|
||||||
commandCompletionFunction str
|
|
||||||
| '/' `elem` str = return []
|
|
||||||
| otherwise = do
|
| otherwise = do
|
||||||
|
f <- fmap lines $ runProcessWithInput "/bin/bash" [] ("compgen -A file " ++ s ++ "\n")
|
||||||
|
return . map escape . uniqSort $ f ++ commandCompletionFunction cmds s
|
||||||
|
|
||||||
|
uniqSort :: Ord a => [a] -> [a]
|
||||||
|
uniqSort = toList . fromList
|
||||||
|
|
||||||
|
commandCompletionFunction :: [String] -> String -> [String]
|
||||||
|
commandCompletionFunction cmds str | '/' `elem` str = []
|
||||||
|
| otherwise = filter (isPrefixOf str) cmds
|
||||||
|
|
||||||
|
getCommands :: IO [String]
|
||||||
|
getCommands = do
|
||||||
p <- getEnv "PATH" `catch` const (return [])
|
p <- getEnv "PATH" `catch` const (return [])
|
||||||
let ds = split ':' p
|
let ds = split ':' p
|
||||||
fp d f = d ++ "/" ++ f
|
fp d f = d ++ "/" ++ f
|
||||||
@@ -76,7 +80,7 @@ commandCompletionFunction str
|
|||||||
if exists
|
if exists
|
||||||
then getDirectoryContents d >>= filterM (isExecutable . fp d)
|
then getDirectoryContents d >>= filterM (isExecutable . fp d)
|
||||||
else return []
|
else return []
|
||||||
return . filter (isPrefixOf str) . concat $ es
|
return . uniqSort . concat $ es
|
||||||
|
|
||||||
isExecutable :: FilePath ->IO Bool
|
isExecutable :: FilePath ->IO Bool
|
||||||
isExecutable f = do
|
isExecutable f = do
|
||||||
|
Reference in New Issue
Block a user