module Neovim.User.Choice
where
import Neovim
import Data.Char (toLower)
import Data.List (isPrefixOf)
oneOf :: [String] -> Neovim env (Maybe String)
oneOf cs = fmap (\i -> cs !! (i1)) <$> askForIndex (zipWith mkChoice cs [1..])
where
mkChoice :: String -> Int -> Object
mkChoice c i = toObject $ show i <> ". " <> c
askForIndex :: [Object] -> Neovim env (Maybe Int)
askForIndex cs = vim_call_function "inputlist" [ObjectArray cs] >>= \case
Left e ->
err $ exceptionToDoc 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 env (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 env Bool
yesOrNo message = do
spec <- vim_call_function
"inputdialog" $ (message ++ " (Y/n) ")
+: ("" :: String) +: ("no" :: String) +: []
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