module Neovim.User.Choice
where
import Neovim
import Data.Char (toLower)
import Data.List (isPrefixOf)
import Text.PrettyPrint.ANSI.Leijen as P hiding ((<$>))
oneOf :: Pretty a => [a] -> Neovim r st (Maybe a)
oneOf cs = fmap (\i -> cs !! (i1)) <$> askForIndex (zipWith mkChoice cs [1..])
where
mkChoice c i = toObject $ int i P.<> text "." <+> pretty c
askForIndex :: [Object] -> Neovim r st (Maybe Int)
askForIndex cs = vim_call_function "inputlist" [ObjectArray cs] >>= \case
Left e ->
(err . text . show) e
Right a -> case fromObject a of
Right i | i >= 1 && i <= length cs ->
return $ Just i
Right _ ->
return Nothing
Left e ->
err e
oneOfS :: Show a => [a] -> Neovim r st (Maybe a)
oneOfS cs = fmap (\i -> cs !! (i1)) <$> askForIndex (zipWith mkChoice cs [1..])
where
mkChoice c i = toObject $ show (i :: Int) ++ ". " ++ show c
yesOrNo :: String
-> Neovim r st Bool
yesOrNo message = do
spec <- vim_call_function
"inputdialog" $ (message ++ " (Y/n) ") +: "" +: "no" +: []
case fmap fromObject spec of
Right (Right s) | map toLower s `isPrefixOf` "yes" ->
return True
Right (Right s) | map toLower s `isPrefixOf` "no" ->
return False
Right (Left e) ->
err e
_ ->
yesOrNo message