module XMonad.Prompt.Zsh
(
Zsh (..)
, zshPrompt
, getZshCompl
, stripZsh
) where
import XMonad
import XMonad.Prompt
import XMonad.Util.Run
data Zsh = Zsh
instance XPrompt Zsh where
showXPrompt :: Zsh -> [Char]
showXPrompt Zsh
Zsh = [Char]
"Run: "
completionToCommand :: Zsh -> [Char] -> [Char]
completionToCommand Zsh
_ = [Char] -> [Char]
stripZsh
commandToComplete :: Zsh -> [Char] -> [Char]
commandToComplete Zsh
_ [Char]
s = [Char]
s
nextCompletion :: Zsh -> [Char] -> [[Char]] -> [Char]
nextCompletion Zsh
_ [Char]
s [[Char]]
cs = [Char] -> [[Char]] -> [Char]
getNextCompletion [Char]
s (([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> [Char]
stripZsh [[Char]]
cs)
zshPrompt :: XPConfig -> FilePath -> X ()
zshPrompt :: XPConfig -> [Char] -> X ()
zshPrompt XPConfig
c [Char]
capture = Zsh -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> ([Char] -> X ()) -> X ()
mkXPrompt Zsh
Zsh XPConfig
c ([Char] -> ComplFunction
getZshCompl [Char]
capture) (\[Char]
x -> [Char] -> [[Char]] -> X ()
forall (m :: * -> *). MonadIO m => [Char] -> [[Char]] -> m ()
safeSpawn [Char]
"zsh" [[Char]
"-c",[Char]
x])
getZshCompl :: FilePath -> String -> IO [String]
getZshCompl :: [Char] -> ComplFunction
getZshCompl [Char]
capture [Char]
s
| [Char]
s [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"" = [[Char]] -> IO [[Char]]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise = [Char] -> [[Char]]
processCompls ([Char] -> [[Char]]) -> IO [Char] -> IO [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> [[Char]] -> [Char] -> IO [Char]
forall (m :: * -> *).
MonadIO m =>
[Char] -> [[Char]] -> [Char] -> m [Char]
runProcessWithInput [Char]
capture [[Char]
s] [Char]
""
where processCompls :: [Char] -> [[Char]]
processCompls = ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (\[Char]
x -> [Char] -> [Char]
skipLastWord [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r') [Char]
x) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines
stripZsh :: String -> String
stripZsh :: [Char] -> [Char]
stripZsh [Char]
"" = [Char]
""
stripZsh (Char
' ':Char
'-':Char
'-':Char
' ':[Char]
_) = [Char]
""
stripZsh (Char
x:[Char]
xs) = Char
x Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
stripZsh [Char]
xs