module FortyTwo.Prompts.Confirm (confirm, confirmWithDefault) where
import qualified Data.Text as T
import FortyTwo.Renderers.Confirm (renderConfirm)
import FortyTwo.Renderers.Question (renderQuestion)
import FortyTwo.Utils (clearLines, flush)
import FortyTwo.Constants (emptyString)
normalizeString :: String -> String
normalizeString s = take 1 $ T.unpack $ T.strip . T.toLower $ T.pack s
getCleanConfirm :: IO String
getCleanConfirm = do s <- getLine; return $ normalizeString s
confirmWithDefault :: String -> Bool -> IO Bool
confirmWithDefault question defaultAnswer = do
putStrLn emptyString
renderQuestion question defaultAnswerHumanized emptyString
renderConfirm
flush
answer <- getCleanConfirm
clearLines 1
if answer == "n" || (answer /= "y" && not defaultAnswer) then do
renderQuestion question emptyString "no"
return False
else do
renderQuestion question emptyString "yes"
return True
where
defaultAnswerHumanized = if defaultAnswer then "yes" else "no"
confirm :: String -> IO Bool
confirm question = confirmWithDefault question False