Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #1151 - Add line numbers and current line marker to secondary prompt #1152

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
27 changes: 27 additions & 0 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ protected enum BellType {

protected String alternateIn;
protected String alternateOut;
protected List<AttributedString> currentLines;

public LineReaderImpl(Terminal terminal) throws IOException {
this(terminal, terminal.getName(), null);
Expand Down Expand Up @@ -4176,6 +4177,16 @@ private AttributedString expandPromptPattern(String pattern, int padToWidth, Str
case 'N':
sb.append(getInt(LINE_OFFSET, 0) + line);
break decode;
case 'C':
Copy link
Member

Choose a reason for hiding this comment

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

Why this new C option ? it seems to me that setting LINE_OFFSET to 1 would achieve the same ?

Choose a reason for hiding this comment

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

The C option used to handle this pattern %5P %C%* for numbering the new secondary prompt lines

sb.append(getInt(LINE_OFFSET, 0) + (line + 1));
break decode;
case '*':
if (getCurrentLineNo(this.currentLines) == line) {
Copy link
Member

Choose a reason for hiding this comment

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

The currentLine should be computed once and passed as an argument to this method.

Choose a reason for hiding this comment

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

I don't think we can do this because, in a secondary prompt, the number of lines may change with each new user input. Therefore, I need to ensure that I always get the updated number of lines.

sb.append("*");
} else {
sb.append(" ");
}
break decode;
case 'M':
if (message != null) sb.append(message);
break decode;
Expand Down Expand Up @@ -4231,6 +4242,21 @@ private AttributedString expandPromptPattern(String pattern, int padToWidth, Str
return AttributedString.join(null, parts);
}

private int getCurrentLineNo(List<AttributedString> lines) {
int currentLine = -1;
int cursor = buf.cursor();
int start = 0;
for (int l = 0; l < lines.size(); l++) {
int end = start + lines.get(l).length();
if (cursor >= start && cursor <= end) {
currentLine = l;
break;
}
start = end + 1;
}
return currentLine;
}

private AttributedString fromAnsi(String str) {
return AttributedString.fromAnsi(str, Collections.singletonList(0), alternateIn, alternateOut);
}
Expand Down Expand Up @@ -4277,6 +4303,7 @@ private AttributedString insertSecondaryPrompts(
buf.setLength(0);
}
int line = 0;
this.currentLines = lines;
Copy link
Member

Choose a reason for hiding this comment

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

if we compute the currentLine easier, maybe we can store only that information instead of the list of lines ?

Choose a reason for hiding this comment

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

So, I'll compute directly the currentLine.
Thanks!

while (line < lines.size() - 1) {
sb.append(lines.get(line)).append("\n");
buf.append(lines.get(line)).append("\n");
Expand Down