Skip to content

Commit

Permalink
Improve status bar drawing mechanism (fixes #939) (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Apr 22, 2024
1 parent 3865e5f commit 0028516
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 153 deletions.
42 changes: 27 additions & 15 deletions console/src/main/java/org/jline/widget/TailTipWidgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.jline.widget;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -156,7 +157,7 @@ public void setTailTips(Map<String, CmdDesc> tailTips) {

public void setDescriptionSize(int descriptionSize) {
this.descriptionSize = descriptionSize;
initDescription(descriptionSize);
initDescription();
}

public int getDescriptionSize() {
Expand Down Expand Up @@ -393,22 +394,33 @@ private void doDescription(List<AttributedString> desc) {
if (descriptionSize == 0 || !descriptionEnabled) {
return;
}
if (desc.isEmpty()) {
clearDescription();
} else if (desc.size() == descriptionSize) {
addDescription(desc);
} else if (desc.size() > descriptionSize) {
List<AttributedString> list = desc;
if (list.size() > descriptionSize) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(desc.get(descriptionSize - 1)).append("...", new AttributedStyle(AttributedStyle.INVERSE));
List<AttributedString> mod = new ArrayList<>(desc.subList(0, descriptionSize - 1));
asb.append(list.get(descriptionSize - 1)).append("", new AttributedStyle(AttributedStyle.INVERSE));
List<AttributedString> mod = new ArrayList<>(list.subList(0, descriptionSize - 1));
mod.add(asb.toAttributedString());
addDescription(mod);
} else {
while (desc.size() != descriptionSize) {
desc.add(new AttributedString(""));
list = mod;
} else if (list.size() < descriptionSize) {
List<AttributedString> mod = new ArrayList<>(list);
while (mod.size() != descriptionSize) {
mod.add(new AttributedString(""));
}
addDescription(desc);
list = mod;
}
setDescription(list);
}

/**
* Initialize terminal status bar
*/
public void initDescription() {
Status.getStatus(reader.getTerminal()).setBorder(true);
clearDescription();
}

public void clearDescription() {
doDescription(Collections.emptyList());
}

private boolean autopairEnabled() {
Expand All @@ -419,7 +431,7 @@ private boolean autopairEnabled() {
public boolean toggleWindow() {
descriptionEnabled = !descriptionEnabled;
if (descriptionEnabled) {
initDescription(descriptionSize);
initDescription();
} else {
destroyDescription();
}
Expand All @@ -435,7 +447,7 @@ public boolean toggleKeyBindings() {
} else {
customBindings();
if (descriptionEnabled) {
initDescription(descriptionSize);
initDescription();
}
readerErrors = reader.getVariable(LineReader.ERRORS);
reader.setVariable(LineReader.ERRORS, 0);
Expand Down
35 changes: 5 additions & 30 deletions console/src/main/java/org/jline/widget/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.jline.widget;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -265,49 +265,24 @@ public void setSuggestionType(SuggestionType type) {
}

/**
* Add description text to the terminal status bar
* Set description text to the terminal status bar
* @param desc description text
*/
public void addDescription(List<AttributedString> desc) {
public void setDescription(List<AttributedString> desc) {
Status.getStatus(reader.getTerminal()).update(desc);
}

/**
* Clears terminal status bar
*/
public void clearDescription() {
initDescription(0);
}

/**
* Initialize terminal status bar
* @param size Terminal status bar size in rows
*/
public void initDescription(int size) {
Status status = Status.getStatus(reader.getTerminal(), false);
if (size > 0) {
if (status == null) {
status = Status.getStatus(reader.getTerminal());
}
status.setBorder(true);
List<AttributedString> as = new ArrayList<>();
for (int i = 0; i < size; i++) {
as.add(new AttributedString(""));
}
addDescription(as);
} else if (status != null) {
if (size < 0) {
status.update(null);
} else {
status.clear();
}
}
setDescription(Collections.emptyList());
}

/**
* Remove terminal status bar
*/
public void destroyDescription() {
initDescription(-1);
Status.getExistingStatus(reader.getTerminal()).ifPresent(Status::hide);
}
}
17 changes: 8 additions & 9 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1194,17 +1194,19 @@ protected String finish(String str) {
return str;
}

protected void handleSignal(Signal signal) {
protected synchronized void handleSignal(Signal signal) {
doAutosuggestion = false;
if (signal == Signal.WINCH) {
size.copy(terminal.getBufferSize());
display.resize(size.getRows(), size.getColumns());
Status status = Status.getStatus(terminal, false);
if (status != null) {
status.hardReset();
status.resize(size);
status.reset();
}
size.copy(terminal.getBufferSize());
display.resize(size.getRows(), size.getColumns());
// restores prompt but also prevents scrolling in consoleZ, see #492
// redrawLine();
terminal.puts(Capability.carriage_return);
terminal.puts(Capability.clr_eos);
redrawLine();
redisplay();
} else if (signal == Signal.CONT) {
terminal.enterRawMode();
Expand Down Expand Up @@ -3874,9 +3876,6 @@ protected void redisplay(boolean flush) {

Status status = Status.getStatus(terminal, false);
if (status != null) {
if (terminal.getType().startsWith(AbstractWindowsTerminal.TYPE_WINDOWS)) {
status.resize();
}
status.redraw();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ public SignalHandler handle(Signal signal, SignalHandler handler) {
public void raise(Signal signal) {
Objects.requireNonNull(signal);
SignalHandler handler = handlers.get(signal);
if (handler != SignalHandler.SIG_DFL && handler != SignalHandler.SIG_IGN) {
if (handler == SignalHandler.SIG_DFL) {
if (status != null && signal == Signal.WINCH) {
status.resize();
}
} else if (handler != SignalHandler.SIG_IGN) {
handler.handle(signal);
}
if (status != null && signal == Signal.WINCH) {
status.resize();
}
}

public final void close() throws IOException {
Expand All @@ -108,8 +109,7 @@ public final void close() throws IOException {

protected void doClose() throws IOException {
if (status != null) {
status.update(null);
flush();
status.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public AttributedStringBuilder append(char c) {
return append(Character.toString(c));
}

public AttributedStringBuilder append(char c, int repeat) {
AttributedString s = new AttributedString(Character.toString(c), current);
while (repeat-- > 0) {
append(s);
}
return this;
}

public AttributedStringBuilder append(CharSequence csq, AttributedStyle style) {
return append(new AttributedString(csq, style));
}
Expand Down
4 changes: 2 additions & 2 deletions terminal/src/main/java/org/jline/utils/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Display {
protected final boolean fullScreen;
protected List<AttributedString> oldLines = Collections.emptyList();
protected int cursorPos;
private int columns;
private int columns1; // columns+1
protected int columns;
protected int columns1; // columns+1
protected int rows;
protected boolean reset;
protected boolean delayLineWrap;
Expand Down
Loading

0 comments on commit 0028516

Please sign in to comment.