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

Update logic for detecting if the output stream is connected to a terminal #2084

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Changes from 1 commit
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
Next Next commit
Update logic for detecting if the output stream is connected to a ter…
…minal

#2083
  • Loading branch information
cushon committed Aug 11, 2023
commit 3212b4a3b5bf24ac412c3275270c512585b08ca9
13 changes: 12 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -17840,7 +17840,18 @@ static final boolean forceEnabled() { return System.getenv("CLICOLOR_FORCE") !=
&& !"0".equals(System.getenv("CLICOLOR_FORCE"));}
/** http://stackoverflow.com/questions/1403772/how-can-i-check-if-a-java-programs-input-output-streams-are-connected-to-a-term */
static boolean calcTTY() {
try { return System.class.getDeclaredMethod("console").invoke(null) != null; }
try {
Object console = System.class.getDeclaredMethod("console").invoke(null);
if (console == null) {
return false;
}
try {
Method isTerminal = Console.class.getDeclaredMethod("isTerminal");
Copy link
Owner

Choose a reason for hiding this comment

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

Looks good, but the Console class was introduced in Java 6, and picocli supports Java 5, so can you change this to find the Console class via reflection?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done

return (boolean) isTerminal.invoke(console);
} catch (NoSuchMethodException e) {
return true;
}
}
catch (Throwable reflectionFailed) { return true; }
}
/** Cygwin and MSYS use pseudo-tty and console is always null... */
Expand Down
Loading