Add XMonad.Prompt.Zsh

This commit is contained in:
Zubin Duggal 2020-06-13 04:07:09 +05:30
parent 8ba646aec6
commit 8c39850dc0
No known key found for this signature in database
GPG Key ID: 7CCFC277A14C97A7
3 changed files with 70 additions and 0 deletions

View File

@ -50,6 +50,11 @@
Declare any window as a scratchpad on the fly. Once declared, the
scratchpad behaves like `XMonad.Util.NamedScratchpad`.
* `XMonad.Prompt.Zsh`
A version of `XMonad.Prompt.Shell` that lets you use completions supplied by
zsh.
### Bug Fixes and Minor Changes
* `XMonad.Util.Run`

64
XMonad/Prompt/Zsh.hs Normal file
View File

@ -0,0 +1,64 @@
{- |
Module : XMonad.Prompt.Zsh
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\/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
"XMonad.Doc.Extending#Editing_key_bindings". -}
data Zsh = Zsh
instance XPrompt Zsh where
showXPrompt Zsh = "Run: "
completionToCommand _ = stripZsh
commandToComplete _ s = s
nextCompletion _ s cs = getNextCompletion s (map stripZsh cs)
zshPrompt :: XPConfig -> FilePath -> X ()
zshPrompt c capture = mkXPrompt Zsh c (getZshCompl capture) (\x -> safeSpawn "zsh" ["-c",x])
getZshCompl :: FilePath -> String -> IO [String]
getZshCompl capture s
| s == "" = return []
| otherwise = processCompls <$> runProcessWithInput capture [s] ""
where processCompls = map (\x -> (skipLastWord s ++ filter (/= '\r') x)) . lines
-- | Removes the argument description from the zsh completion
stripZsh :: String -> String
stripZsh "" = ""
stripZsh (' ':'-':'-':' ':_) = ""
stripZsh (x:xs) = x : stripZsh xs

View File

@ -316,6 +316,7 @@ library
XMonad.Prompt.Window
XMonad.Prompt.Workspace
XMonad.Prompt.XMonad
XMonad.Prompt.Zsh
XMonad.Util.Cursor
XMonad.Util.CustomKeys
XMonad.Util.DebugWindow