001/*
002 * Copyright (C) 2009-2017 the original author(s).
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fusesource.jansi;
017
018import static org.fusesource.jansi.Ansi.ansi;
019
020import java.io.Closeable;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.InputStreamReader;
026import java.io.PrintStream;
027import java.io.Reader;
028import java.util.Properties;
029
030import org.fusesource.hawtjni.runtime.Library;
031import org.fusesource.jansi.internal.CLibrary;
032import static org.fusesource.jansi.internal.CLibrary.isatty;
033
034/**
035 * Main class for the library, providing executable jar to diagnose Jansi setup.
036 * <p>If no system property is set and output is sent to a terminal (no redirect to a file):
037 * <ul>
038 * <li>any terminal on any Unix should get <code>RESET_ANSI_AT_CLOSE</code> mode,</li>
039 * <li>on Windows, Git-bash or Cygwin terminals should get <code>RESET_ANSI_AT_CLOSE</code> mode also, since they
040 * support natively ANSI escape sequences like any Unix terminal,</li>
041 * <li>on Windows, cmd.exe, PowerShell or Git-cmd terminals should get <code>WINDOWS</code> mode.</li>
042 * </ul>
043 * The results will vary if you play with <code>jansi.passthrough</code>, <code>jansi.strip</code> or
044 * <code>jansi.force</code> system property, or if you redirect output to a file.
045 * <p>If you have a specific situation that is not covered, please report precise conditions to reproduce
046 * the issue and ideas on how to detect precisely the affected situation.
047 */
048public class AnsiMain {
049    public static void main(String... args) throws IOException {
050        System.out.println("Jansi " + getJansiVersion()
051            + " (Jansi native " + getPomPropertiesVersion("org.fusesource.jansi/jansi-native")
052            + ", HawtJNI runtime " + getPomPropertiesVersion("org.fusesource.hawtjni/hawtjni-runtime") + ")");
053
054        System.out.println();
055
056        // info on native library
057        System.out.println("library.jansi.path= " + System.getProperty("library.jansi.path", ""));
058        System.out.println("library.jansi.version= " + System.getProperty("library.jansi.version", ""));
059        Library lib = new Library("jansi", CLibrary.class);
060        lib.load();
061        System.out.println("Jansi native library loaded from " + lib.getNativeLibraryPath());
062        if (lib.getNativeLibrarySourceUrl() != null) {
063            System.out.println("   which was auto-extracted from " + lib.getNativeLibrarySourceUrl());
064        }
065
066        System.out.println();
067
068        System.out.println("os.name= " + System.getProperty("os.name") + ", "
069                        + "os.version= " + System.getProperty("os.version") + ", "
070                        + "os.arch= " + System.getProperty("os.arch"));
071        System.out.println("file.encoding= " + System.getProperty("file.encoding"));
072        System.out.println("java.version= " + System.getProperty("java.version") + ", "
073                        + "java.vendor= " + System.getProperty("java.vendor") + ","
074                        + " java.home= " + System.getProperty("java.home"));
075
076        System.out.println();
077
078        System.out.println("jansi.passthrough= " + Boolean.getBoolean("jansi.passthrough"));
079        System.out.println("jansi.strip= " + Boolean.getBoolean("jansi.strip"));
080        System.out.println("jansi.force= " + Boolean.getBoolean("jansi.force"));
081        System.out.println(Ansi.DISABLE + "= " + Boolean.getBoolean(Ansi.DISABLE));
082
083        System.out.println();
084
085        System.out.println("IS_WINDOWS: " + AnsiConsole.IS_WINDOWS);
086        if (AnsiConsole.IS_WINDOWS) {
087            System.out.println("IS_CYGWIN: " + AnsiConsole.IS_CYGWIN);
088            System.out.println("IS_MINGW_XTERM: " + AnsiConsole.IS_MINGW_XTERM);
089        }
090
091        System.out.println();
092
093        diagnoseTty(false); // System.out
094        diagnoseTty(true);  // System.err
095
096        AnsiConsole.systemInstall();
097
098        System.out.println();
099
100        System.out.println("Resulting Jansi modes for stout/stderr streams:");
101        System.out.println("  - System.out: " + AnsiConsole.JANSI_STDOUT_TYPE);
102        System.out.println("  - System.err: " + AnsiConsole.JANSI_STDERR_TYPE);
103        System.out.println("modes description:");
104        int n = 1;
105        for(AnsiConsole.JansiOutputType type: AnsiConsole.JansiOutputType.values()) {
106            System.out.println(n++ + ". " + type + ": " + type.getDescription());
107        }
108
109        try {
110            System.out.println();
111
112            testAnsi(false);
113            testAnsi(true);
114
115            if (args.length == 0) {
116                printJansiLogoDemo();
117                return;
118            }
119
120            System.out.println();
121
122            if (args.length == 1) {
123                File f = new File(args[0]);
124                if (f.exists())
125                {
126                    // write file content
127                    System.out.println(ansi().bold().a("\"" + args[0] + "\" content:").reset());
128                    writeFileContent(f);
129                    return;
130                }
131            }
132
133            // write args without Jansi then with Jansi AnsiConsole
134            System.out.println(ansi().bold().a("original args:").reset());
135            int i = 1;
136            for (String arg: args) {
137                AnsiConsole.system_out.print(i++ + ": ");
138                AnsiConsole.system_out.println(arg);
139            }
140
141            System.out.println(ansi().bold().a("Jansi filtered args:").reset());
142            i = 1;
143            for (String arg: args) {
144                System.out.print(i++ + ": ");
145                System.out.println(arg);
146            }
147        } finally {
148            AnsiConsole.systemUninstall();
149        }
150    }
151
152    private static String getJansiVersion() {
153        Package p = AnsiMain.class.getPackage();
154        return ( p == null ) ? null : p.getImplementationVersion();
155    }
156
157    private static void diagnoseTty(boolean stderr) {
158        int fd = stderr ? CLibrary.STDERR_FILENO : CLibrary.STDOUT_FILENO;
159        int isatty = isatty(fd);
160
161        System.out.println("isatty(STD" + (stderr ? "ERR" : "OUT") + "_FILENO): " + isatty + ", System."
162            + (stderr ? "err" : "out") + " " + ((isatty == 0) ? "is *NOT*" : "is") + " a terminal");
163    }
164
165    private static void testAnsi(boolean stderr) {
166        @SuppressWarnings( "resource" )
167        PrintStream s = stderr ? System.err : System.out;
168        s.print("test on System." + (stderr ? "err" : "out") + ":");
169        for(Ansi.Color c: Ansi.Color.values()) {
170            s.print(" " + ansi().fg(c) + c + ansi().reset());
171        }
172        s.println();
173        s.print("            bright:");
174        for(Ansi.Color c: Ansi.Color.values()) {
175            s.print(" " + ansi().fgBright(c) + c + ansi().reset());
176        }
177        s.println();
178        s.print("              bold:");
179        for(Ansi.Color c: Ansi.Color.values()) {
180            s.print(" " + ansi().bold().fg(c) + c + ansi().reset());
181        }
182        s.println();
183    }
184
185    private static String getPomPropertiesVersion(String path) throws IOException {
186        InputStream in = AnsiMain.class.getResourceAsStream("/META-INF/maven/" + path + "/pom.properties");
187        if (in == null) {
188            return null;
189        }
190        try {
191            Properties p = new Properties();
192            p.load(in);
193            return p.getProperty("version");
194        } finally {
195            closeQuietly(in);
196        }
197    }
198
199    private static void printJansiLogoDemo() throws IOException {
200        Reader in = new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), "UTF-8");
201        try {
202            char[] buf = new char[1024];
203            int l = 0;
204            while ((l = in.read(buf)) >= 0) {
205                for(int i = 0; i < l; i++) {
206                    System.out.print(buf[i]);
207                }
208            }
209        } finally {
210            closeQuietly(in);
211        }
212    }
213
214    private static void writeFileContent(File f) throws IOException {
215        InputStream in = new FileInputStream(f);
216        try {
217            byte[] buf = new byte[1024];
218            int l = 0;
219            while ((l = in.read(buf)) >= 0) {
220                System.out.write(buf, 0, l);
221            }
222        } finally {
223            closeQuietly(in);
224        }
225    }
226
227    private static void closeQuietly(Closeable c) {
228        try {
229            c.close();
230        } catch (IOException ioe) {
231            ioe.printStackTrace(System.err);
232        }
233    }
234}