Skip to content

Commit

Permalink
allow setting the prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Otto committed Apr 15, 2021
1 parent e21eea0 commit 7a0f3aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.cotto.bitbook.cli;

public interface PromptChangeListener {
void changePrompt(String newState);

void changePromptToDefault();
}
16 changes: 14 additions & 2 deletions cli/src/main/java/de/cotto/bitbook/cli/CustomPromptProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@
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
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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$ ");
}
}

0 comments on commit 7a0f3aa

Please sign in to comment.