Skip to content

Commit

Permalink
Make sure ConsolePrompt cancellable is properly reset
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse committed Dec 18, 2024
1 parent ffce720 commit b2f9c20
Showing 1 changed file with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,39 +179,41 @@ public Map<String, PromptResultItemIF> prompt(
Deque<List<PromptableElementIF>> prevLists = new ArrayDeque<>();
Deque<Map<String, PromptResultItemIF>> prevResults = new ArrayDeque<>();
boolean cancellable = config.cancellableFirstPrompt();
// Get our first list of prompts
List<PromptableElementIF> peList = promptableElementLists.apply(new HashMap<>());
Map<String, PromptResultItemIF> 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<PromptableElementIF> peList = promptableElementLists.apply(new HashMap<>());
Map<String, PromptResultItemIF> 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;
}

/**
Expand Down

0 comments on commit b2f9c20

Please sign in to comment.