-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.java
72 lines (59 loc) · 1.7 KB
/
Editor.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
package src;
import src.models.Action;
import src.models.ActionType;
import src.services.ActionBuilder;
import src.services.FileReadWrite;
import src.services.PageEdit;
public class Editor {
private FileReadWrite fileRW;
private PageEdit pageEdit;
private ActionBuilder actionBuilder;
private boolean render;
public Editor() {
this.actionBuilder = new ActionBuilder(a -> {
return applyAction(a);
});
this.render = false;
}
private Void applyAction(Action action) {
if (action.getType() == ActionType.SAVE) {
this.saveFile();
} else if (action.getType() == ActionType.EXIT) {
this.closeEditor();
} else {
this.pageEdit.applyAction(action);
}
if (this.render) {
renderEditView();
}
return null;
}
private void renderEditView() {
System.out.print("\033[H\033[2J");
System.out.println(fileRW.getFileName() + " : ");
System.out.println(pageEdit.editView(10));
System.out.println(String.join(" | ", Action.CMD_LIST));
}
public void startRender() {
render = true;
renderEditView();
}
public void stopRender() {
render = false;
System.out.print("\033[H\033[2J");
}
public void openFile(String fileName) {
fileRW = new FileReadWrite(fileName);
pageEdit = new PageEdit(fileRW.readFile());
actionBuilder.listenToInput();
}
public void saveFile() {
fileRW.writeFile(pageEdit.getPage());
}
public void closeEditor() {
stopRender();
actionBuilder.close();
pageEdit.close();
System.exit(0);
}
}