All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rnorth.ansi.CapabilityDetection Maven / Gradle / Ivy

package org.rnorth.ansi;

import com.sun.jna.Platform;

/**
 * Simple terminal capability detection.
 */
public class CapabilityDetection {
    private static final int STDOUT_FILENO = 1;

    private CapabilityDetection() {
        // Utility class - don't allow construction;
    }

    public static boolean isUnderIDEA() {
        // Rough check for whether or not we're running in IDEA
        String classPath = System.getProperty("java.class.path");
        return classPath.contains("idea_rt.jar");
    }

    public static boolean isUnderMaven() {
        // Rough check for whether or not we're running in a Maven build
        return  findClass("org.apache.maven.surefire.booter.ForkedBooter") ||
                findClass("org.codehaus.plexus.classworlds.launcher.Launcher");
    }

    private static boolean findClass(String className) {
        try {
            Class.forName(className);
            return true;
        } catch (Exception ignored) {
            return false;
        }
    }

    public static boolean isTty() {
        // Don't attempt to check on Windows
        if (Platform.isWindows()) {
            return false;
        }

        // Check native isatty using JNA. If we fail for any reason, assume no TTY and carry on.
        try {
            return CLib.INSTANCE.isatty(STDOUT_FILENO) != 0;
        } catch (Throwable ignored) {
            return false;
        }
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy