{- |
  This module provides the basic features of the package.
-}

module System.Terminal.Core
    (
      Colour (..), set_colours, set_title
    )
  where

import System.Console.ANSI as RAW

-- | Possible terminal colours. (D for Dark, L for Light.)
data Colour =
    DBlack | DBlue | DGreen | DCyan | DRed | DMagenta | DYellow | DWhite |
    LBlack | LBlue | LGreen | LCyan | LRed | LMagenta | LYellow | LWhite
  deriving Enum

decodeC :: Colour -> RAW.Color
decodeC c =
  [
    RAW.Black,
    RAW.Blue,
    RAW.Green,
    RAW.Cyan,
    RAW.Red,
    RAW.Magenta,
    RAW.Yellow,
    RAW.White
  ]
  !! (fromEnum c `mod` 8)

decodeI :: Colour -> RAW.ColorIntensity
decodeI c = if fromEnum c <= 7 then RAW.Dull else RAW.Vivid

{- |
  Set terminal forground and background colours.

  Note that under Windows, the change in colour takes place
  immediately. You may need to flush @stdout@ before calling
  this function. (This is not necessary under Unix, but it's
  probably good practise to do it for portability's sake.)
-}
set_colours :: Colour -> Colour -> IO ()
set_colours f b = RAW.setSGR
  [
    RAW.SetColor RAW.Foreground (decodeI f) (decodeC f),
    RAW.SetColor RAW.Background (decodeI b) (decodeC b)
  ]

-- | Change the title of the [virtual] terminal.
set_title :: String -> IO ()
set_title = RAW.setTitle