Skip to content

Commit

Permalink
Console UI ListChoice's (relative)pageSize is never used, fixes jline…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jul 18, 2024
1 parent c7c31f7 commit 87cffa1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jline.consoleui.elements.Checkbox;
import org.jline.consoleui.elements.ConfirmChoice;
import org.jline.consoleui.elements.ExpandableChoice;
import org.jline.consoleui.elements.InputValue;
Expand Down Expand Up @@ -51,19 +50,20 @@ public abstract class AbstractPrompt<T extends ConsoleUIItemIF> {

public AbstractPrompt(
Terminal terminal, List<AttributedString> header, AttributedString message, ConsolePrompt.UiConfig cfg) {
this(terminal, header, message, new ArrayList<>(), cfg);
this(terminal, header, message, new ArrayList<>(), 0, cfg);
}

public AbstractPrompt(
Terminal terminal,
List<AttributedString> header,
AttributedString message,
List<T> items,
int pageSize,
ConsolePrompt.UiConfig cfg) {
this.terminal = terminal;
this.bindingReader = new BindingReader(terminal.reader());
this.size.copy(terminal.getSize());
int listSpace = Math.min(size.getRows(), 10);
int listSpace = Math.min(size.getRows(), Math.max(pageSize, 3));
this.header = header.size() > size.getRows() - listSpace
? header.subList(header.size() - size.getRows() + listSpace, header.size())
: header;
Expand Down Expand Up @@ -143,10 +143,7 @@ private int candidateStartPosition(int candidatesColumn, String buffer, List<Can
private List<AttributedString> displayLines(
int cursorRow, int candidatesColumn, AttributedString buffer, List<Candidate> candidates) {
computeListRange(cursorRow, candidates.size());
List<AttributedString> out = new ArrayList<>();
for (int i = range.headerStart; i < header.size(); i++) {
out.add(header.get(i));
}
List<AttributedString> out = new ArrayList<>(header);
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(message);
asb.append(buffer);
Expand Down Expand Up @@ -192,16 +189,12 @@ private List<AttributedString> displayLines(
asb2.append(asb).append(" ");
out.add(asb2.toAttributedString());
}
addFooter(out, candidates.size());
return out;
}

private List<AttributedString> displayLines(int cursorRow, Set<String> selected) {
computeListRange(cursorRow, items.size());
List<AttributedString> out = new ArrayList<>();
for (int i = range.headerStart; i < header.size(); i++) {
out.add(header.get(i));
}
List<AttributedString> out = new ArrayList<>(header);
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(message);
out.add(asb.toAttributedString());
Expand Down Expand Up @@ -233,9 +226,12 @@ private List<AttributedString> displayLines(int cursorRow, Set<String> selected)
if (s.isDisabled()) {
asb.append(" (").append(s.getDisabledText()).append(")");
}
int textLength = asb.length();
for (int j = 0; j < size.getColumns() - textLength; j++) {
asb.append(' ');
}
out.add(asb.toAttributedString());
}
addFooter(out, items.size()); // footer is necessary for making the long item list scroll correctly
return out;
}

Expand All @@ -255,10 +251,8 @@ private void fillCheckboxSpace(AttributedStringBuilder asb) {
private static class ListRange {
final int first;
final int last;
final int headerStart;

public ListRange(int headerStart, int first, int last) {
this.headerStart = headerStart;
public ListRange(int first, int last) {
this.first = first;
this.last = last;
}
Expand All @@ -268,33 +262,21 @@ private void computeListRange(int cursorRow, int itemsSize) {
if (range != null && range.first <= cursorRow - firstItemRow && range.last - 1 > cursorRow - firstItemRow) {
return;
}
range = new ListRange(0, 0, itemsSize + 1);
range = new ListRange(0, itemsSize + 1);
if (size.getRows() < header.size() + itemsSize + 1) {
int itemId = cursorRow - firstItemRow;
int headerStart = header.size() + 1 > 10 ? header.size() - 9 : 0;
firstItemRow = firstItemRow - headerStart;
int forList = size.getRows() - header.size() + headerStart - 1;
int forList = size.getRows() - header.size();
if (itemId < forList - 1) {
range = new ListRange(headerStart, 0, forList);
range = new ListRange(0, forList);
} else {
range = new ListRange(headerStart, itemId - forList + 2, itemId + 2);
range = new ListRange(itemId - forList + 2, itemId + 2);
}
}
}

private void addFooter(List<AttributedString> lines, int itemsSize) {
if (size.getRows() < header.size() + itemsSize + 1) {
AttributedStringBuilder asb = new AttributedStringBuilder();
lines.add(asb.append(".").toAttributedString());
}
}

private List<AttributedString> displayLines(int cursorRow, AttributedString buffer, boolean newline) {
computeListRange(cursorRow, items.size());
List<AttributedString> out = new ArrayList<>();
for (int i = range.headerStart; i < header.size(); i++) {
out.add(header.get(i));
}
List<AttributedString> out = new ArrayList<>(header);
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(message);
if (buffer != null && !newline) {
Expand Down Expand Up @@ -324,7 +306,6 @@ private List<AttributedString> displayLines(int cursorRow, AttributedString buff
out.add(asb.append(s.getText()).toAttributedString());
}
}
addFooter(out, items.size());
return out;
}

Expand Down Expand Up @@ -785,8 +766,9 @@ private ListChoicePrompt(
List<AttributedString> header,
AttributedString message,
List<T> listItems,
int pageSize,
ConsolePrompt.UiConfig cfg) {
super(terminal, header, message, listItems, cfg);
super(terminal, header, message, listItems, pageSize, cfg);
items = listItems;
}

Expand All @@ -795,8 +777,9 @@ public static <T extends ListItemIF> ListChoicePrompt<T> getPrompt(
List<AttributedString> header,
AttributedString message,
List<T> listItems,
int pageSize,
ConsolePrompt.UiConfig cfg) {
return new ListChoicePrompt<>(terminal, header, message, listItems, cfg);
return new ListChoicePrompt<>(terminal, header, message, listItems, pageSize, cfg);
}

private void bindKeys(KeyMap<Operation> map) {
Expand Down Expand Up @@ -859,19 +842,21 @@ private CheckboxPrompt(
Terminal terminal,
List<AttributedString> header,
AttributedString message,
Checkbox checkbox,
List<CheckboxItemIF> checkboxItemList,
int pageSize,
ConsolePrompt.UiConfig cfg) {
super(terminal, header, message, checkbox.getCheckboxItemList(), cfg);
items = checkbox.getCheckboxItemList();
super(terminal, header, message, checkboxItemList, pageSize, cfg);
items = checkboxItemList;
}

public static CheckboxPrompt getPrompt(
Terminal terminal,
List<AttributedString> header,
AttributedString message,
Checkbox checkbox,
List<CheckboxItemIF> checkboxItemList,
int pageSize,
ConsolePrompt.UiConfig cfg) {
return new CheckboxPrompt(terminal, header, message, checkbox, cfg);
return new CheckboxPrompt(terminal, header, message, checkboxItemList, pageSize, cfg);
}

private void bindKeys(KeyMap<Operation> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ public Map<String, PromptResultItemIF> prompt(
if (pe instanceof ListChoice) {
ListChoice lc = (ListChoice) pe;
result = ListChoicePrompt.getPrompt(
terminal, header, asb.toAttributedString(), lc.getListItemList(), config)
terminal,
header,
asb.toAttributedString(),
lc.getListItemList(),
computePageSize(terminal, lc.getPageSize(), lc.getPageSizeType()),
config)
.execute();
} else if (pe instanceof InputValue) {
InputValue ip = (InputValue) pe;
Expand All @@ -137,12 +142,18 @@ public Map<String, PromptResultItemIF> prompt(
.execute();
} catch (ExpandableChoiceException e) {
result = ListChoicePrompt.getPrompt(
terminal, header, message.toAttributedString(), ec.getChoiceItems(), config)
terminal, header, message.toAttributedString(), ec.getChoiceItems(), 10, config)
.execute();
}
} else if (pe instanceof Checkbox) {
Checkbox cb = (Checkbox) pe;
result = CheckboxPrompt.getPrompt(terminal, header, message.toAttributedString(), cb, config)
result = CheckboxPrompt.getPrompt(
terminal,
header,
message.toAttributedString(),
cb.getCheckboxItemList(),
computePageSize(terminal, cb.getPageSize(), cb.getPageSizeType()),
config)
.execute();
} else if (pe instanceof ConfirmChoice) {
ConfirmChoice cc = (ConfirmChoice) pe;
Expand Down Expand Up @@ -185,6 +196,11 @@ public Map<String, PromptResultItemIF> prompt(
}
}

private int computePageSize(Terminal terminal, int pageSize, PageSizeType sizeType) {
int rows = terminal.getHeight();
return sizeType == PageSizeType.ABSOLUTE ? Math.min(rows, pageSize) : (rows * pageSize) / 100;
}

/**
* Creates a {@link PromptBuilder}.
*
Expand Down

0 comments on commit 87cffa1

Please sign in to comment.