{- |
Copyright: (c) 2017-2019 Kowainik
SPDX-License-Identifier: MPL-2.0
Maintainer: Kowainik <xrom.xkov@gmail.com>

This module contains functions for colorful printing into terminal.
-}

module Summoner.Ansi
       ( boldDefault
       , putStrFlush
       , prompt
       , successMessage
       , warningMessage
       , errorMessage
       , infoMessage
       , skipMessage
       ) where

import Colourista (blue, bold, formatWith)
import System.IO (hFlush)

import qualified Colourista
import qualified Data.Text as T


-- | Explicit flush ensures prompt messages are in the correct order on all systems.
putStrFlush :: Text -> IO ()
putStrFlush :: Text -> IO ()
putStrFlush msg :: Text
msg = do
    Text -> IO ()
forall (m :: * -> *). MonadIO m => Text -> m ()
putText Text
msg
    Handle -> IO ()
hFlush Handle
stdout

{- | Read 'Text' from standard input after arrow prompt.
-}
prompt :: IO Text
prompt :: IO Text
prompt = do
    Text -> IO ()
putStrFlush (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ [Text] -> Text -> Text
forall str. (IsString str, Semigroup str) => [str] -> str -> str
formatWith [Text
forall str. IsString str => str
blue] "  ->   "
    Text -> Text
T.strip (Text -> Text) -> IO Text -> IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Text
forall (m :: * -> *). MonadIO m => m Text
getLine

boldDefault :: Text -> Text
boldDefault :: Text -> Text
boldDefault message :: Text
message = [Text] -> Text -> Text
forall str. (IsString str, Semigroup str) => [str] -> str -> str
formatWith [Text
forall str. IsString str => str
bold] (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ " [" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
message Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "]"

errorMessage, warningMessage, successMessage, infoMessage, skipMessage :: Text -> IO ()
errorMessage :: Text -> IO ()
errorMessage   = Text -> IO ()
Colourista.errorMessage   (Text -> IO ()) -> (Text -> Text) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
indent
warningMessage :: Text -> IO ()
warningMessage = Text -> IO ()
Colourista.warningMessage (Text -> IO ()) -> (Text -> Text) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
indent
successMessage :: Text -> IO ()
successMessage = Text -> IO ()
Colourista.successMessage (Text -> IO ()) -> (Text -> Text) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
indent
infoMessage :: Text -> IO ()
infoMessage    = Text -> IO ()
Colourista.infoMessage    (Text -> IO ()) -> (Text -> Text) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
indent
skipMessage :: Text -> IO ()
skipMessage    = Text -> IO ()
Colourista.skipMessage    (Text -> IO ()) -> (Text -> Text) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
indent

-- | Add 2 spaces in front.
indent :: Text -> Text
indent :: Text -> Text
indent = ("  " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>)