From 364c33395e2f2d5500307f01989f70241c247d5a Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 25 Jul 2021 14:41:49 -0700 Subject: [PATCH] Don't print colors when $TERM is 'dumb' or unset (fixes #2260) --- CHANGELOG.md | 1 + src/ShellCheck/Formatter/Format.hs | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62e516a..9e87af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixed - SC2102 about repetitions in ranges no longer triggers on [[ -v arr[xx] ]] - SC2290: Warn about misused = in declare & co, which were not caught by SC2270+ +- The flag --color=auto no longer outputs color when TERM is "dumb" or unset ### Changed - SC2048: Warning about $\* now also applies to ${array[\*]} diff --git a/src/ShellCheck/Formatter/Format.hs b/src/ShellCheck/Formatter/Format.hs index cb7dfe6..53b59a4 100644 --- a/src/ShellCheck/Formatter/Format.hs +++ b/src/ShellCheck/Formatter/Format.hs @@ -28,6 +28,7 @@ import Data.Array import Data.List import System.IO import System.Info +import System.Environment -- A formatter that carries along an arbitrary piece of data data Formatter = Formatter { @@ -68,12 +69,14 @@ makeNonVirtual comments contents = shouldOutputColor :: ColorOption -> IO Bool -shouldOutputColor colorOption = do - term <- hIsTerminalDevice stdout - let windows = "mingw" `isPrefixOf` os - let isUsableTty = term && not windows - let useColor = case colorOption of - ColorAlways -> True - ColorNever -> False - ColorAuto -> isUsableTty - return useColor +shouldOutputColor colorOption = + case colorOption of + ColorAlways -> return True + ColorNever -> return False + ColorAuto -> do + isTerminal <- hIsTerminalDevice stdout + term <- lookupEnv "TERM" + let windows = "mingw" `isPrefixOf` os + let dumbTerm = term `elem` [Just "dumb", Just "", Nothing] + let isUsableTty = isTerminal && not windows && not dumbTerm + return isUsableTty