module XMonad.Prompt.DirExec
(
dirExecPrompt
, dirExecPromptNamed
, DirExec
) where
import Control.Exception as E
import System.Directory
import XMonad
import XMonad.Prelude
import XMonad.Prompt
econst :: Monad m => a -> IOException -> m a
econst :: forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst = forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return
newtype DirExec = DirExec String
instance XPrompt DirExec where
showXPrompt :: DirExec -> String
showXPrompt (DirExec String
name) = String
name
dirExecPrompt :: XPConfig -> (String -> X ()) -> FilePath -> X ()
dirExecPrompt :: XPConfig -> (String -> X ()) -> String -> X ()
dirExecPrompt XPConfig
cfg String -> X ()
runner String
path = do
let name :: String
name = (forall a. [a] -> [a] -> [a]
++ String
": ") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
last
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([String
"Root"] forall a. [a] -> [a] -> [a]
++)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\Char
x -> if Char
x forall a. Eq a => a -> a -> Bool
== Char
'/' then Char
' ' else Char
x)
forall a b. (a -> b) -> a -> b
$ String
path
XPConfig -> (String -> X ()) -> String -> String -> X ()
dirExecPromptNamed XPConfig
cfg String -> X ()
runner String
path String
name
dirExecPromptNamed :: XPConfig -> (String -> X ()) -> FilePath -> String -> X ()
dirExecPromptNamed :: XPConfig -> (String -> X ()) -> String -> String -> X ()
dirExecPromptNamed XPConfig
cfg String -> X ()
runner String
path String
name = do
let path' :: String
path' = String
path forall a. [a] -> [a] -> [a]
++ String
"/"
[String]
cmds <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ ComplFunction
getDirectoryExecutables String
path'
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> DirExec
DirExec String
name) XPConfig
cfg (forall {m :: * -> *} {a}.
(Monad m, Eq a) =>
[[a]] -> [a] -> m [[a]]
compList [String]
cmds) (String -> X ()
runner forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
path' forall a. [a] -> [a] -> [a]
++))
where
compList :: [[a]] -> [a] -> m [[a]]
compList [[a]]
cmds [a]
s = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => [a] -> [a] -> Bool
isInfixOf [a]
s) forall a b. (a -> b) -> a -> b
$ [[a]]
cmds
getDirectoryExecutables :: FilePath -> IO [String]
getDirectoryExecutables :: ComplFunction
getDirectoryExecutables String
path =
(ComplFunction
getDirectoryContents String
path forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM (\String
x -> let x' :: String
x' = String
path forall a. [a] -> [a] -> [a]
++ String
x in
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(&&)
(String -> IO Bool
doesFileExist String
x')
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Permissions -> Bool
executable (String -> IO Permissions
getPermissions String
x'))))
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst []