2021-11-05 12:03:27 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class TextOutput {
|
|
|
|
private boolean formatRaw;
|
2021-11-05 15:08:25 +01:00
|
|
|
private int columnWidth = 10;
|
2021-11-05 12:03:27 +01:00
|
|
|
|
|
|
|
public void print(ArrayList<String> text) {
|
2021-11-05 15:08:25 +01:00
|
|
|
if (formatRaw) {
|
|
|
|
printFormated(text);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printUnformated(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void printFormated(ArrayList<String> text) {
|
2021-11-05 12:03:27 +01:00
|
|
|
for (int counter = 0; counter < text.size(); counter++) {
|
|
|
|
System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">");
|
|
|
|
}
|
2021-11-05 15:08:25 +01:00
|
|
|
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
2021-11-05 15:08:25 +01:00
|
|
|
private void printUnformated(ArrayList<String> text) {
|
2021-11-05 12:03:27 +01:00
|
|
|
for (String paragraph : text) {
|
|
|
|
String[] words = paragraph.split(" ");
|
|
|
|
int currentLength = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
for (String word : words) {
|
|
|
|
if (word.length() > columnWidth) {
|
2021-11-05 12:03:27 +01:00
|
|
|
String[] letters = word.split("");
|
|
|
|
int letterLenght = letters.length;
|
|
|
|
int lettersPrinted = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
do {
|
2021-11-05 16:16:39 +01:00
|
|
|
//System.out.println();
|
2021-11-05 12:03:27 +01:00
|
|
|
currentLength = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
for (int i = 0; i < columnWidth; i++) {
|
|
|
|
if (letterLenght > 0) {
|
2021-11-05 12:03:27 +01:00
|
|
|
System.out.print(letters[lettersPrinted]);
|
|
|
|
letterLenght--;
|
|
|
|
lettersPrinted++;
|
|
|
|
currentLength++;
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 16:16:39 +01:00
|
|
|
if(!(letterLenght == 0)){
|
|
|
|
System.out.println();
|
|
|
|
}
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
2021-11-05 16:16:39 +01:00
|
|
|
while (letterLenght >= columnWidth);
|
2021-11-05 12:03:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2021-11-05 15:08:25 +01:00
|
|
|
if (word.length() >= columnWidth - currentLength) {
|
2021-11-05 12:03:27 +01:00
|
|
|
currentLength = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
System.out.println();
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
|
|
|
System.out.print(word + " ");
|
|
|
|
currentLength += word.length() + 1;
|
2021-11-05 16:29:29 +01:00
|
|
|
}
|
2021-11-05 16:16:39 +01:00
|
|
|
}
|
|
|
|
System.out.println(); //added
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void formatRaw() {
|
|
|
|
formatRaw = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void formatFix(int length) {
|
|
|
|
formatRaw = false;
|
|
|
|
columnWidth = length;
|
|
|
|
}
|
2021-11-05 15:08:25 +01:00
|
|
|
public void errorInvalidString() {
|
|
|
|
System.err.println("Invalid String");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void errorInvalidCommand() {
|
|
|
|
System.err.println("Invalid Command");
|
|
|
|
}
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|