-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrinter.java
237 lines (206 loc) · 7.93 KB
/
Printer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
** IUT Nancy-Charlemagne, 2021
** Projet :
** Project Name
** Author :
** Antoine Orion
** File description :
** Display/print the mazes on the standard output or to a file
*/
import java.util.Map;
import java.util.Hashtable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.Help.Ansi;
class Printer implements AutoCloseable {
File file;
BufferedWriter writer;
boolean verbose;
boolean color;
Map<Character, String> colors_dic;
int stepping;
static int step_bro = 0;
boolean prettyPrint;
public Printer(ParseResult pr) throws IOException {
this.file = pr.commandSpec().findOption("output").getValue();
this.verbose = pr.commandSpec().findOption("verbose").getValue();
this.color = pr.commandSpec().findOption("color").getValue();
this.colors_dic = new Hashtable<Character, String>();
this.stepping = pr.commandSpec().findOption("step").getValue();
this.step_bro = 0;
this.prettyPrint = pr.commandSpec().findOption("pretty-print").getValue();
this.colors_dic.put(MazeElement.WALL.getChar(), "white");
this.colors_dic.put(MazeElement.WALL_UNVISITED.getChar(), "bg(5;5;5)"); //
this.colors_dic.put(MazeElement.WALL_VISITED.getChar(), "white");
this.colors_dic.put(MazeElement.PATH.getChar(), "yellow");
this.colors_dic.put(MazeElement.PATH_UNVISITED.getChar(), "red");
this.colors_dic.put(MazeElement.PATH_VISITED.getChar(), "bold,cyan");
// instead of red|green, we can put alot of things here, see
// last comment for more infos -> option...?
char c = '\0';
if (this.file == null)
this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
else {
if (this.file != null && this.file.exists()) {
this.print(MessageLevel.IMPORTANT, "Warning : File " + this.file + " already exist. Do you wants to override ? y/N:", ' ');
do {
try {
c = (char) System.in.read();
print(MessageLevel.DEBUG, "read : " + c);
} catch (IOException e) {
this.print(MessageLevel.IMPORTANT, "Issue when reading the standard intput.");
}
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
if (c == 'n' || c == 'N')
this.print(MessageLevel.FATAL, "Stopped by user : File \"" + this.file + "\" won't be overwrite.");
}
this.writer = new BufferedWriter(new FileWriter(this.file));
}
}
public void print(String message) {
this.print(MessageLevel.INFO, message);
}
public void print(MessageLevel level, String message) {
this.print(level, message, '\n');
}
public void print(MessageLevel level, String msg, char end) {
if (level != MessageLevel.DEBUG && (this.verbose || level == MessageLevel.IMPORTANT || level == MessageLevel.FATAL))
for (int startbold = 0, endbold = -1; (startbold = msg.substring(startbold).indexOf("**")) != -1 && (endbold = msg.substring(startbold += 2).indexOf("**")) != -1; startbold = endbold + 2) //TODO a function
System.out.print(msg.substring(0, startbold - 2) +
Ansi.AUTO.string("@|bold "+ msg.substring(startbold, endbold += startbold ) + "|@")
+ msg.substring(endbold + 2) + end);
if (level == MessageLevel.FATAL)
System.exit(42);
}
public void displayValue(String str, String value) { // value : double -> T ?
this.displayValue(str, value, "green");
}
public void displayValue(String str, String value, String color) {
if (this.color)
this.print(MessageLevel.IMPORTANT, String.format(str, Ansi.AUTO.string("@|"+ color + ' ' + value + "|@")));
else
this.print(MessageLevel.IMPORTANT, String.format(str, value));
}
public void display(Maze maze) {
try {
if (this.color) {
displayColor(maze, stepping > 0);
if (this.verbose)
displayColorCode();
} else
displayBlakAndWhite(maze, stepping > 0);
} catch (IOException e) {
print(MessageLevel.FATAL, e.getMessage());
}
}
public void stepDisplay(Maze maze) {
char c = '\0';
if (this.stepping <= 0 || step_bro < this.stepping) {
this.step_bro += 1;
return;
}
this.print(MessageLevel.INFO, "displayed every " + this.stepping + " loop.");
this.step_bro = 0;
this.display(maze);
try {
this.print(MessageLevel.IMPORTANT, "------------------\n");
this.print(MessageLevel.IMPORTANT, "Presse [Enter] to continue or insert a new step number : ");
do {
c = (char) System.in.read();
print(MessageLevel.DEBUG, "read : " + c);
} while (c != '\n'); //turn into reader ?
} catch (IOException e) {
this.print(MessageLevel.FATAL, "Error when trying to read from standard intput");
}
}
private void displayColorCode() throws IOException {
char c;
this.writer.write("Color codes :\n");
for (MazeElement value : MazeElement.values()) {
if (value == MazeElement.UNDEFINED)
continue;
c = value.getChar();
this.writer.write('\t' + Ansi.AUTO.string("@|" + this.colors_dic.get(c) + ' ' + c + "|@") + " : " + value.name() + '\n');
}
}
private void displayColor(Maze maze, boolean stepping) throws IOException {
StringBuffer strbuf = new StringBuffer();
char onHold = maze.getElement(0, 0).getChar();
char node = onHold;
for (int y = 0; y < maze.getHeight(); y++, this.writer.write('\n')) {
for (int x = 0; x < maze.getWidth(); x++, strbuf.append(node)) {
node = stepping ? maze.getValue(x, y) : maze.getElement(x, y).getChar();
if (node != onHold) {
this.writer.write(Ansi.AUTO.string("@|" + this.colors_dic.get(onHold) + ' ' + strbuf + "|@"));
strbuf.setLength(0);
onHold = node;
}
}
this.writer.write(Ansi.AUTO.string("@|" + this.colors_dic.get(onHold) + ' ' + strbuf + "|@"));
strbuf.setLength(0);
}
this.writer.write(Ansi.AUTO.string("@|" + this.colors_dic.get(onHold) + ' ' + strbuf + "|@"));
this.writer.flush();
}
private void displayBlakAndWhite(Maze maze, boolean stepping) throws IOException {
for (int y = 0; y < maze.getHeight(); y++, this.writer.write('\n')) {
for (int x = 0; x < maze.getWidth(); x++) {
if (stepping)
this.writer.write(maze.getValue(x, y));
else if (this.prettyPrint)
this.writer.write(this.chooseDisplayChar(maze, x, y));
else
this.writer.write(maze.getElement(x, y).getChar());
}
}
this.writer.flush();
}
@Override
public void close() {
try {
this.print(MessageLevel.DEBUG, "Closing filewriter");
this.writer.close();
} catch (Exception e) {
this.print(MessageLevel.FATAL, "Printer failed to close writer normally");
}
}
public char chooseDisplayChar(Maze maze, int x, int y) {
// │ ─ +
if (x == 0 && y == 0 && maze.getElement(x+1, y) == MazeElement.PATH)
return ('→');
else if (x == 0 && y == 0)
return ('↓');
if (x == maze.getWidth()-1 && y == maze.getHeight()-1 && maze.getElement(x-1, y) == MazeElement.PATH)
return ('→');
else if (x == maze.getWidth()-1 && y == maze.getHeight()-1)
return ('↓');
if (x == 0 && y == maze.getHeight() - 1)
return ('└');
if (x == maze.getWidth() - 1 && y == 0)
return ('┐');
if (maze.getElement(x, y) == MazeElement.PATH)
return (' ');
// Cases at border
if ((x == 0 /*&& maze.getElement(x+1, y) == MazeElement.PATH*/) ||
(x == maze.getWidth()-1 /*&& maze.getElement(x-1, y) == MazeElement.PATH*/))
return ('│');
else if ((y == 0 /*&& maze.getElement(x, y+1) == MazeElement.PATH*/) ||
(y == maze.getHeight()-1 /*&& maze.getElement(x, y-1) == MazeElement.PATH*/))
return ('─');
// Cases in middle
if (maze.getElement(x-1, y) == MazeElement.PATH && maze.getElement(x+1, y) == MazeElement.PATH)
return ('│');
else if (maze.getElement(x, y-1) == MazeElement.PATH && maze.getElement(x, y+1) == MazeElement.PATH)
return ('─');
// Default case
return ('┼');
}
}
// String str = Ansi.AUTO.string("@|bold,green,underline Hello, colored world!|@");
// System.out.println(str);
// @|bg(0;5;0) text with red=0, green=5, blue=0 background|@, or
// @|fg(46) the same color by index, as foreground color|@.