if (command.length() > 0) {
// Shell is directly executing a sub/command, we don't setup a terminal and console
// in this case, this avoids us reading from stdin un-necessarily.
CommandSession session = commandProcessor.createSession(in, out, err);
session.put("USER", user);
session.put("APPLICATION", application);
session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
try {
session.execute(command);
} catch (Throwable t) {
if (t instanceof CommandNotFoundException) {
String str = Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Command not found: ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(((CommandNotFoundException) t).getCommand())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.fg(Ansi.Color.DEFAULT).toString();
session.getConsole().println(str);
} else if (t instanceof CommandException) {
session.getConsole().println(((CommandException) t).getNiceHelp());
} else {
session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString());
t.printStackTrace(session.getConsole());
session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString());
}
}
} else {
// We are going into full blown interactive shell mode.
final TerminalFactory terminalFactory = new TerminalFactory();
final Terminal terminal = terminalFactory.getTerminal();
ConsoleImpl console = createConsole(commandProcessor, threadIO, in, out, err, terminal);
CommandSession session = console.getSession();
session.put("USER", user);
session.put("APPLICATION", application);
session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
session.put("#LINES", new Function() {
public Object execute(CommandSession session, List<Object> arguments) throws Exception {
return Integer.toString(terminal.getHeight());
}
});
session.put("#COLUMNS", new Function() {
public Object execute(CommandSession session, List<Object> arguments) throws Exception {
return Integer.toString(terminal.getWidth());
}
});
session.put(".jline.terminal", terminal);
console.run();
terminalFactory.destroy();
}