{-# LANGUAGE OverloadedStrings #-}

module ColorOption
  ( ColorWhen(..)
  , shouldUseColor
  ) where

import Types (ColorWhen(..))
import System.IO (hIsTerminalDevice, stdout)
import System.Environment (lookupEnv)

-- | Determine if color should be used based on the color option
shouldUseColor :: ColorWhen -> IO Bool
shouldUseColor :: ColorWhen -> IO Bool
shouldUseColor ColorWhen
Always = Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
shouldUseColor ColorWhen
Never = Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
shouldUseColor ColorWhen
Auto = do
  -- Respect NO_COLOR environment variable per https://no-color.org
  noColor <- String -> IO (Maybe String)
lookupEnv String
"NO_COLOR"
  case noColor of
    Just String
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False  -- NO_COLOR is set, disable colors
    Maybe String
Nothing -> Handle -> IO Bool
hIsTerminalDevice Handle
stdout  -- Check if stdout is a terminal