Search in sources :

Example 1 with JLineConsoleLineInput

use of com.teradata.jaqy.lineinput.JLineConsoleLineInput in project jaqy by Teradata.

the class Main method main.

public static void main(String[] args) throws Exception {
    // disable jline trace messages on SLES systems
    jline.internal.Log.setOutput(new PrintStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        }
    }));
    Globals globals = new Globals();
    // initiate the name and version
    Package pkg = Main.class.getPackage();
    globals.setName(pkg.getImplementationTitle());
    globals.setVersion(pkg.getImplementationVersion());
    globals.getOs();
    // install Jansi
    if (Os.isWindows()) {
        AnsiConsole.systemInstall();
    }
    // initiate the greeting message.
    try {
        String greet = StringUtils.getStringFromStream(Main.class.getResourceAsStream(GREET_RC));
        globals.setGreeting(greet);
    } catch (Exception ex) {
    }
    // initiate the display
    ConsoleDisplay display = new ConsoleDisplay(globals);
    // Add shutdown hook that closes all sessions on exit.
    JaqyShutdownHook.register(globals);
    globals.getOs();
    // register signal handlers for dealing with Ctrl-C
    if (!Os.isWindows()) {
    // The signal handler only works on Linux
    // new ConsoleSignalHandler (display).register ();
    }
    // now create an initial session and set the session to it.
    Session session = globals.createSession(display);
    // create an interpreter
    JaqyInterpreter interpreter = new JaqyInterpreter(globals, display, session);
    // initiate settings
    SettingSetup.init(globals);
    // initiate commands
    CommandSetup.init(globals);
    // handle command line options
    OptionSetup.init(globals);
    // install predefined helper factories
    HelperSetup.init(globals);
    try {
        globals.loadRC(ClassLoader.getSystemClassLoader(), interpreter);
    } catch (Exception ex) {
    }
    // load initiation scripts
    Path initFile = new FilePath(getDefaultInitFile());
    CommandLine cmdLine = globals.getOptionManager().getCommandLine(args);
    if (cmdLine.hasOption("norc")) {
        initFile = null;
    } else if (cmdLine.hasOption("rcfile")) {
        String fileName = cmdLine.getOptionValue("rcfile");
        initFile = new FilePath(new File(fileName));
    }
    loadInit(globals, interpreter, display, initFile);
    // Now handle command line options
    // We want to do this after loading the initiation script to
    // allow any custom addons to be installed.
    args = globals.getOptionManager().handleOptions(globals, display, args);
    // print the start up screen
    initScreen(globals, display);
    // we are done with the loading phase
    display.setInitiated();
    // display the title
    if (display.isInteractive()) {
        display.showTitle(interpreter);
    }
    // reset the command counter so we get consistent result regardless
    // of the initiation script
    interpreter.resetCommandCount();
    Path currentDir = new FilePath(globals.getDirectory());
    // setup the input
    if (display.isInteractive()) {
        globals.getOs();
        if (Os.isWindows()) {
            // windows
            // 
            // Windows have its own readline-like support for
            // all apps, so we can just use the default system
            // behavior.
            interpreter.push(LineInputFactory.getSimpleLineInput(System.in, currentDir, true));
        } else {
            try {
                // we use JLine other systems.
                interpreter.push(new JLineConsoleLineInput(currentDir));
            } catch (IOException ex) {
                // just in case we fail with JLine,
                // fall back to default.
                interpreter.push(LineInputFactory.getSimpleLineInput(System.in, currentDir, true));
            }
        }
    } else {
        if (!skipStdin) {
            String encoding = null;
            if (System.in.available() == 0) {
                // If the input available bytes is 0, it is possible the input
                // is not available.  So we do not want to guess the input
                // encoding at all.  Instead, use the default.
                encoding = Charset.defaultCharset().displayName();
            }
            interpreter.push(LineInputFactory.getLineInput(System.in, currentDir, encoding, false));
        }
    }
    // Interpret any remaining command line arguments first
    if (args.length > 0) {
        interpreter.push(new CommandLineInput(args, currentDir));
    }
    // parse the user commands
    interpreter.interpret(display.isInteractive());
    if (!skipStdin) {
        globals.log(Level.INFO, "Errors: " + interpreter.getErrorCount() + ", Failures: " + interpreter.getFailureCount());
        System.exit(interpreter.getExitCode());
    }
}
Also used : FilePath(com.teradata.jaqy.path.FilePath) Path(com.teradata.jaqy.interfaces.Path) FilePath(com.teradata.jaqy.path.FilePath) JLineConsoleLineInput(com.teradata.jaqy.lineinput.JLineConsoleLineInput) CommandLine(org.apache.commons.cli.CommandLine) CommandLineInput(com.teradata.jaqy.lineinput.CommandLineInput)

Aggregations

Path (com.teradata.jaqy.interfaces.Path)1 CommandLineInput (com.teradata.jaqy.lineinput.CommandLineInput)1 JLineConsoleLineInput (com.teradata.jaqy.lineinput.JLineConsoleLineInput)1 FilePath (com.teradata.jaqy.path.FilePath)1 CommandLine (org.apache.commons.cli.CommandLine)1