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

Auto detect Jansi #483

Closed
charphi opened this issue Sep 17, 2018 · 4 comments
Closed

Auto detect Jansi #483

charphi opened this issue Sep 17, 2018 · 4 comments
Milestone

Comments

@charphi
Copy link
Contributor

charphi commented Sep 17, 2018

It would be usefull to make Ansi.AUTO detect if Jansi is available and enabled at runtime.
Thus, we wouldn't have to specify it manually.

For example:

AnsiConsole.systemInstall(); // Jansi magic
CommandLine.run(new WindowsJansiDemo(), args);
AnsiConsole.systemUninstall();

instead of

AnsiConsole.systemInstall(); // Jansi magic
CommandLine.run(new WindowsJansiDemo(), System.err, Ansi.ON, args);
AnsiConsole.systemUninstall();
@remkop
Copy link
Owner

remkop commented Sep 17, 2018

Makes a lot of sense. I like this idea a lot.

What is a good way to detect that Jansi is installed? (I'm not sure that it is enough just to check that the Jansi jar is on the classpath...)

@charphi
Copy link
Contributor Author

charphi commented Sep 17, 2018

It seems that you just need to compare AnsiConsole.out and System.out such as:
AnsiConsole.out == System.out

Here is a quick example code:

static boolean isJansiEnabled() {
	try {
		Class<?> ansiConsole = Class.forName("org.fusesource.jansi.AnsiConsole");
		Field out = ansiConsole.getField("out");
		return out.get(null) == System.out;
	} catch (Exception ex) {
		return false;
	}
}

public static void main(String[] args) {
	System.out.println(isJansiEnabled());
	AnsiConsole.systemInstall();
	System.out.println(isJansiEnabled());
	AnsiConsole.systemUninstall();
	System.out.println(isJansiEnabled());
}

@remkop
Copy link
Owner

remkop commented Sep 17, 2018

Perfect!

Do you feel like providing a PR for this?

@remkop
Copy link
Owner

remkop commented Sep 20, 2018

#486 has been merged. Thanks for the contribution!

@remkop remkop closed this as completed Sep 20, 2018
@remkop remkop added this to the 3.6.1 milestone Sep 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants