Skip to content

Commit

Permalink
Bug fix: less fails to view first line(s) if status enabled + reviewed
Browse files Browse the repository at this point in the history
tab option implementation
  • Loading branch information
mattirn committed Jun 2, 2019
1 parent a4acf40 commit f1dce10
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
3 changes: 3 additions & 0 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jline.utils.Display;
import org.jline.utils.InfoCmp.Capability;
import org.jline.utils.NonBlockingReader;
import org.jline.utils.Status;

import static org.jline.keymap.KeyMap.alt;
import static org.jline.keymap.KeyMap.ctrl;
Expand Down Expand Up @@ -124,6 +125,7 @@ public void run(List<Source> sources) throws IOException, InterruptedException {
openSource();

try {
Status.getStatus(terminal).suspend();
size.copy(terminal.getSize());

if (quitIfOneScreen && sources.size() == 1) {
Expand Down Expand Up @@ -406,6 +408,7 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
}
} finally {
reader.close();
Status.getStatus(terminal).restore();
}
}

Expand Down
8 changes: 1 addition & 7 deletions terminal/src/main/java/org/jline/utils/AttributedString.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ public static AttributedString fromAnsi(String ansi) {
}

public static AttributedString fromAnsi(String ansi, int tabs) {
if (ansi == null) {
return null;
}
return new AttributedStringBuilder(ansi.length())
.tabs(tabs)
.ansiAppend(ansi)
.toAttributedString();
return fromAnsi(ansi, Arrays.asList(tabs));
}

public static AttributedString fromAnsi(String ansi, List<Integer> tabs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,10 @@ public void setLength(int l) {
* @return this
*/
public AttributedStringBuilder tabs(int tabsize) {
if (length > 0) {
throw new IllegalStateException("Cannot change tab size after appending text");
}
if (tabsize < 0) {
throw new IllegalArgumentException("Tab size must be non negative");
}
this.tabs = new TabStops(tabsize);
return this;
return tabs(Arrays.asList(tabsize));
}

public AttributedStringBuilder tabs(List<Integer> tabs) {
Expand Down Expand Up @@ -429,7 +425,7 @@ public TabStops(List<Integer> tabs) {
this.tabs = tabs;
int p = 0;
for (int s: tabs) {
if (s < p) {
if (s <= p) {
continue;
}
lastStop = s;
Expand All @@ -439,7 +435,7 @@ public TabStops(List<Integer> tabs) {
}

boolean defined() {
return lastSize != 0;
return lastSize > 0;
}

int spaces(int lastLineLength) {
Expand Down
36 changes: 35 additions & 1 deletion terminal/src/main/java/org/jline/utils/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public class Status {
protected final AbstractTerminal terminal;
protected final boolean supported;
protected List<AttributedString> oldLines = Collections.emptyList();
protected List<AttributedString> linesToRestore = Collections.emptyList();
protected int rows;
protected int columns;
protected boolean force;
protected boolean suspended = false;

public static Status getStatus(Terminal terminal) {
return getStatus(terminal, true);
Expand Down Expand Up @@ -62,20 +64,33 @@ public void reset() {
}

public void hardReset() {
if (suspended) {
return;
}
List<AttributedString> lines = new ArrayList<>(oldLines);
update(null);
update(lines);
}

public void redraw() {
if (suspended) {
return;
}
update(oldLines);
}

public void update(List<AttributedString> lines) {
if (!supported) {
return;
}
if (lines == null) {
lines = Collections.emptyList();
}
if (!supported || (oldLines.equals(lines) && !force)) {
if (suspended) {
linesToRestore = new ArrayList<>(lines);
return;
}
if (oldLines.equals(lines) && !force) {
return;
}
int nb = lines.size() - oldLines.size();
Expand All @@ -100,4 +115,23 @@ public void update(List<AttributedString> lines) {
oldLines = new ArrayList<>(lines);
force = false;
}

public void suspend() {
if (suspended) {
return;
}
linesToRestore = new ArrayList<>(oldLines);
update(null);
suspended = true;
}

public void restore() {
if (!suspended) {
return;
}
suspended = false;
update(linesToRestore);
linesToRestore = Collections.emptyList();
}

}

0 comments on commit f1dce10

Please sign in to comment.