module Nbparts.Util.Prompt where

import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
import System.IO (hFlush, stdout)

confirm :: Text -> IO Bool
confirm :: Text -> IO Bool
confirm Text
prompt = do
  Text -> IO ()
Text.putStr (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ Text
prompt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (Y/n) "
  Handle -> IO ()
hFlush Handle
stdout
  Text
res <- IO Text
Text.getLine
  case Text -> Text
Text.toLower Text
res of
    Text
"y" -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
    Text
_
      | Text -> Bool
Text.null Text
res -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
      | Bool
otherwise -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False