Skip to content

Commit

Permalink
Interpreter: cache getBshPrompt and ensure streams not null.
Browse files Browse the repository at this point in the history
Cache the default prompt string retrieved via eval("getBshPrompt();"); for interactive mode, to avoid having to evaluate the string on every command.
Console ensures out and err streams are never null by returning default streams from System if not set.
  • Loading branch information
nickl- committed Aug 9, 2018
1 parent 78eee27 commit 014e83b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/bsh/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public static void setShutdownOnExit(final boolean value) {
*/
private boolean compatibility = COMPATIBIILTY;

/** Cached getBshPrompt for interactive mode. */
private String prompt = null;

/* --- End instance data --- */

/**
Expand Down Expand Up @@ -1168,13 +1171,15 @@ global namespace. This may be from the getBshPrompt() command or may
be defined by the user as with any other method.
Defaults to "bsh % " if the method is not defined or there is an error.
*/
private String getBshPrompt()
{
private String getBshPrompt() {
if ( null != prompt )
return prompt;
try {
return (String)eval("getBshPrompt()");
prompt = (String) eval("getBshPrompt()");
} catch ( Exception e ) {
return "bsh % ";
prompt = "bsh % ";
}
return prompt;
}

/**
Expand Down Expand Up @@ -1273,11 +1278,15 @@ public Reader getIn() {

@Override
public PrintStream getOut() {
if ( null == out )
out = System.out;
return out;
}

@Override
public PrintStream getErr() {
if ( null == err )
err = System.err;
return err;
}

Expand Down

0 comments on commit 014e83b

Please sign in to comment.