From 7a0f3aa5d35c5d22a7ca485d53620eaccd36bd31 Mon Sep 17 00:00:00 2001 From: Carsten Otto Date: Sun, 11 Apr 2021 18:36:12 +0100 Subject: [PATCH] allow setting the prompt --- .../cotto/bitbook/cli/PromptChangeListener.java | 7 +++++++ .../cotto/bitbook/cli/CustomPromptProvider.java | 16 ++++++++++++++-- .../bitbook/cli/CustomPromptProviderTest.java | 15 ++++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 cli-base/src/main/java/de/cotto/bitbook/cli/PromptChangeListener.java diff --git a/cli-base/src/main/java/de/cotto/bitbook/cli/PromptChangeListener.java b/cli-base/src/main/java/de/cotto/bitbook/cli/PromptChangeListener.java new file mode 100644 index 00000000..2df75cc5 --- /dev/null +++ b/cli-base/src/main/java/de/cotto/bitbook/cli/PromptChangeListener.java @@ -0,0 +1,7 @@ +package de.cotto.bitbook.cli; + +public interface PromptChangeListener { + void changePrompt(String newState); + + void changePromptToDefault(); +} diff --git a/cli/src/main/java/de/cotto/bitbook/cli/CustomPromptProvider.java b/cli/src/main/java/de/cotto/bitbook/cli/CustomPromptProvider.java index d8f38ecd..d7683f08 100644 --- a/cli/src/main/java/de/cotto/bitbook/cli/CustomPromptProvider.java +++ b/cli/src/main/java/de/cotto/bitbook/cli/CustomPromptProvider.java @@ -6,7 +6,10 @@ import org.springframework.stereotype.Component; @Component -public class CustomPromptProvider implements PromptProvider { +public class CustomPromptProvider implements PromptProvider, PromptChangeListener { + private static final String DEFAULT_PROMPT = "BitBook$ "; + + private String prompt = DEFAULT_PROMPT; public CustomPromptProvider() { // default constructor @@ -14,7 +17,16 @@ public CustomPromptProvider() { @Override public AttributedString getPrompt() { - return new AttributedString("BitBook$ ", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW)); + return new AttributedString(prompt, AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW)); + } + + @Override + public void changePrompt(String newPrompt) { + prompt = newPrompt; } + @Override + public void changePromptToDefault() { + changePrompt(DEFAULT_PROMPT); + } } \ No newline at end of file diff --git a/cli/src/test/java/de/cotto/bitbook/cli/CustomPromptProviderTest.java b/cli/src/test/java/de/cotto/bitbook/cli/CustomPromptProviderTest.java index 696e8062..6de51698 100644 --- a/cli/src/test/java/de/cotto/bitbook/cli/CustomPromptProviderTest.java +++ b/cli/src/test/java/de/cotto/bitbook/cli/CustomPromptProviderTest.java @@ -9,7 +9,20 @@ class CustomPromptProviderTest { private final CustomPromptProvider customPromptProvider = new CustomPromptProvider(); @Test - void customPrompt_text() { + void default_prompt() { + assertThat(customPromptProvider.getPrompt()).hasToString("BitBook$ "); + } + + @Test + void changePrompt() { + customPromptProvider.changePrompt("foo"); + assertThat(customPromptProvider.getPrompt()).hasToString("foo"); + } + + @Test + void changePromptToDefault() { + customPromptProvider.changePrompt("foo"); + customPromptProvider.changePromptToDefault(); assertThat(customPromptProvider.getPrompt()).hasToString("BitBook$ "); } } \ No newline at end of file