Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better ANSI support detection. #43

Merged
merged 3 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 10 additions & 1 deletion Sources/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ protocol ANSIConvertible : Error, CustomStringConvertible {

extension ANSIConvertible {
func print() {
if isatty(fileno(stderr)) != 0 {
// 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)
}
Copy link
Contributor

@AliSoftware AliSoftware Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but not sure it's worth having that private function anymore since XcodeColors isn't checked anymore and only the TERM env var has to be read

So using this directly for line 16 would suffice 😛

let termType = getenv("TERM").flatMap({ String(cString: $0) })

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work since getenv returns UnsafeMutablePointer not an optional.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so add the method back in again? It is clear what it does, and nicely wraps around unsafe pointer stuff.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 not sure to understand how the method we removed worked then… does if let value = getenv(…) magically do something special with UnsafeMutablePointer?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, seems UnsafeMutablePointer is actually force-unwrapped (!). So there is no optional, but if let would still work.

}


Expand Down