-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodo.java
More file actions
269 lines (214 loc) · 8.78 KB
/
Todo.java
File metadata and controls
269 lines (214 loc) · 8.78 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.5.0
//JAVA 11+
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Command(name = "Todo", mixinStandardHelpOptions = true, version = "Todo 0.1",
description = "Todo made with jbang")
class Todo implements Callable<Integer> {
@Parameters(index = "0", description = "Text for new Todo item or comment to append on check / uncheck.", defaultValue = "")
private String text;
@Option(names = {"-t", "--todo", "-todo"}, description = "Create a new Todo item with given text.")
private boolean newTodo;
@Option(names = {"-n", "--new", "-new"}, description = "Create a new Todo file.")
private boolean newTodoFile;
@Option(names = {"-c", "--check", "-check"}, description = "Line number for item to check/uncheck and additional comment to append to the item line.")
private Long itemToCheck;
@Option(names = {"-l", "--list", "-list"}, description = "List todos of current file.")
private boolean printCurrentTodo;
public static void main(String... args) {
int exitCode = new CommandLine(new Todo()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() {
if (prepareTodo()) {
if (newTodoFile) {
this.createNewTodoFile();
}
if (printCurrentTodo) {
this.printCurrentTodoList();
} else if (newTodo) {
this.createNewTodoEntry();
} else if (itemToCheck != null && itemToCheck > 0) {
this.checkUncheckTodoItem();
}
} else {
System.err.println("Error occurred quitting ...");
}
return 0;
}
private Path getCurrentWorkingDir() {
String dir = System.getProperty("user.home");
String customDir = System.getenv("TODO_DIR");
Path path = null;
try {
if (customDir != null) {
dir = customDir;
}
dir = dir + File.separator + "Todo";
path = Paths.get(dir);
if (Files.notExists(path)) {
System.out.println("Working dir does not exists, will be created: " + path.toString());
Files.createDirectories(path);
}
} catch (IOException e) {
System.err.println("Failed to create directory (" + dir + ")!" + e.getMessage());
}
return path;
}
private Boolean prepareTodo() {
boolean success = true;
Path path = getCurrentWorkingDir();
if (path != null) {
try {
if (Files.notExists(path)) {
System.out.println("Working dir does not exists, will be created: " + path.toString());
Files.createDirectories(path);
}
} catch (IOException e) {
success = false;
System.err.println("Failed to create directory (" + path.toString() + ")!" + e.getMessage());
}
} else {
success = false;
System.err.println("Working directory not set!");
}
return success;
}
private void createNewTodoFile() {
ZonedDateTime date = ZonedDateTime.now(ZoneId.systemDefault());
String dateStr = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'kk_mm_ss").format(date);
Path dir = getCurrentWorkingDir();
Path fileToCreatePath = dir.resolve(dateStr + ".md");
try {
Path newFilePath = Files.createFile(fileToCreatePath);
} catch (IOException e) {
System.err.println("Failed to create new Todo file (" + fileToCreatePath.toString() + ")!" + e.getMessage());
}
System.out.println("\nCreated new Todo file: " + fileToCreatePath.toString() + "\n");
}
private Path getLatestTodo(boolean descending) {
Path path = null;
try (Stream<Path> stream = Files.list(getCurrentWorkingDir())) {
List<Path> pathList = stream
.filter(file -> !Files.isDirectory(file))
.collect(Collectors.toList());
Comparator<Path> comparator = Comparator.comparingLong(this::getFileCreationEpoch);
if (descending) {
comparator = comparator.reversed();
}
pathList.sort(comparator);
if (pathList.size() > 0) {
path = pathList.get(0);
}
} catch (IOException ex) {
System.err.println("Error on loading latest Todo!");
}
return path;
}
private long getFileCreationEpoch(Path filePath) {
try {
BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
return attr.creationTime()
.toInstant()
.toEpochMilli();
} catch (IOException e) {
throw new RuntimeException(filePath.toString(), e);
}
}
private void createNewTodoEntry() {
Path path = getLatestTodo(true);
if (path == null) {
System.err.println("No todo list found!");
} else {
String newTodoItem = "- [ ] " + this.text + System.lineSeparator();
try {
Files.write(
path,
newTodoItem.getBytes(),
StandardOpenOption.APPEND);
this.printTodoList(path);
} catch (IOException e) {
System.err.println("Failed to write new item to " + path.toString() + ")!" + e.getMessage());
}
}
}
private void printTodoList(Path path) throws IOException {
System.out.println("\n##########################################");
System.out.println("# " + path.getFileName());
System.out.println("##########################################");
Stream<String> lines = Files.lines(path);
lines.forEach(System.out::println);
System.out.println("\n");
lines.close();
}
private void checkUncheckTodoItem() {
Path path = null;
try {
path = getLatestTodo(true);
if (path == null) {
System.err.println("No todo list found!");
} else {
Stream<String> lines = Files.lines(path);
List<String> lineList = lines.collect(Collectors.toList());
lines.close();
String temp = lineList.get(this.itemToCheck.intValue() - 1);
if (temp.startsWith("- [ ]")) {
temp = temp.replaceFirst("- \\[ \\]", "- [x]");
} else if (temp.startsWith("- [x]")) {
temp = temp.replaceFirst("- \\[x\\]", "- [ ]");
}
ZonedDateTime date = ZonedDateTime.now(ZoneId.systemDefault());
String dateStr = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'kk:mm:ss").format(date);
if (this.text != null && !this.text.isEmpty()) {
temp += " | " + dateStr + " | " + this.text;
}
lineList.set(this.itemToCheck.intValue() - 1, temp);
BufferedWriter writer = Files.newBufferedWriter(path);
writer.write("");
writer.flush();
for (String line : lineList) {
String newLine = line + System.lineSeparator();
Files.write(
path,
newLine.getBytes(),
StandardOpenOption.APPEND);
}
this.printTodoList(path);
}
} catch (IOException e) {
System.err.println("Failed to check/uncheck item [" + this.itemToCheck + "] " + path.toString() + ")!" + e.getMessage());
}
}
private void printCurrentTodoList() {
Path path = getLatestTodo(true);
if (path == null) {
System.err.println("No todo list found!");
} else {
try {
this.printTodoList(path);
} catch (IOException e) {
System.err.println("Failed to read current todo list: " + path.toString() + ")!" + e.getMessage());
}
}
}
}