module Options.Applicative.BashCompletion
( bashCompletionParser
) where
import Control.Applicative
import Prelude
import Data.Foldable ( asum )
import Data.List ( isPrefixOf )
import Data.Maybe ( fromMaybe, listToMaybe )
import Options.Applicative.Builder
import Options.Applicative.Common
import Options.Applicative.Internal
import Options.Applicative.Types
import Options.Applicative.Help.Pretty
import Options.Applicative.Help.Chunk
data Richness
= Standard
| Enriched Int Int
deriving (Richness -> Richness -> Bool
(Richness -> Richness -> Bool)
-> (Richness -> Richness -> Bool) -> Eq Richness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Richness -> Richness -> Bool
$c/= :: Richness -> Richness -> Bool
== :: Richness -> Richness -> Bool
$c== :: Richness -> Richness -> Bool
Eq, Eq Richness
Eq Richness =>
(Richness -> Richness -> Ordering)
-> (Richness -> Richness -> Bool)
-> (Richness -> Richness -> Bool)
-> (Richness -> Richness -> Bool)
-> (Richness -> Richness -> Bool)
-> (Richness -> Richness -> Richness)
-> (Richness -> Richness -> Richness)
-> Ord Richness
Richness -> Richness -> Bool
Richness -> Richness -> Ordering
Richness -> Richness -> Richness
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Richness -> Richness -> Richness
$cmin :: Richness -> Richness -> Richness
max :: Richness -> Richness -> Richness
$cmax :: Richness -> Richness -> Richness
>= :: Richness -> Richness -> Bool
$c>= :: Richness -> Richness -> Bool
> :: Richness -> Richness -> Bool
$c> :: Richness -> Richness -> Bool
<= :: Richness -> Richness -> Bool
$c<= :: Richness -> Richness -> Bool
< :: Richness -> Richness -> Bool
$c< :: Richness -> Richness -> Bool
compare :: Richness -> Richness -> Ordering
$ccompare :: Richness -> Richness -> Ordering
$cp1Ord :: Eq Richness
Ord, Int -> Richness -> ShowS
[Richness] -> ShowS
Richness -> String
(Int -> Richness -> ShowS)
-> (Richness -> String) -> ([Richness] -> ShowS) -> Show Richness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Richness] -> ShowS
$cshowList :: [Richness] -> ShowS
show :: Richness -> String
$cshow :: Richness -> String
showsPrec :: Int -> Richness -> ShowS
$cshowsPrec :: Int -> Richness -> ShowS
Show)
bashCompletionParser :: ParserInfo a -> ParserPrefs -> Parser CompletionResult
bashCompletionParser :: ParserInfo a -> ParserPrefs -> Parser CompletionResult
bashCompletionParser pinfo :: ParserInfo a
pinfo pprefs :: ParserPrefs
pprefs = Parser CompletionResult
complParser
where
failure :: (String -> IO [String]) -> CompletionResult
failure opts :: String -> IO [String]
opts = CompletionResult :: (String -> IO String) -> CompletionResult
CompletionResult
{ execCompletion :: String -> IO String
execCompletion = \progn :: String
progn -> [String] -> String
unlines ([String] -> String) -> IO [String] -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO [String]
opts String
progn }
complParser :: Parser CompletionResult
complParser = [Parser CompletionResult] -> Parser CompletionResult
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
[ (String -> IO [String]) -> CompletionResult
failure ((String -> IO [String]) -> CompletionResult)
-> Parser (String -> IO [String]) -> Parser CompletionResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
( ParserInfo a
-> ParserPrefs
-> Richness
-> [String]
-> Int
-> String
-> IO [String]
forall a.
ParserInfo a
-> ParserPrefs
-> Richness
-> [String]
-> Int
-> String
-> IO [String]
bashCompletionQuery ParserInfo a
pinfo ParserPrefs
pprefs
(Richness -> [String] -> Int -> String -> IO [String])
-> Parser Richness
-> Parser ([String] -> Int -> String -> IO [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ( (Int -> Int -> Richness)
-> Mod FlagFields (Int -> Int -> Richness)
-> Parser (Int -> Int -> Richness)
forall a. a -> Mod FlagFields a -> Parser a
flag' Int -> Int -> Richness
Enriched (String -> Mod FlagFields (Int -> Int -> Richness)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "bash-completion-enriched" Mod FlagFields (Int -> Int -> Richness)
-> Mod FlagFields (Int -> Int -> Richness)
-> Mod FlagFields (Int -> Int -> Richness)
forall a. Monoid a => a -> a -> a
`mappend` Mod FlagFields (Int -> Int -> Richness)
forall (f :: * -> *) a. Mod f a
internal)
Parser (Int -> Int -> Richness)
-> Parser Int -> Parser (Int -> Richness)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Int
forall a. Read a => ReadM a
auto (String -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "bash-completion-option-desc-length" Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields Int
forall (f :: * -> *) a. Mod f a
internal Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Monoid a => a -> a -> a
`mappend` Int -> Mod OptionFields Int
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value 40)
Parser (Int -> Richness) -> Parser Int -> Parser Richness
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Int
forall a. Read a => ReadM a
auto (String -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "bash-completion-command-desc-length" Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields Int
forall (f :: * -> *) a. Mod f a
internal Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Monoid a => a -> a -> a
`mappend` Int -> Mod OptionFields Int
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value 40)
Parser Richness -> Parser Richness -> Parser Richness
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Richness -> Parser Richness
forall (f :: * -> *) a. Applicative f => a -> f a
pure Richness
Standard
)
Parser ([String] -> Int -> String -> IO [String])
-> Parser [String] -> Parser (Int -> String -> IO [String])
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser String -> Parser [String]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser String -> Parser [String])
-> (Mod OptionFields String -> Parser String)
-> Mod OptionFields String
-> Parser [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mod OptionFields String -> Parser String
forall s. IsString s => Mod OptionFields s -> Parser s
strOption) (String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "bash-completion-word"
Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields String
forall (f :: * -> *) a. Mod f a
internal)
Parser (Int -> String -> IO [String])
-> Parser Int -> Parser (String -> IO [String])
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Int
forall a. Read a => ReadM a
auto (String -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "bash-completion-index" Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields Int
forall (f :: * -> *) a. Mod f a
internal) )
, (String -> IO [String]) -> CompletionResult
failure ((String -> IO [String]) -> CompletionResult)
-> Parser (String -> IO [String]) -> Parser CompletionResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
(String -> String -> IO [String]
bashCompletionScript (String -> String -> IO [String])
-> Parser String -> Parser (String -> IO [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Mod OptionFields String -> Parser String
forall s. IsString s => Mod OptionFields s -> Parser s
strOption (String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "bash-completion-script" Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields String
forall (f :: * -> *) a. Mod f a
internal))
, (String -> IO [String]) -> CompletionResult
failure ((String -> IO [String]) -> CompletionResult)
-> Parser (String -> IO [String]) -> Parser CompletionResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
(String -> String -> IO [String]
fishCompletionScript (String -> String -> IO [String])
-> Parser String -> Parser (String -> IO [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Mod OptionFields String -> Parser String
forall s. IsString s => Mod OptionFields s -> Parser s
strOption (String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "fish-completion-script" Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields String
forall (f :: * -> *) a. Mod f a
internal))
, (String -> IO [String]) -> CompletionResult
failure ((String -> IO [String]) -> CompletionResult)
-> Parser (String -> IO [String]) -> Parser CompletionResult
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
(String -> String -> IO [String]
zshCompletionScript (String -> String -> IO [String])
-> Parser String -> Parser (String -> IO [String])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Mod OptionFields String -> Parser String
forall s. IsString s => Mod OptionFields s -> Parser s
strOption (String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long "zsh-completion-script" Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Monoid a => a -> a -> a
`mappend` Mod OptionFields String
forall (f :: * -> *) a. Mod f a
internal))
]
bashCompletionQuery :: ParserInfo a -> ParserPrefs -> Richness -> [String] -> Int -> String -> IO [String]
bashCompletionQuery :: ParserInfo a
-> ParserPrefs
-> Richness
-> [String]
-> Int
-> String
-> IO [String]
bashCompletionQuery pinfo :: ParserInfo a
pinfo pprefs :: ParserPrefs
pprefs richness :: Richness
richness ws :: [String]
ws i :: Int
i _ = case Completion a
-> ParserPrefs -> Maybe (Either (SomeParser, ArgPolicy) Completer)
forall r.
Completion r
-> ParserPrefs -> Maybe (Either (SomeParser, ArgPolicy) Completer)
runCompletion Completion a
compl ParserPrefs
pprefs of
Just (Left (SomeParser p :: Parser a
p, a :: ArgPolicy
a))
-> ArgPolicy -> Parser a -> IO [String]
forall a. ArgPolicy -> Parser a -> IO [String]
list_options ArgPolicy
a Parser a
p
Just (Right c :: Completer
c)
-> Completer -> IO [String]
run_completer Completer
c
Nothing
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
where
compl :: Completion a
compl = ParserInfo a -> [String] -> Completion a
forall (m :: * -> *) a. MonadP m => ParserInfo a -> [String] -> m a
runParserInfo ParserInfo a
pinfo (Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop 1 [String]
ws')
list_options :: ArgPolicy -> Parser a -> IO [String]
list_options a :: ArgPolicy
a
= ([[String]] -> [String]) -> IO [[String]] -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
(IO [[String]] -> IO [String])
-> (Parser a -> IO [[String]]) -> Parser a -> IO [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [IO [String]] -> IO [[String]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
([IO [String]] -> IO [[String]])
-> (Parser a -> [IO [String]]) -> Parser a -> IO [[String]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. OptHelpInfo -> Option x -> IO [String])
-> Parser a -> [IO [String]]
forall b a.
(forall x. OptHelpInfo -> Option x -> b) -> Parser a -> [b]
mapParser (ArgPolicy -> OptHelpInfo -> Option x -> IO [String]
forall a. ArgPolicy -> OptHelpInfo -> Option a -> IO [String]
opt_completions ArgPolicy
a)
opt_completions :: ArgPolicy -> OptHelpInfo -> Option a -> IO [String]
opt_completions argPolicy :: ArgPolicy
argPolicy hinfo :: OptHelpInfo
hinfo opt :: Option a
opt = case Option a -> OptReader a
forall a. Option a -> OptReader a
optMain Option a
opt of
OptReader ns :: [OptName]
ns _ _
| ArgPolicy
argPolicy ArgPolicy -> ArgPolicy -> Bool
forall a. Eq a => a -> a -> Bool
/= ArgPolicy
AllPositionals
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String])
-> ([String] -> [String]) -> [String] -> IO [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Option a -> [String] -> [String]
forall (f :: * -> *) a.
Functor f =>
Option a -> f String -> f String
add_opt_help Option a
opt ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ [OptName] -> [String]
show_names [OptName]
ns
| Bool
otherwise
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
FlagReader ns :: [OptName]
ns _
| ArgPolicy
argPolicy ArgPolicy -> ArgPolicy -> Bool
forall a. Eq a => a -> a -> Bool
/= ArgPolicy
AllPositionals
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String])
-> ([String] -> [String]) -> [String] -> IO [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Option a -> [String] -> [String]
forall (f :: * -> *) a.
Functor f =>
Option a -> f String -> f String
add_opt_help Option a
opt ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ [OptName] -> [String]
show_names [OptName]
ns
| Bool
otherwise
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
ArgReader rdr :: CReader a
rdr
| OptHelpInfo -> Bool
hinfoUnreachableArgs OptHelpInfo
hinfo
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise
-> Completer -> IO [String]
run_completer (CReader a -> Completer
forall a. CReader a -> Completer
crCompleter CReader a
rdr)
CmdReader _ ns :: [String]
ns p :: String -> Maybe (ParserInfo a)
p
| OptHelpInfo -> Bool
hinfoUnreachableArgs OptHelpInfo
hinfo
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise
-> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String])
-> ([String] -> [String]) -> [String] -> IO [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Maybe (ParserInfo a)) -> [String] -> [String]
forall (f :: * -> *) a.
Functor f =>
(String -> Maybe (ParserInfo a)) -> f String -> f String
add_cmd_help String -> Maybe (ParserInfo a)
p ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ [String] -> [String]
filter_names [String]
ns
add_opt_help :: Functor f => Option a -> f String -> f String
add_opt_help :: Option a -> f String -> f String
add_opt_help opt :: Option a
opt = case Richness
richness of
Standard ->
f String -> f String
forall a. a -> a
id
Enriched len :: Int
len _ ->
ShowS -> f String -> f String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ShowS -> f String -> f String) -> ShowS -> f String -> f String
forall a b. (a -> b) -> a -> b
$ \o :: String
o ->
let h :: Maybe Doc
h = Chunk Doc -> Maybe Doc
forall a. Chunk a -> Maybe a
unChunk (Chunk Doc -> Maybe Doc) -> Chunk Doc -> Maybe Doc
forall a b. (a -> b) -> a -> b
$ Option a -> Chunk Doc
forall a. Option a -> Chunk Doc
optHelp Option a
opt
in String -> (Doc -> String) -> Maybe Doc -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
o (\h' :: Doc
h' -> String
o String -> ShowS
forall a. [a] -> [a] -> [a]
++ "\t" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Doc -> String
render_line Int
len Doc
h') Maybe Doc
h
add_cmd_help :: Functor f => (String -> Maybe (ParserInfo a)) -> f String -> f String
add_cmd_help :: (String -> Maybe (ParserInfo a)) -> f String -> f String
add_cmd_help p :: String -> Maybe (ParserInfo a)
p = case Richness
richness of
Standard ->
f String -> f String
forall a. a -> a
id
Enriched _ len :: Int
len ->
ShowS -> f String -> f String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ShowS -> f String -> f String) -> ShowS -> f String -> f String
forall a b. (a -> b) -> a -> b
$ \cmd :: String
cmd ->
let h :: Maybe Doc
h = String -> Maybe (ParserInfo a)
p String
cmd Maybe (ParserInfo a) -> (ParserInfo a -> Maybe Doc) -> Maybe Doc
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Chunk Doc -> Maybe Doc
forall a. Chunk a -> Maybe a
unChunk (Chunk Doc -> Maybe Doc)
-> (ParserInfo a -> Chunk Doc) -> ParserInfo a -> Maybe Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserInfo a -> Chunk Doc
forall a. ParserInfo a -> Chunk Doc
infoProgDesc
in String -> (Doc -> String) -> Maybe Doc -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
cmd (\h' :: Doc
h' -> String
cmd String -> ShowS
forall a. [a] -> [a] -> [a]
++ "\t" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Doc -> String
render_line Int
len Doc
h') Maybe Doc
h
show_names :: [OptName] -> [String]
show_names :: [OptName] -> [String]
show_names = [String] -> [String]
filter_names ([String] -> [String])
-> ([OptName] -> [String]) -> [OptName] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OptName -> String) -> [OptName] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map OptName -> String
showOption
render_line :: Int -> Doc -> String
render_line :: Int -> Doc -> String
render_line len :: Int
len doc :: Doc
doc = case String -> [String]
lines (SimpleDoc -> ShowS
displayS (Float -> Int -> Doc -> SimpleDoc
renderPretty 1 Int
len Doc
doc) "") of
[] -> ""
[x :: String
x] -> String
x
x :: String
x : _ -> String
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ "..."
filter_names :: [String] -> [String]
filter_names :: [String] -> [String]
filter_names = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
is_completion
run_completer :: Completer -> IO [String]
run_completer :: Completer -> IO [String]
run_completer c :: Completer
c = Completer -> String -> IO [String]
runCompleter Completer
c (String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe "" ([String] -> Maybe String
forall a. [a] -> Maybe a
listToMaybe [String]
ws''))
(ws' :: [String]
ws', ws'' :: [String]
ws'') = Int -> [String] -> ([String], [String])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
i [String]
ws
is_completion :: String -> Bool
is_completion :: String -> Bool
is_completion =
case [String]
ws'' of
w :: String
w:_ -> String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf String
w
_ -> Bool -> String -> Bool
forall a b. a -> b -> a
const Bool
True
bashCompletionScript :: String -> String -> IO [String]
bashCompletionScript :: String -> String -> IO [String]
bashCompletionScript prog :: String
prog progn :: String
progn = [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return
[ "_" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn String -> ShowS
forall a. [a] -> [a] -> [a]
++ "()"
, "{"
, " local CMDLINE"
, " local IFS=$'\\n'"
, " CMDLINE=(--bash-completion-index $COMP_CWORD)"
, ""
, " for arg in ${COMP_WORDS[@]}; do"
, " CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)"
, " done"
, ""
, " COMPREPLY=( $(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
prog String -> ShowS
forall a. [a] -> [a] -> [a]
++ " \"${CMDLINE[@]}\") )"
, "}"
, ""
, "complete -o filenames -F _" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn String -> ShowS
forall a. [a] -> [a] -> [a]
++ " " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn ]
fishCompletionScript :: String -> String -> IO [String]
fishCompletionScript :: String -> String -> IO [String]
fishCompletionScript prog :: String
prog progn :: String
progn = [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return
[ " function _" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn
, " set -l cl (commandline --tokenize --current-process)"
, " # Hack around fish issue #3934"
, " set -l cn (commandline --tokenize --cut-at-cursor --current-process)"
, " set -l cn (count $cn)"
, " set -l tmpline --bash-completion-enriched --bash-completion-index $cn"
, " for arg in $cl"
, " set tmpline $tmpline --bash-completion-word $arg"
, " end"
, " for opt in (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
prog String -> ShowS
forall a. [a] -> [a] -> [a]
++ " $tmpline)"
, " if test -d $opt"
, " echo -E \"$opt/\""
, " else"
, " echo -E \"$opt\""
, " end"
, " end"
, "end"
, ""
, "complete --no-files --command " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn String -> ShowS
forall a. [a] -> [a] -> [a]
++ " --arguments '(_" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn String -> ShowS
forall a. [a] -> [a] -> [a]
++ ")'"
]
zshCompletionScript :: String -> String -> IO [String]
zshCompletionScript :: String -> String -> IO [String]
zshCompletionScript prog :: String
prog progn :: String
progn = [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return
[ "#compdef " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
progn
, ""
, "local request"
, "local completions"
, "local word"
, "local index=$((CURRENT - 1))"
, ""
, "request=(--bash-completion-enriched --bash-completion-index $index)"
, "for arg in ${words[@]}; do"
, " request=(${request[@]} --bash-completion-word $arg)"
, "done"
, ""
, "IFS=$'\\n' completions=($( " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
prog String -> ShowS
forall a. [a] -> [a] -> [a]
++ " \"${request[@]}\" ))"
, ""
, "for word in $completions; do"
, " local -a parts"
, ""
, " # Split the line at a tab if there is one."
, " IFS=$'\\t' parts=($( echo $word ))"
, ""
, " if [[ -n $parts[2] ]]; then"
, " if [[ $word[1] == \"-\" ]]; then"
, " local desc=(\"$parts[1] ($parts[2])\")"
, " compadd -d desc -- $parts[1]"
, " else"
, " local desc=($(print -f \"%-019s -- %s\" $parts[1] $parts[2]))"
, " compadd -l -d desc -- $parts[1]"
, " fi"
, " else"
, " compadd -f -- $word"
, " fi"
, "done"
]