{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NamedFieldPuns #-}
module XMonad.Prompt.Shell
(
Shell (..)
, shellPrompt
, safePrompt
, safeDirPrompt
, unsafePrompt
, prompt
, compgenDirectories
, compgenFiles
, getCommands
, getBrowser
, getEditor
, getShellCompl
, getShellCompl'
, split
) where
import Codec.Binary.UTF8.String (encodeString)
import Control.Exception as E
import Data.Bifunctor (bimap)
import System.Directory (getDirectoryContents)
import System.Environment (getEnv)
import System.Posix.Files (getFileStatus, isDirectory)
import XMonad hiding (config)
import XMonad.Prelude
import XMonad.Prompt
import XMonad.Util.Run
econst :: Monad m => a -> IOException -> m a
econst :: forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst = m a -> IOException -> m a
forall a b. a -> b -> a
const (m a -> IOException -> m a)
-> (a -> m a) -> a -> IOException -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
data Shell = Shell
type Predicate = String -> String -> Bool
instance XPrompt Shell where
showXPrompt :: Shell -> [Char]
showXPrompt Shell
Shell = [Char]
"Run: "
completionToCommand :: Shell -> [Char] -> [Char]
completionToCommand Shell
_ = [Char] -> [Char]
escape
shellPrompt :: XPConfig -> X ()
shellPrompt :: XPConfig -> X ()
shellPrompt XPConfig
c = do
cmds <- IO [[Char]] -> X [[Char]]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO [[Char]]
getCommands
mkXPrompt Shell c (getShellCompl cmds $ searchPredicate c) spawn
prompt, unsafePrompt, safePrompt :: FilePath -> XPConfig -> X ()
prompt :: [Char] -> XPConfig -> X ()
prompt = [Char] -> XPConfig -> X ()
unsafePrompt
safePrompt :: [Char] -> XPConfig -> X ()
safePrompt [Char]
c XPConfig
config = Shell -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
mkXPrompt Shell
Shell XPConfig
config ([[Char]] -> Predicate -> ComplFunction
getShellCompl [[Char]
c] (Predicate -> ComplFunction) -> Predicate -> ComplFunction
forall a b. (a -> b) -> a -> b
$ XPConfig -> Predicate
searchPredicate XPConfig
config) [Char] -> X ()
run
where run :: [Char] -> X ()
run = [Char] -> [[Char]] -> X ()
forall (m :: * -> *). MonadIO m => [Char] -> [[Char]] -> m ()
safeSpawn [Char]
c ([[Char]] -> X ()) -> ([Char] -> [[Char]]) -> [Char] -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return
unsafePrompt :: [Char] -> XPConfig -> X ()
unsafePrompt [Char]
c XPConfig
config = Shell -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
mkXPrompt Shell
Shell XPConfig
config ([[Char]] -> Predicate -> ComplFunction
getShellCompl [[Char]
c] (Predicate -> ComplFunction) -> Predicate -> ComplFunction
forall a b. (a -> b) -> a -> b
$ XPConfig -> Predicate
searchPredicate XPConfig
config) [Char] -> X ()
forall (m :: * -> *). MonadIO m => [Char] -> m ()
run
where run :: [Char] -> m ()
run [Char]
a = [Char] -> m ()
forall (m :: * -> *). MonadIO m => [Char] -> m ()
unsafeSpawn ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
a
safeDirPrompt
:: FilePath
-> XPConfig
-> String
-> X ()
safeDirPrompt :: [Char] -> XPConfig -> [Char] -> X ()
safeDirPrompt [Char]
cmd cfg :: XPConfig
cfg@XPC{ Predicate
searchPredicate :: XPConfig -> Predicate
searchPredicate :: Predicate
searchPredicate } [Char]
compgenStr =
Shell -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
mkXPrompt Shell
Shell XPConfig
cfg ComplFunction
mkCompl ([Char] -> [[Char]] -> X ()
forall (m :: * -> *). MonadIO m => [Char] -> [[Char]] -> m ()
safeSpawn [Char]
cmd ([[Char]] -> X ()) -> ([Char] -> [[Char]]) -> [Char] -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
where
mkCompl :: String -> IO [String]
mkCompl :: ComplFunction
mkCompl [Char]
input =
ComplCaseSensitivity
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [Char] -> ComplFunction
shellComplImpl
ComplCaseSensitivity
CaseSensitive
(([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Predicate
searchPredicate [Char]
ext))
([[Char]] -> Predicate -> [Char] -> [[Char]]
commandCompletionFunction [[Char]
cmd] Predicate
searchPredicate [Char]
input)
(if [Char]
"/" Predicate
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` [Char]
input then [Char]
dir else [Char]
compgenStr)
[Char]
input
where
([Char]
ext, [Char]
dir) :: (String, String)
= ([Char] -> [Char])
-> ([Char] -> [Char]) -> ([Char], [Char]) -> ([Char], [Char])
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap [Char] -> [Char]
forall a. [a] -> [a]
reverse [Char] -> [Char]
forall a. [a] -> [a]
reverse (([Char], [Char]) -> ([Char], [Char]))
-> ([Char] -> ([Char], [Char])) -> [Char] -> ([Char], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> ([Char], [Char])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') ([Char] -> ([Char], [Char]))
-> ([Char] -> [Char]) -> [Char] -> ([Char], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
forall a. [a] -> [a]
reverse ([Char] -> ([Char], [Char])) -> [Char] -> ([Char], [Char])
forall a b. (a -> b) -> a -> b
$ [Char]
input
getShellCompl :: [String] -> Predicate -> String -> IO [String]
getShellCompl :: [[Char]] -> Predicate -> ComplFunction
getShellCompl = ComplCaseSensitivity -> [[Char]] -> Predicate -> ComplFunction
getShellCompl' ComplCaseSensitivity
CaseSensitive
getShellCompl' :: ComplCaseSensitivity -> [String] -> Predicate -> String -> IO [String]
getShellCompl' :: ComplCaseSensitivity -> [[Char]] -> Predicate -> ComplFunction
getShellCompl' ComplCaseSensitivity
csn [[Char]]
cmds Predicate
p [Char]
input =
ComplCaseSensitivity
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [Char] -> ComplFunction
shellComplImpl ComplCaseSensitivity
csn [[Char]] -> [[Char]]
forall a. a -> a
id ([[Char]] -> Predicate -> [Char] -> [[Char]]
commandCompletionFunction [[Char]]
cmds Predicate
p [Char]
input) [Char]
input [Char]
input
shellComplImpl
:: ComplCaseSensitivity
-> ([String] -> [String])
-> [String]
-> String
-> String
-> IO [String]
shellComplImpl :: ComplCaseSensitivity
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [Char] -> ComplFunction
shellComplImpl ComplCaseSensitivity
csn [[Char]] -> [[Char]]
filterFiles [[Char]]
cmds [Char]
cmpgenStr [Char]
input
| [Char]
input Predicate
forall a. Eq a => a -> a -> Bool
== [Char]
"" Bool -> Bool -> Bool
|| [Char] -> Char
forall a. HasCallStack => [a] -> a
last [Char]
input Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' = [[Char]] -> IO [[Char]]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
| Bool
otherwise = do
choices <- [[Char]] -> [[Char]]
filterFiles ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines ([Char] -> [[Char]]) -> IO [Char] -> IO [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ComplCaseSensitivity -> [Char] -> IO [Char]
compgenFiles ComplCaseSensitivity
csn [Char]
cmpgenStr
files <- case choices of
[[Char]
x] -> do fs <- [Char] -> IO FileStatus
getFileStatus ([Char] -> [Char]
encodeString [Char]
x)
pure $ if isDirectory fs then [x ++ "/"] else [x]
[[Char]]
_ -> [[Char]] -> IO [[Char]]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [[Char]]
choices
pure . sortBy typedFirst . uniqSort $ files ++ cmds
where
typedFirst :: String -> String -> Ordering
typedFirst :: [Char] -> [Char] -> Ordering
typedFirst [Char]
x [Char]
y
| [Char]
x Predicate
`startsWith` [Char]
input Bool -> Bool -> Bool
&& Bool -> Bool
not ([Char]
y Predicate
`startsWith` [Char]
input) = Ordering
LT
| [Char]
y Predicate
`startsWith` [Char]
input Bool -> Bool -> Bool
&& Bool -> Bool
not ([Char]
x Predicate
`startsWith` [Char]
input) = Ordering
GT
| Bool
otherwise = [Char]
x [Char] -> [Char] -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` [Char]
y
startsWith :: String -> String -> Bool
startsWith :: Predicate
startsWith [Char]
str [Char]
ps = (Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower [Char]
ps Predicate
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` (Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower [Char]
str
compgenFiles :: ComplCaseSensitivity -> String -> IO String
compgenFiles :: ComplCaseSensitivity -> [Char] -> IO [Char]
compgenFiles ComplCaseSensitivity
csn = ComplCaseSensitivity -> [Char] -> [Char] -> IO [Char]
compgen ComplCaseSensitivity
csn [Char]
"file"
compgenDirectories :: ComplCaseSensitivity -> String -> IO String
compgenDirectories :: ComplCaseSensitivity -> [Char] -> IO [Char]
compgenDirectories ComplCaseSensitivity
csn = ComplCaseSensitivity -> [Char] -> [Char] -> IO [Char]
compgen ComplCaseSensitivity
csn [Char]
"directory"
compgen :: ComplCaseSensitivity -> String -> String -> IO String
compgen :: ComplCaseSensitivity -> [Char] -> [Char] -> IO [Char]
compgen ComplCaseSensitivity
csn [Char]
actionOpt [Char]
s = [Char] -> [[Char]] -> [Char] -> IO [Char]
forall (m :: * -> *).
MonadIO m =>
[Char] -> [[Char]] -> [Char] -> m [Char]
runProcessWithInput [Char]
"bash" [] ([Char] -> IO [Char]) -> [Char] -> IO [Char]
forall a b. (a -> b) -> a -> b
$
ComplCaseSensitivity -> [Char]
complCaseSensitivityCmd ComplCaseSensitivity
csn [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" ; " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char] -> [Char]
compgenCmd [Char]
actionOpt [Char]
s
complCaseSensitivityCmd :: ComplCaseSensitivity -> String
complCaseSensitivityCmd :: ComplCaseSensitivity -> [Char]
complCaseSensitivityCmd ComplCaseSensitivity
CaseSensitive =
[Char]
"bind 'set completion-ignore-case off'"
complCaseSensitivityCmd ComplCaseSensitivity
CaseInSensitive =
[Char]
"bind 'set completion-ignore-case on'"
compgenCmd :: String -> String -> String
compgenCmd :: [Char] -> [Char] -> [Char]
compgenCmd [Char]
actionOpt [Char]
s = [Char]
"compgen -A " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
actionOpt [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" -- " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n"
commandCompletionFunction :: [String] -> Predicate -> String -> [String]
commandCompletionFunction :: [[Char]] -> Predicate -> [Char] -> [[Char]]
commandCompletionFunction [[Char]]
cmds Predicate
p [Char]
str | Char
'/' Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char]
str = []
| Bool
otherwise = ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Predicate
p [Char]
str) [[Char]]
cmds
getCommands :: IO [String]
getCommands :: IO [[Char]]
getCommands = do
p <- [Char] -> IO [Char]
getEnv [Char]
"PATH" IO [Char] -> (IOException -> IO [Char]) -> IO [Char]
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` [Char] -> IOException -> IO [Char]
forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst []
let ds = ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Predicate
forall a. Eq a => a -> a -> Bool
/= [Char]
"") ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ Char -> [Char] -> [[Char]]
forall a. Eq a => a -> [a] -> [[a]]
split Char
':' [Char]
p
es <- forM ds $ \[Char]
d -> ComplFunction
getDirectoryContents [Char]
d IO [[Char]] -> (IOException -> IO [[Char]]) -> IO [[Char]]
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` [[Char]] -> IOException -> IO [[Char]]
forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst []
return . uniqSort . filter (not . ("." `isPrefixOf`)) . concat $ es
split :: Eq a => a -> [a] -> [[a]]
split :: forall a. Eq a => a -> [a] -> [[a]]
split a
_ [] = []
split a
e [a]
l =
[a]
f [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: a -> [a] -> [[a]]
forall a. Eq a => a -> [a] -> [[a]]
split a
e (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
1 [a]
ls)
where
([a]
f,[a]
ls) = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
e) [a]
l
escape :: String -> String
escape :: [Char] -> [Char]
escape [] = [Char]
""
escape (Char
x:[Char]
xs)
| Char -> Bool
isSpecialChar Char
x = Char
'\\' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Char
x Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
escape [Char]
xs
| Bool
otherwise = Char
x Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
escape [Char]
xs
isSpecialChar :: Char -> Bool
isSpecialChar :: Char -> Bool
isSpecialChar = (Char -> [Char] -> Bool) -> [Char] -> Char -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem [Char]
" &\\@\"'#?$*()[]{};"
env :: String -> String -> IO String
env :: [Char] -> [Char] -> IO [Char]
env [Char]
variable [Char]
fallthrough = [Char] -> IO [Char]
getEnv [Char]
variable IO [Char] -> (IOException -> IO [Char]) -> IO [Char]
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` [Char] -> IOException -> IO [Char]
forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst [Char]
fallthrough
getBrowser :: IO String
getBrowser :: IO [Char]
getBrowser = [Char] -> [Char] -> IO [Char]
env [Char]
"BROWSER" [Char]
"firefox"
getEditor :: IO String
getEditor :: IO [Char]
getEditor = [Char] -> [Char] -> IO [Char]
env [Char]
"EDITOR" [Char]
"emacs"