diff --git a/console-ui/src/main/java/org/jline/consoleui/prompt/AbstractPrompt.java b/console-ui/src/main/java/org/jline/consoleui/prompt/AbstractPrompt.java index 21eb2131..da9e4853 100644 --- a/console-ui/src/main/java/org/jline/consoleui/prompt/AbstractPrompt.java +++ b/console-ui/src/main/java/org/jline/consoleui/prompt/AbstractPrompt.java @@ -576,6 +576,7 @@ public InputResult execute() { List matches = new ArrayList<>(); KeyMap keyMap = new KeyMap<>(); bindKeys(keyMap); + StringBuilder displayBuffer = new StringBuilder(); StringBuilder buffer = new StringBuilder(); CompletionMatcher completionMatcher = new CompletionMatcherImpl(); boolean tabCompletion = completer != null && reader != null; @@ -596,7 +597,7 @@ public InputResult execute() { refreshDisplay( firstItemRow - 1, column, - buffer.toString(), + displayBuffer.toString(), row, startColumn, displayCandidates ? matches : new ArrayList<>()); @@ -608,22 +609,26 @@ 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; @@ -631,7 +636,7 @@ public InputResult execute() { 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)) { @@ -639,17 +644,32 @@ public InputResult execute() { 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; } diff --git a/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java b/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java index cb7894e0..83b3ebfb 100644 --- a/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java +++ b/console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java @@ -130,7 +130,7 @@ public Map prompt( } } } - String resp = result.getResult(); + String resp = result.getDisplayResult(); if (result instanceof ConfirmResult) { ConfirmResult cr = (ConfirmResult) result; if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) { diff --git a/console-ui/src/main/java/org/jline/consoleui/prompt/InputResult.java b/console-ui/src/main/java/org/jline/consoleui/prompt/InputResult.java index 34832628..ada53bc4 100644 --- a/console-ui/src/main/java/org/jline/consoleui/prompt/InputResult.java +++ b/console-ui/src/main/java/org/jline/consoleui/prompt/InputResult.java @@ -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() { diff --git a/console-ui/src/main/java/org/jline/consoleui/prompt/PromptResultItemIF.java b/console-ui/src/main/java/org/jline/consoleui/prompt/PromptResultItemIF.java index 73da5647..33927bce 100644 --- a/console-ui/src/main/java/org/jline/consoleui/prompt/PromptResultItemIF.java +++ b/console-ui/src/main/java/org/jline/consoleui/prompt/PromptResultItemIF.java @@ -10,4 +10,8 @@ public interface PromptResultItemIF { String getResult(); + + default String getDisplayResult() { + return getResult(); + } }