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 ad0c7cc9..0ebcf07f 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 @@ -179,39 +179,41 @@ public Map prompt( Deque> prevLists = new ArrayDeque<>(); Deque> prevResults = new ArrayDeque<>(); boolean cancellable = config.cancellableFirstPrompt(); - // Get our first list of prompts - List peList = promptableElementLists.apply(new HashMap<>()); - Map peResult = new HashMap<>(); - while (peList != null) { - // Second and later prompts should always be cancellable - config.setCancellableFirstPrompt(!prevLists.isEmpty() || cancellable); - // Prompt the user - prompt(headerIn, peList, peResult); - if (peResult.isEmpty()) { - // The prompt was cancelled by the user, so let's go back to the - // previous list of prompts and its results (if any) - peList = prevLists.pollFirst(); - peResult = prevResults.pollFirst(); - if (peResult != null) { - // Remove the results of the previous prompt from the main result map - peResult.forEach((k, v) -> resultMap.remove(k)); - headerIn.remove(headerIn.size() - 1); + try { + // Get our first list of prompts + List peList = promptableElementLists.apply(new HashMap<>()); + Map peResult = new HashMap<>(); + while (peList != null) { + // Second and later prompts should always be cancellable + config.setCancellableFirstPrompt(!prevLists.isEmpty() || cancellable); + // Prompt the user + prompt(headerIn, peList, peResult); + if (peResult.isEmpty()) { + // The prompt was cancelled by the user, so let's go back to the + // previous list of prompts and its results (if any) + peList = prevLists.pollFirst(); + peResult = prevResults.pollFirst(); + if (peResult != null) { + // Remove the results of the previous prompt from the main result map + peResult.forEach((k, v) -> resultMap.remove(k)); + headerIn.remove(headerIn.size() - 1); + } + } else { + // We remember the list of prompts and their results + prevLists.push(peList); + prevResults.push(peResult); + // Add the results to the main result map + resultMap.putAll(peResult); + // And we get our next list of prompts (if any) + peList = promptableElementLists.apply(resultMap); + peResult = new HashMap<>(); } - } else { - // We remember the list of prompts and their results - prevLists.push(peList); - prevResults.push(peResult); - // Add the results to the main result map - resultMap.putAll(peResult); - // And we get our next list of prompts (if any) - peList = promptableElementLists.apply(resultMap); - peResult = new HashMap<>(); } + return resultMap; + } finally { + // Restore the original state of cancellable + config.setCancellableFirstPrompt(cancellable); } - // Restore the original state of cancellable - config.setCancellableFirstPrompt(cancellable); - - return resultMap; } /**