Skip to content

Commit

Permalink
add terminal support class
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Otto committed May 9, 2021
1 parent 16ec10a commit e96c0a8
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cli/base/src/main/java/de/cotto/bitbook/cli/TerminalSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.cotto.bitbook.cli;

import org.apache.commons.lang3.StringUtils;
import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.util.Optional;

@Component
public class TerminalSupport {
private final LineReader lineReader;
private final PromptChangeListener promptChangeListener;
private final PrintWriter writer;

public TerminalSupport(Terminal terminal, LineReader lineReader, PromptChangeListener promptChangeListener) {
this.lineReader = lineReader;
this.promptChangeListener = promptChangeListener;
writer = terminal.writer();
}

public void write(String text) {
writer.println(text);
writer.flush();
}

public Optional<String> request(String prompt) {
String answer = lineReader.readLine(prompt + " ");
if (StringUtils.isBlank(answer)) {
return Optional.empty();
} else {
return Optional.of(answer);
}
}

public void changePrompt(String newPrompt) {
promptChangeListener.changePrompt(newPrompt);
}

public void changePromptToDefault() {
promptChangeListener.changePromptToDefault();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package de.cotto.bitbook.cli;

import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.PrintWriter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class TerminalSupportTest {
private TerminalSupport terminalSupport;

@Mock
private Terminal terminal;

@Mock
private LineReader lineReader;

@Mock
private PromptChangeListener promptChangeListener;

@Mock
private PrintWriter writer;

@BeforeEach
void setUp() {
when(terminal.writer()).thenReturn(writer);
terminalSupport = new TerminalSupport(terminal, lineReader, promptChangeListener);
}

@Test
void write() {
terminalSupport.write("x");
InOrder inOrder = Mockito.inOrder(writer);
inOrder.verify(writer).println("x");
inOrder.verify(writer).flush();
}

@Test
void request_prompt() {
terminalSupport.request("?");
verify(lineReader).readLine("? ");
}

@Test
void request_empty() {
when(lineReader.readLine(anyString())).thenReturn("");
assertThat(terminalSupport.request("?")).isEmpty();
}

@Test
void request_blank() {
when(lineReader.readLine(anyString())).thenReturn("\t \r\n");
assertThat(terminalSupport.request("?")).isEmpty();
}

@Test
void request() {
when(lineReader.readLine(anyString())).thenReturn("hello");
assertThat(terminalSupport.request("?")).contains("hello");
}

@Test
void changePrompt() {
terminalSupport.changePrompt("x");
verify(promptChangeListener).changePrompt("x");
}

@Test
void changePromptToDefault() {
terminalSupport.changePromptToDefault();
verify(promptChangeListener).changePromptToDefault();
}
}

0 comments on commit e96c0a8

Please sign in to comment.