Skip to content

Commit

Permalink
Fix right prompt support
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 3, 2016
1 parent 25d3a3d commit 8e92151
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3444,11 +3444,15 @@ private AttributedString addRightPrompt(AttributedString prompt, AttributedStrin
int nb = size.getColumns() - width - line.columnLength() - 3;
if (nb >= 0) {
AttributedStringBuilder sb = new AttributedStringBuilder(size.getColumns());
sb.append(line);
boolean endsWithNl = line.charAt(line.length() - 1) == '\n';
sb.append(line, 0, endsWithNl ? line.length() - 1 : line.length());
for (int j = 0; j < nb + 2; j++) {
sb.append(' ');
}
sb.append(prompt);
if (endsWithNl) {
sb.append('\n');
}
line = sb.toAttributedString();
}
return line;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/jline/utils/AttributedStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ public AttributedStyle style() {
}

public AttributedStringBuilder append(AttributedString str) {
int l = str.length();
ensureCapacity(length + l);
for (int i = 0; i < l; i++) {
return append(str, 0, str.length());
}

public AttributedStringBuilder append(AttributedString str, int start, int end) {
ensureCapacity(length + end - start);
for (int i = start; i < end; i++) {
char c = str.charAt(i);
int s = str.styleCodeAt(i) & ~current.getMask() | current.getStyle();
if (tabs > 0 && c == '\t') {
Expand Down

1 comment on commit 8e92151

@PerBothner
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable. However, rather than:

boolean endsWithNl = line.charAt(line.length() - 1) == '\n';

this seems safer:

boolean endsWithNl = line.length() > 0 && line.charAt(line.length() - 1) == '\n';

I believe the last line may be empty and it is not newline-terminated.

Please sign in to comment.