{-# LANGUAGE OverloadedStrings #-}
module Vimeta.UI.Term.Common
( byline
, notEmpty
, execVimetaBylineApp
) where
import Control.Monad
import Control.Monad.Trans.Class (lift)
import Data.Text (Text)
import qualified Data.Text as Text
import System.Console.Byline hiding (ask)
import System.Exit (exitSuccess, exitFailure)
import Vimeta.Core
import Vimeta.Core.Vimeta (Vimeta(..))
byline :: Byline IO a -> Vimeta (Byline IO) a
byline = Vimeta . lift . lift
notEmpty :: Stylized -> Text -> IO (Either Stylized Text)
notEmpty errortxt input = return $ if Text.length clean > 0
then Right clean
else Left errortxt
where
clean :: Text
clean = Text.strip input
execVimetaBylineApp :: (Config -> Config) -> Vimeta (Byline IO) () -> IO ()
execVimetaBylineApp cf vimeta = void $ runByline $ do
v <- execVimeta cf vimeta
case v of
Right _ -> liftIO exitSuccess
Left e -> reportLn Error (text $ Text.pack e) >> liftIO exitFailure