From 3675a9d9d73bc9c9333ec8e8f9735564e769a4b3 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Fri, 2 Dec 2016 20:30:18 +0100 Subject: [PATCH 1/3] Better ANSI support detection. --- CHANGELOG.md | 5 +++++ Sources/Error.swift | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e979c21..d3d43c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Commander Changelog +### Bug Fixes + +- Better detection of ANSI support in output tty. + [#43](https://github.com/kylef/Commander/issues/43) + ## 0.6.0 ### Enhancements diff --git a/Sources/Error.swift b/Sources/Error.swift index 11345e8..7e2bce6 100644 --- a/Sources/Error.swift +++ b/Sources/Error.swift @@ -12,12 +12,27 @@ protocol ANSIConvertible : Error, CustomStringConvertible { extension ANSIConvertible { func print() { - if isatty(fileno(stderr)) != 0 { + // Check if Xcode Colors is installed and enabled. + let xcodeColorsEnabled = (getEnvValue("XcodeColors") == "YES") + if xcodeColorsEnabled { + fputs("\(ansiDescription)\n", stderr) + } + + // Check if we are in any term env and the output is a tty. + let termType = getEnvValue("TERM") + if let t = termType, t.lowercased() != "dumb" && isatty(fileno(stdout)) != 0 { fputs("\(ansiDescription)\n", stderr) } else { fputs("\(description)\n", stderr) } } + + private func getEnvValue(_ key: String) -> String? { + guard let value = getenv(key) else { + return nil + } + return String(cString: value) + } } From e22a6ea08b91d822dc3f1ad9d09c7542990db4fd Mon Sep 17 00:00:00 2001 From: David Jennes Date: Mon, 12 Dec 2016 18:21:14 +0100 Subject: [PATCH 2/3] remove XcodeColors detection --- Sources/Error.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Sources/Error.swift b/Sources/Error.swift index 7e2bce6..66cbf43 100644 --- a/Sources/Error.swift +++ b/Sources/Error.swift @@ -12,12 +12,6 @@ protocol ANSIConvertible : Error, CustomStringConvertible { extension ANSIConvertible { func print() { - // Check if Xcode Colors is installed and enabled. - let xcodeColorsEnabled = (getEnvValue("XcodeColors") == "YES") - if xcodeColorsEnabled { - fputs("\(ansiDescription)\n", stderr) - } - // Check if we are in any term env and the output is a tty. let termType = getEnvValue("TERM") if let t = termType, t.lowercased() != "dumb" && isatty(fileno(stdout)) != 0 { From 5b9f09c516684d4698b144a087b0f867394d37e7 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Mon, 12 Dec 2016 20:00:10 +0100 Subject: [PATCH 3/3] Remove unneeded `getEnvValue()` method --- Sources/Error.swift | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Sources/Error.swift b/Sources/Error.swift index 66cbf43..8efea16 100644 --- a/Sources/Error.swift +++ b/Sources/Error.swift @@ -13,20 +13,13 @@ protocol ANSIConvertible : Error, CustomStringConvertible { extension ANSIConvertible { func print() { // Check if we are in any term env and the output is a tty. - let termType = getEnvValue("TERM") - if let t = termType, t.lowercased() != "dumb" && isatty(fileno(stdout)) != 0 { + if let termType = getenv("TERM"), String(cString: termType).lowercased() != "dumb" && + isatty(fileno(stdout)) != 0 { fputs("\(ansiDescription)\n", stderr) } else { fputs("\(description)\n", stderr) } } - - private func getEnvValue(_ key: String) -> String? { - guard let value = getenv(key) else { - return nil - } - return String(cString: value) - } }