Skip to content

Commit 8929f9b

Browse files
mattirngnodet
andauthored
InputValue prompt with mask doesn't return the enter value, only the repeated mask character, fixes #1103 (#1114)
Co-authored-by: Guillaume Nodet <[email protected]>
1 parent 2743957 commit 8929f9b

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

console-ui/src/main/java/org/jline/consoleui/prompt/AbstractPrompt.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ public InputResult execute() {
576576
List<Candidate> matches = new ArrayList<>();
577577
KeyMap<Operation> keyMap = new KeyMap<>();
578578
bindKeys(keyMap);
579+
StringBuilder displayBuffer = new StringBuilder();
579580
StringBuilder buffer = new StringBuilder();
580581
CompletionMatcher completionMatcher = new CompletionMatcherImpl();
581582
boolean tabCompletion = completer != null && reader != null;
@@ -596,7 +597,7 @@ public InputResult execute() {
596597
refreshDisplay(
597598
firstItemRow - 1,
598599
column,
599-
buffer.toString(),
600+
displayBuffer.toString(),
600601
row,
601602
startColumn,
602603
displayCandidates ? matches : new ArrayList<>());
@@ -608,48 +609,67 @@ public InputResult execute() {
608609
}
609610
break;
610611
case RIGHT:
611-
if (column < startColumn + buffer.length()) {
612+
if (column < startColumn + displayBuffer.length()) {
612613
column++;
613614
}
614615
break;
615616
case INSERT:
616-
buffer.insert(column - startColumn, mask == null ? bindingReader.getLastBinding() : mask);
617+
displayBuffer.insert(
618+
column - startColumn, mask == null ? bindingReader.getLastBinding() : mask);
619+
buffer.insert(column - startColumn, bindingReader.getLastBinding());
617620
column++;
618621
break;
619622
case BACKSPACE:
620623
if (column > startColumn) {
624+
displayBuffer.deleteCharAt(column - startColumn - 1);
621625
buffer.deleteCharAt(column - startColumn - 1);
622626
column--;
623627
}
624628
break;
625629
case DELETE:
626-
if (column < startColumn + buffer.length() && column >= startColumn) {
630+
if (column < startColumn + displayBuffer.length() && column >= startColumn) {
631+
displayBuffer.deleteCharAt(column - startColumn);
627632
buffer.deleteCharAt(column - startColumn);
628633
}
629634
break;
630635
case BEGINNING_OF_LINE:
631636
column = startColumn;
632637
break;
633638
case END_OF_LINE:
634-
column = startColumn + buffer.length();
639+
column = startColumn + displayBuffer.length();
635640
break;
636641
case SELECT_CANDIDATE:
637642
if (tabCompletion && matches.size() < ReaderUtils.getInt(reader, LineReader.LIST_MAX, 50)) {
638643
String selected =
639644
selectCandidate(firstItemRow - 1, buffer.toString(), row + 1, startColumn, matches);
640645
resetHeader();
641646
if (selected != null) {
642-
buffer.delete(0, buffer.length());
647+
displayBuffer.delete(0, displayBuffer.length());
648+
if (mask == null) {
649+
displayBuffer.append(selected);
650+
} else {
651+
for (int i = 0; i < selected.length(); i++) {
652+
displayBuffer.append(mask);
653+
}
654+
}
655+
buffer.delete(0, displayBuffer.length());
643656
buffer.append(selected);
644-
column = startColumn + buffer.length();
657+
column = startColumn + displayBuffer.length();
645658
}
646659
}
647660
break;
648661
case EXIT:
649-
if (buffer.toString().isEmpty()) {
662+
if (displayBuffer.toString().isEmpty()) {
663+
if (mask == null) {
664+
displayBuffer.append(defaultValue);
665+
} else {
666+
for (int i = 0; i < defaultValue.length(); i++) {
667+
displayBuffer.append(mask);
668+
}
669+
}
650670
buffer.append(defaultValue);
651671
}
652-
return new InputResult(buffer.toString());
672+
return new InputResult(buffer.toString(), displayBuffer.toString());
653673
case CANCEL:
654674
return null;
655675
}

console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public Map<String, PromptResultItemIF> prompt(
130130
}
131131
}
132132
}
133-
String resp = result.getResult();
133+
String resp = result.getDisplayResult();
134134
if (result instanceof ConfirmResult) {
135135
ConfirmResult cr = (ConfirmResult) result;
136136
if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) {

console-ui/src/main/java/org/jline/consoleui/prompt/InputResult.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
public class InputResult implements PromptResultItemIF {
1212
private final String input;
13+
private final String displayInput;
1314

14-
public InputResult(String input) {
15+
public InputResult(String input, String displayInput) {
1516
this.input = input;
17+
this.displayInput = displayInput;
1618
}
1719

18-
public String getInput() {
19-
return input;
20+
public String getDisplayResult() {
21+
return displayInput;
2022
}
2123

2224
public String getResult() {

console-ui/src/main/java/org/jline/consoleui/prompt/PromptResultItemIF.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010

1111
public interface PromptResultItemIF {
1212
String getResult();
13+
14+
default String getDisplayResult() {
15+
return getResult();
16+
}
1317
}

0 commit comments

Comments
 (0)