2023-04-12 17:48:21 +00:00
|
|
|
{- Determining if output is to a terminal.
|
|
|
|
-
|
|
|
|
- Copyright 2023 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
2023-04-12 23:19:22 +00:00
|
|
|
{-# LANGUAGE CPP, ScopedTypeVariables #-}
|
2023-04-12 17:48:21 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
|
|
|
module Utility.Terminal (
|
|
|
|
IsTerminal(..),
|
|
|
|
checkIsTerminal,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import System.IO
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import System.Win32.MinTTY (isMinTTYHandle)
|
|
|
|
import System.Win32.Types
|
|
|
|
import Graphics.Win32.Misc
|
|
|
|
import Control.Exception
|
|
|
|
#endif
|
|
|
|
|
|
|
|
newtype IsTerminal = IsTerminal Bool
|
|
|
|
|
|
|
|
checkIsTerminal :: Handle -> IO IsTerminal
|
|
|
|
checkIsTerminal h = do
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
b <- hIsTerminalDevice h
|
|
|
|
return (IsTerminal b)
|
|
|
|
#else
|
|
|
|
b <- hIsTerminalDevice h
|
|
|
|
if b
|
|
|
|
then return (IsTerminal b)
|
|
|
|
else do
|
|
|
|
h' <- getStdHandle sTD_OUTPUT_HANDLE
|
|
|
|
`catch` \(_ :: IOError) ->
|
|
|
|
return nullHANDLE
|
2023-04-12 23:33:19 +00:00
|
|
|
if h' == nullHANDLE
|
2023-04-12 17:48:21 +00:00
|
|
|
then return (IsTerminal False)
|
|
|
|
else do
|
|
|
|
b' <- isMinTTYHandle h'
|
2024-03-26 17:09:21 +00:00
|
|
|
return (IsTerminal b')
|
2023-04-12 17:48:21 +00:00
|
|
|
#endif
|