Skip to content

Commit

Permalink
normalize formatting using default idea settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jdillon committed May 24, 2016
1 parent adeb16e commit 519410f
Show file tree
Hide file tree
Showing 15 changed files with 1,427 additions and 1,433 deletions.
735 changes: 369 additions & 366 deletions jansi/src/main/java/org/fusesource/jansi/Ansi.java

Large diffs are not rendered by default.

203 changes: 102 additions & 101 deletions jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,136 +28,137 @@

/**
* Provides consistent access to an ANSI aware console PrintStream.
*
*
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
* @since 1.0
*/
public class AnsiConsole {

public static final PrintStream system_out = System.out;
public static final PrintStream system_out = System.out;
public static final PrintStream out = new PrintStream(wrapOutputStream(system_out));

public static final PrintStream system_err = System.err;
public static final PrintStream err = new PrintStream(wrapOutputStream(system_err, STDERR_FILENO));

private static int installed;

private AnsiConsole() {}
private AnsiConsole() {
}

public static OutputStream wrapOutputStream(final OutputStream stream) {
return wrapOutputStream(stream, STDOUT_FILENO);
}
public static OutputStream wrapOutputStream(final OutputStream stream) {
return wrapOutputStream(stream, STDOUT_FILENO);
}

public static OutputStream wrapOutputStream(final OutputStream stream, int fileno) {
public static OutputStream wrapOutputStream(final OutputStream stream, int fileno) {

// If the jansi.passthrough property is set, then don't interpret
// any of the ansi sequences.
if( Boolean.getBoolean("jansi.passthrough") ) {
return stream;
if (Boolean.getBoolean("jansi.passthrough")) {
return stream;
}

// If the jansi.strip property is set, then we just strip the
// the ansi escapes.
if( Boolean.getBoolean("jansi.strip") ) {
return new AnsiOutputStream(stream);
if (Boolean.getBoolean("jansi.strip")) {
return new AnsiOutputStream(stream);
}

String os = System.getProperty("os.name");
if (os.startsWith("Windows") && !isCygwin()) {

// On windows we know the console does not interpret ANSI codes..
try {
return new WindowsAnsiOutputStream(stream);
} catch (Throwable ignore) {
// this happens when JNA is not in the path.. or
// this happens when the stdout is being redirected to a file.
}

// Use the ANSIOutputStream to strip out the ANSI escape sequences.
return new AnsiOutputStream(stream);
}

String os = System.getProperty("os.name");
if( os.startsWith("Windows") && !isCygwin() ) {

// On windows we know the console does not interpret ANSI codes..
try {
return new WindowsAnsiOutputStream(stream);
} catch (Throwable ignore) {
// this happens when JNA is not in the path.. or
// this happens when the stdout is being redirected to a file.
}

// Use the ANSIOutputStream to strip out the ANSI escape sequences.
return new AnsiOutputStream(stream);
}

// We must be on some Unix variant, including Cygwin or MSYS(2) on Windows...
try {
// If the jansi.force property is set, then we force to output
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
boolean forceColored = Boolean.getBoolean("jansi.force");
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
int rc = isatty(fileno);
if( !isCygwin() && !forceColored && rc == 0 ) {
return new AnsiOutputStream(stream);
}

// These erros happen if the JNI lib is not available for your platform.
// We must be on some Unix variant, including Cygwin or MSYS(2) on Windows...
try {
// If the jansi.force property is set, then we force to output
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
boolean forceColored = Boolean.getBoolean("jansi.force");
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
int rc = isatty(fileno);
if (!isCygwin() && !forceColored && rc == 0) {
return new AnsiOutputStream(stream);
}

// These erros happen if the JNI lib is not available for your platform.
} catch (NoClassDefFoundError ignore) {
} catch (UnsatisfiedLinkError ignore) {
}

// By default we assume your Unix tty can handle ANSI codes.
// Just wrap it up so that when we get closed, we reset the
// attributes.
return new FilterOutputStream(stream) {
@Override
public void close() throws IOException {
write(AnsiOutputStream.REST_CODE);
flush();
super.close();
}
};
}

private static boolean isCygwin() {
String term = System.getenv("TERM");
return term != null && term.equals("xterm");
}

/**
* If the standard out natively supports ANSI escape codes, then this just
* returns System.out, otherwise it will provide an ANSI aware PrintStream
* which strips out the ANSI escape sequences or which implement the escape
* sequences.
*
* @return a PrintStream which is ANSI aware.
*/
public static PrintStream out() {
return out;
}
} catch (UnsatisfiedLinkError ignore) {
}

// By default we assume your Unix tty can handle ANSI codes.
// Just wrap it up so that when we get closed, we reset the
// attributes.
return new FilterOutputStream(stream) {
@Override
public void close() throws IOException {
write(AnsiOutputStream.REST_CODE);
flush();
super.close();
}
};
}

private static boolean isCygwin() {
String term = System.getenv("TERM");
return term != null && term.equals("xterm");
}

/**
* If the standard out natively supports ANSI escape codes, then this just
* returns System.err, otherwise it will provide an ANSI aware PrintStream
* which strips out the ANSI escape sequences or which implement the escape
* sequences.
*
* @return a PrintStream which is ANSI aware.
*/
* If the standard out natively supports ANSI escape codes, then this just
* returns System.out, otherwise it will provide an ANSI aware PrintStream
* which strips out the ANSI escape sequences or which implement the escape
* sequences.
*
* @return a PrintStream which is ANSI aware.
*/
public static PrintStream out() {
return out;
}

/**
* If the standard out natively supports ANSI escape codes, then this just
* returns System.err, otherwise it will provide an ANSI aware PrintStream
* which strips out the ANSI escape sequences or which implement the escape
* sequences.
*
* @return a PrintStream which is ANSI aware.
*/
public static PrintStream err() {
return err;
}
/**
* Install Console.out to System.out.
*/
synchronized static public void systemInstall() {
installed++;
if( installed==1 ) {
System.setOut(out);

/**
* Install Console.out to System.out.
*/
synchronized static public void systemInstall() {
installed++;
if (installed == 1) {
System.setOut(out);
System.setErr(err);
}
}
/**
* undo a previous {@link #systemInstall()}. If {@link #systemInstall()} was called
* multiple times, it {@link #systemUninstall()} must call the same number of times before
* it is actually uninstalled.
*/
synchronized public static void systemUninstall() {
installed--;
if( installed==0 ) {
System.setOut(system_out);
}
}

/**
* undo a previous {@link #systemInstall()}. If {@link #systemInstall()} was called
* multiple times, it {@link #systemUninstall()} must call the same number of times before
* it is actually uninstalled.
*/
synchronized public static void systemUninstall() {
installed--;
if (installed == 0) {
System.setOut(system_out);
System.setErr(system_err);
}
}
}
}

}
Loading

0 comments on commit 519410f

Please sign in to comment.