module String.ANSI
  ( -- $intro

    -- * Foreground color
    black,
    red,
    green,
    yellow,
    blue,
    magenta,
    cyan,
    white,
    brightBlack,
    brightRed,
    brightGreen,
    brightYellow,
    brightBlue,
    brightMagenta,
    brightCyan,
    brightWhite,
    rgb,

    -- * Background color
    blackBg,
    redBg,
    greenBg,
    yellowBg,
    blueBg,
    magentaBg,
    cyanBg,
    whiteBg,
    brightBlackBg,
    brightRedBg,
    brightGreenBg,
    brightYellowBg,
    brightBlueBg,
    brightMagentaBg,
    brightCyanBg,
    brightWhiteBg,
    rgbBg,

    -- * Style
    bold,
    faint,
    italic,
    underline,
    doubleUnderline,
    strikethrough,
    frame,
    encircle,
    overline,
  )
where

import qualified Data.Text as Text
import Data.Text.Builder.Linear (Builder)
import qualified Data.Text.Builder.Linear as Builder
import Data.Word (Word8)
import qualified Text.Builder.ANSI as Builder.ANSI

-- $intro
--
-- Text styling for ANSI terminals using SGR codes, as defined by the
-- <https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf ECMA-48>
-- standard.
--
-- Supports foreground\/background color, bold\/faint intensity, italic,
-- single\/double underline, strikethrough, frame, encircle, and overline escape
-- sequences. Some styles may not work on your terminal.
--
-- Also features terminal detection, so redirecting styled output to a file will
-- automatically strip the ANSI escape sequences.

-- | Black foreground.
black :: String -> String
black :: String -> String
black =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.black
{-# INLINE black #-}

-- | Red foreground.
red :: String -> String
red :: String -> String
red =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.red
{-# INLINE red #-}

-- | Green foreground.
green :: String -> String
green :: String -> String
green =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.green
{-# INLINE green #-}

-- | Yellow foreground.
yellow :: String -> String
yellow :: String -> String
yellow =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.yellow
{-# INLINE yellow #-}

-- | Blue foreground.
blue :: String -> String
blue :: String -> String
blue =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.blue
{-# INLINE blue #-}

-- | Magenta foreground.
magenta :: String -> String
magenta :: String -> String
magenta =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.magenta
{-# INLINE magenta #-}

-- | Cyan foreground.
cyan :: String -> String
cyan :: String -> String
cyan =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.cyan
{-# INLINE cyan #-}

-- | White foreground.
white :: String -> String
white :: String -> String
white =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.white
{-# INLINE white #-}

-- | Bright black foreground.
brightBlack :: String -> String
brightBlack :: String -> String
brightBlack =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightBlack
{-# INLINE brightBlack #-}

-- | Bright red foreground.
brightRed :: String -> String
brightRed :: String -> String
brightRed =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightRed
{-# INLINE brightRed #-}

-- | Bright green foreground.
brightGreen :: String -> String
brightGreen :: String -> String
brightGreen =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightGreen
{-# INLINE brightGreen #-}

-- | Bright yellow foreground.
brightYellow :: String -> String
brightYellow :: String -> String
brightYellow =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightYellow
{-# INLINE brightYellow #-}

-- | Bright blue foreground.
brightBlue :: String -> String
brightBlue :: String -> String
brightBlue =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightBlue
{-# INLINE brightBlue #-}

-- | Bright magenta foreground.
brightMagenta :: String -> String
brightMagenta :: String -> String
brightMagenta =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightMagenta
{-# INLINE brightMagenta #-}

-- | Bright cyan foreground.
brightCyan :: String -> String
brightCyan :: String -> String
brightCyan =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightCyan
{-# INLINE brightCyan #-}

-- | Bright white foreground.
brightWhite :: String -> String
brightWhite :: String -> String
brightWhite =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightWhite
{-# INLINE brightWhite #-}

-- | RGB foreground.
rgb :: Word8 -> Word8 -> Word8 -> String -> String
rgb :: Word8 -> Word8 -> Word8 -> String -> String
rgb Word8
r Word8
g Word8
b =
  (Builder -> Builder) -> String -> String
lift (Word8 -> Word8 -> Word8 -> Builder -> Builder
Builder.ANSI.rgb Word8
r Word8
g Word8
b)
{-# INLINE rgb #-}

-- | Black background.
blackBg :: String -> String
blackBg :: String -> String
blackBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.blackBg
{-# INLINE blackBg #-}

-- | Red background.
redBg :: String -> String
redBg :: String -> String
redBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.redBg
{-# INLINE redBg #-}

-- | Green background.
greenBg :: String -> String
greenBg :: String -> String
greenBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.greenBg
{-# INLINE greenBg #-}

-- | Yellow background.
yellowBg :: String -> String
yellowBg :: String -> String
yellowBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.yellowBg
{-# INLINE yellowBg #-}

-- | Blue background.
blueBg :: String -> String
blueBg :: String -> String
blueBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.blueBg
{-# INLINE blueBg #-}

-- | Magenta background.
magentaBg :: String -> String
magentaBg :: String -> String
magentaBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.magentaBg
{-# INLINE magentaBg #-}

-- | Cyan background.
cyanBg :: String -> String
cyanBg :: String -> String
cyanBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.cyanBg
{-# INLINE cyanBg #-}

-- | White background.
whiteBg :: String -> String
whiteBg :: String -> String
whiteBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.whiteBg
{-# INLINE whiteBg #-}

-- | Bright black background.
brightBlackBg :: String -> String
brightBlackBg :: String -> String
brightBlackBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightBlackBg
{-# INLINE brightBlackBg #-}

-- | Bright red background.
brightRedBg :: String -> String
brightRedBg :: String -> String
brightRedBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightRedBg
{-# INLINE brightRedBg #-}

-- | Bright green background.
brightGreenBg :: String -> String
brightGreenBg :: String -> String
brightGreenBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightGreenBg
{-# INLINE brightGreenBg #-}

-- | Bright yellow background.
brightYellowBg :: String -> String
brightYellowBg :: String -> String
brightYellowBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightYellowBg
{-# INLINE brightYellowBg #-}

-- | Bright blue background.
brightBlueBg :: String -> String
brightBlueBg :: String -> String
brightBlueBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightBlueBg
{-# INLINE brightBlueBg #-}

-- | Bright magenta background.
brightMagentaBg :: String -> String
brightMagentaBg :: String -> String
brightMagentaBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightMagentaBg
{-# INLINE brightMagentaBg #-}

-- | Bright cyan background.
brightCyanBg :: String -> String
brightCyanBg :: String -> String
brightCyanBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightCyanBg
{-# INLINE brightCyanBg #-}

-- | Bright white background.
brightWhiteBg :: String -> String
brightWhiteBg :: String -> String
brightWhiteBg =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.brightWhiteBg
{-# INLINE brightWhiteBg #-}

-- | RGB background.
rgbBg :: Word8 -> Word8 -> Word8 -> String -> String
rgbBg :: Word8 -> Word8 -> Word8 -> String -> String
rgbBg Word8
r Word8
g Word8
b =
  (Builder -> Builder) -> String -> String
lift (Word8 -> Word8 -> Word8 -> Builder -> Builder
Builder.ANSI.rgbBg Word8
r Word8
g Word8
b)
{-# INLINE rgbBg #-}

-- | __Bold__ style (high intensity).
bold :: String -> String
bold :: String -> String
bold =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.bold
{-# INLINE bold #-}

-- | Faint style (low intensity).
faint :: String -> String
faint :: String -> String
faint =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.faint
{-# INLINE faint #-}

-- | /Italic/ style.
italic :: String -> String
italic :: String -> String
italic =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.italic
{-# INLINE italic #-}

-- | U̲n̲d̲e̲r̲l̲i̲n̲e̲ style.
underline :: String -> String
underline :: String -> String
underline =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.underline
{-# INLINE underline #-}

-- | D̳o̳u̳b̳l̳e̳ ̳u̳n̳d̳e̳r̳l̳i̳n̳e̳ style.
doubleUnderline :: String -> String
doubleUnderline :: String -> String
doubleUnderline =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.doubleUnderline
{-# INLINE doubleUnderline #-}

-- | S̶t̶r̶i̶k̶e̶t̶h̶r̶o̶u̶g̶h̶ style.
strikethrough :: String -> String
strikethrough :: String -> String
strikethrough =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.strikethrough
{-# INLINE strikethrough #-}

-- | Frame style.
frame :: String -> String
frame :: String -> String
frame =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.frame
{-# INLINE frame #-}

-- | Encircle style.
encircle :: String -> String
encircle :: String -> String
encircle =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.encircle
{-# INLINE encircle #-}

-- | O̅v̅e̅r̅l̅i̅n̅e̅ style.
overline :: String -> String
overline :: String -> String
overline =
  (Builder -> Builder) -> String -> String
lift Builder -> Builder
Builder.ANSI.overline
{-# INLINE overline #-}

--

lift :: (Builder -> Builder) -> String -> String
lift :: (Builder -> Builder) -> String -> String
lift Builder -> Builder
f =
  Text -> String
Text.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
Builder.runBuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Builder
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Char -> Builder
Builder.fromChar
-- Don't inline before phase 2
{-# NOINLINE [2] lift #-}

-- Collapse lift/lift to a single lift before phase 2
{-# RULES
"lift/lift" [~2] forall f g s.
  lift f (lift g s) =
    lift (f . g) s
  #-}