{- |
Module      :  XMonad.Prompt.Zsh
Description :  Zsh-specific version of "XMonad.Prompt.Shell".
Copyright   :  (C) 2020 Zubin Duggal
License     :  BSD3

Maintainer  :  zubin.duggal@gmail.com
Stability   :  unstable
Portability :  unportable

A version of "XMonad.Prompt.Shell" that lets you access the awesome power of Zsh
completions in your xmonad prompt
-}

module XMonad.Prompt.Zsh
    ( -- * Usage
      -- $usage
      Zsh (..)
    , zshPrompt
    -- * Utility functions
    , getZshCompl
    , stripZsh
    ) where

import XMonad
import XMonad.Prompt
import XMonad.Util.Run

{- $usage
1. Grab the @capture.zsh@ script to capture zsh completions from <https://github.com/Valodim/zsh-capture-completion>
2. In your @xmonad.hs@:

> import XMonad.Prompt
> import XMonad.Prompt.Zsh

3. In your keybindings add something like:

>   , ((modm .|. controlMask, xK_x), zshPrompt def "/path/to/capture.zsh")

For detailed instruction on editing the key binding see
<https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>. -}

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

-- | Removes the argument description from the zsh completion
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