Skip to content

Commit

Permalink
fix NPE: redisplay() before readLine()
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 29, 2019
1 parent d862e3e commit 4683d8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
6 changes: 4 additions & 2 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ public static void main(String[] args) throws IOException {
int counter = 0;
while (true) {
try {
Thread.sleep(1000);
Status status = Status.getStatus(reader.getTerminal());
counter++;
status.update(Arrays.asList(new AttributedStringBuilder().append("counter: " + counter).toAttributedString()));
((LineReaderImpl) reader).redisplay();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -297,7 +297,9 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
}
}
callbacks.forEach(c -> c.accept(reader));

if (!callbacks.isEmpty()) {
Thread.sleep(2000);
}
while (true) {
String line = null;
try {
Expand Down
33 changes: 19 additions & 14 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ protected enum BellType {

protected final Size size = new Size();

protected AttributedString prompt;
protected AttributedString rightPrompt;
protected AttributedString prompt = AttributedString.EMPTY;
protected AttributedString rightPrompt = AttributedString.EMPTY;

protected MaskingCallback maskingCallback;

Expand Down Expand Up @@ -279,6 +279,7 @@ public LineReaderImpl(Terminal terminal, String appName, Map<String, Object> var
builtinWidgets = builtinWidgets();
widgets = new HashMap<>(builtinWidgets);
bindingReader = new BindingReader(terminal.reader());
doDisplay();
}

public Terminal getTerminal() {
Expand Down Expand Up @@ -537,18 +538,7 @@ public String readLine(String prompt, String rightPrompt, MaskingCallback maskin
previousContHandler = terminal.handle(Signal.CONT, this::handleSignal);
originalAttributes = terminal.enterRawMode();

// Cache terminal size for the duration of the call to readLine()
// It will eventually be updated with WINCH signals
size.copy(terminal.getBufferSize());

display = new Display(terminal, false);
if (size.getRows() == 0 || size.getColumns() == 0) {
display.resize(1, Integer.MAX_VALUE);
} else {
display.resize(size.getRows(), size.getColumns());
}
if (isSet(Option.DELAY_LINE_WRAP))
display.setDelayLineWrap(true);
doDisplay();

// Move into application mode
if (!dumb) {
Expand Down Expand Up @@ -676,6 +666,21 @@ public String readLine(String prompt, String rightPrompt, MaskingCallback maskin
}
}

private void doDisplay(){
// Cache terminal size for the duration of the call to readLine()
// It will eventually be updated with WINCH signals
size.copy(terminal.getBufferSize());

display = new Display(terminal, false);
if (size.getRows() == 0 || size.getColumns() == 0) {
display.resize(1, Integer.MAX_VALUE);
} else {
display.resize(size.getRows(), size.getColumns());
}
if (isSet(Option.DELAY_LINE_WRAP))
display.setDelayLineWrap(true);
}

@Override
public void printAbove(String str) {
try {
Expand Down

0 comments on commit 4683d8e

Please sign in to comment.