Skip to content

Commit

Permalink
InputValue prompt with mask doesn't return the enter value, only the …
Browse files Browse the repository at this point in the history
…repeated mask character, fixes #1103 (#1114)

Co-authored-by: Guillaume Nodet <[email protected]>
  • Loading branch information
mattirn and gnodet authored Dec 10, 2024
1 parent 2743957 commit 8929f9b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ public InputResult execute() {
List<Candidate> matches = new ArrayList<>();
KeyMap<Operation> keyMap = new KeyMap<>();
bindKeys(keyMap);
StringBuilder displayBuffer = new StringBuilder();
StringBuilder buffer = new StringBuilder();
CompletionMatcher completionMatcher = new CompletionMatcherImpl();
boolean tabCompletion = completer != null && reader != null;
Expand All @@ -596,7 +597,7 @@ public InputResult execute() {
refreshDisplay(
firstItemRow - 1,
column,
buffer.toString(),
displayBuffer.toString(),
row,
startColumn,
displayCandidates ? matches : new ArrayList<>());
Expand All @@ -608,48 +609,67 @@ public InputResult execute() {
}
break;
case RIGHT:
if (column < startColumn + buffer.length()) {
if (column < startColumn + displayBuffer.length()) {
column++;
}
break;
case INSERT:
buffer.insert(column - startColumn, mask == null ? bindingReader.getLastBinding() : mask);
displayBuffer.insert(
column - startColumn, mask == null ? bindingReader.getLastBinding() : mask);
buffer.insert(column - startColumn, bindingReader.getLastBinding());
column++;
break;
case BACKSPACE:
if (column > startColumn) {
displayBuffer.deleteCharAt(column - startColumn - 1);
buffer.deleteCharAt(column - startColumn - 1);
column--;
}
break;
case DELETE:
if (column < startColumn + buffer.length() && column >= startColumn) {
if (column < startColumn + displayBuffer.length() && column >= startColumn) {
displayBuffer.deleteCharAt(column - startColumn);
buffer.deleteCharAt(column - startColumn);
}
break;
case BEGINNING_OF_LINE:
column = startColumn;
break;
case END_OF_LINE:
column = startColumn + buffer.length();
column = startColumn + displayBuffer.length();
break;
case SELECT_CANDIDATE:
if (tabCompletion && matches.size() < ReaderUtils.getInt(reader, LineReader.LIST_MAX, 50)) {
String selected =
selectCandidate(firstItemRow - 1, buffer.toString(), row + 1, startColumn, matches);
resetHeader();
if (selected != null) {
buffer.delete(0, buffer.length());
displayBuffer.delete(0, displayBuffer.length());
if (mask == null) {
displayBuffer.append(selected);
} else {
for (int i = 0; i < selected.length(); i++) {
displayBuffer.append(mask);
}
}
buffer.delete(0, displayBuffer.length());
buffer.append(selected);
column = startColumn + buffer.length();
column = startColumn + displayBuffer.length();
}
}
break;
case EXIT:
if (buffer.toString().isEmpty()) {
if (displayBuffer.toString().isEmpty()) {
if (mask == null) {
displayBuffer.append(defaultValue);
} else {
for (int i = 0; i < defaultValue.length(); i++) {
displayBuffer.append(mask);
}
}
buffer.append(defaultValue);
}
return new InputResult(buffer.toString());
return new InputResult(buffer.toString(), displayBuffer.toString());
case CANCEL:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Map<String, PromptResultItemIF> prompt(
}
}
}
String resp = result.getResult();
String resp = result.getDisplayResult();
if (result instanceof ConfirmResult) {
ConfirmResult cr = (ConfirmResult) result;
if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

public class InputResult implements PromptResultItemIF {
private final String input;
private final String displayInput;

public InputResult(String input) {
public InputResult(String input, String displayInput) {
this.input = input;
this.displayInput = displayInput;
}

public String getInput() {
return input;
public String getDisplayResult() {
return displayInput;
}

public String getResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@

public interface PromptResultItemIF {
String getResult();

default String getDisplayResult() {
return getResult();
}
}

0 comments on commit 8929f9b

Please sign in to comment.