src/jdk.internal.le/share/classes/jdk/internal/jline/TerminalFactory.java
author wetmore
Thu, 21 Sep 2017 12:13:25 -0700
changeset 47238 2557ac47e731
parent 47216 71c04702a3d5
child 50338 1d5694c1aa03
permissions -rw-r--r--
8187788: Disasble javax/net tests until JDK-8187786 is resolved Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31530
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     1
/*
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2002-2012, the original author or authors.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     3
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     4
 * This software is distributable under the BSD license. See the terms of the
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     5
 * BSD license in the documentation provided with this software.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     6
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     7
 * http://www.opensource.org/licenses/bsd-license.php
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     8
 */
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
     9
package jdk.internal.jline;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    10
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    11
import java.text.MessageFormat;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    12
import java.util.HashMap;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    13
import java.util.Map;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    14
import java.util.concurrent.Callable;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    15
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    16
import jdk.internal.jline.internal.Configuration;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    17
import jdk.internal.jline.internal.Log;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    18
import jdk.internal.jline.internal.Preconditions;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    19
import static jdk.internal.jline.internal.Preconditions.checkNotNull;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    20
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    21
/**
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    22
 * Creates terminal instances.
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    23
 *
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    24
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    25
 * @since 2.0
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    26
 */
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    27
public class TerminalFactory
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    28
{
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    29
    public static final String JLINE_TERMINAL = "jline.terminal";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    30
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    31
    public static final String AUTO = "auto";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    32
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    33
    public static final String UNIX = "unix";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    34
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    35
    public static final String WIN = "win";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    36
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    37
    public static final String WINDOWS = "windows";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    38
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    39
    public static final String NONE = "none";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    40
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    41
    public static final String OFF = "off";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    42
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    43
    public static final String FALSE = "false";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    44
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    45
    private static Terminal term = null;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    46
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    47
    public static synchronized Terminal create() {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    48
        if (Log.TRACE) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    49
            //noinspection ThrowableInstanceNeverThrown
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    50
            Log.trace(new Throwable("CREATE MARKER"));
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    51
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    52
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    53
        String type = Configuration.getString(JLINE_TERMINAL, AUTO);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    54
        if ("dumb".equals(System.getenv("TERM"))) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    55
            type = "none";
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    56
            Log.debug("$TERM=dumb; setting type=", type);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    57
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    58
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    59
        Log.debug("Creating terminal; type=", type);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    60
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    61
        Terminal t;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    62
        try {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    63
            String tmp = type.toLowerCase();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    64
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    65
            if (tmp.equals(UNIX)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    66
                t = getFlavor(Flavor.UNIX);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    67
            }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    68
            else if (tmp.equals(WIN) | tmp.equals(WINDOWS)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    69
                t = getFlavor(Flavor.WINDOWS);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    70
            }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    71
            else if (tmp.equals(NONE) || tmp.equals(OFF) || tmp.equals(FALSE)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    72
                t = new UnsupportedTerminal();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    73
            }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    74
            else {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    75
                if (tmp.equals(AUTO)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    76
                    String os = Configuration.getOsName();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    77
                    Flavor flavor = Flavor.UNIX;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    78
                    if (os.contains(WINDOWS)) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    79
                        flavor = Flavor.WINDOWS;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    80
                    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    81
                    t = getFlavor(flavor);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    82
                }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    83
                else {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    84
                    try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 31530
diff changeset
    85
                        @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 31530
diff changeset
    86
                        Object o = Thread.currentThread().getContextClassLoader().loadClass(type).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 31530
diff changeset
    87
                        t = (Terminal) o;
31530
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    88
                    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    89
                    catch (Exception e) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    90
                        throw new IllegalArgumentException(MessageFormat.format("Invalid terminal type: {0}", type), e);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    91
                    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    92
                }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    93
            }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    94
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    95
        catch (Exception e) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    96
            Log.error("Failed to construct terminal; falling back to unsupported", e);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    97
            t = new UnsupportedTerminal();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    98
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
    99
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   100
        Log.debug("Created Terminal: ", t);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   101
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   102
        try {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   103
            t.init();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   104
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   105
        catch (Throwable e) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   106
            Log.error("Terminal initialization failed; falling back to unsupported", e);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   107
            return new UnsupportedTerminal();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   108
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   109
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   110
        return t;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   111
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   112
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   113
    public static synchronized void reset() {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   114
        term = null;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   115
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   116
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   117
    public static synchronized void resetIf(final Terminal t) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   118
        if(t == term) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   119
            reset();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   120
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   121
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   122
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   123
    public static enum Type
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   124
    {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   125
        AUTO,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   126
        WINDOWS,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   127
        UNIX,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   128
        NONE
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   129
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   130
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   131
    public static synchronized void configure(final String type) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   132
        checkNotNull(type);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   133
        System.setProperty(JLINE_TERMINAL, type);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   134
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   135
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   136
    public static synchronized void configure(final Type type) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   137
        checkNotNull(type);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   138
        configure(type.name().toLowerCase());
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   139
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   140
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   141
    //
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   142
    // Flavor Support
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   143
    //
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   144
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   145
    public static enum Flavor
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   146
    {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   147
        WINDOWS,
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   148
        UNIX
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   149
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   150
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   151
    private static final Map<Flavor, Callable<? extends Terminal>> FLAVORS = new HashMap<>();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   152
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   153
    static {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   154
//        registerFlavor(Flavor.WINDOWS, AnsiWindowsTerminal.class);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   155
//        registerFlavor(Flavor.UNIX, UnixTerminal.class);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   156
        registerFlavor(Flavor.WINDOWS, WindowsTerminal :: new);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   157
        registerFlavor(Flavor.UNIX, UnixTerminal :: new);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   158
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   159
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   160
    public static synchronized Terminal get() {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   161
        if (term == null) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   162
            term = create();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   163
        }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   164
        return term;
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   165
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   166
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   167
    public static Terminal getFlavor(final Flavor flavor) throws Exception {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   168
        return FLAVORS.getOrDefault(flavor, () -> {throw new InternalError();}).call();
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   169
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   170
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   171
    public static void registerFlavor(final Flavor flavor, final Callable<? extends Terminal> sup) {
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   172
        FLAVORS.put(flavor, sup);
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   173
    }
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   174
aa87d96a02b1 8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff changeset
   175
}