-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzdate
More file actions
executable file
·75 lines (58 loc) · 1.87 KB
/
Copy pathzdate
File metadata and controls
executable file
·75 lines (58 loc) · 1.87 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
#!/usr/bin/env -S java --source 25
/**
* zdate - prints and copies the current date to the system clipboard
* default: dd.MM.yyyy, -y: yyyy.MM.dd
*/
import module java.desktop;
String version = "2026-02-14.1";
enum Log {
ERROR(Color.BRIGHT_RED, System.err),
INFO(Color.BRIGHT_GREEN, System.out),
SYSTEM(Color.SKY_BLUE, System.out);
PrintStream out;
enum Color {
BRIGHT_RED("\033[38;5;196m"),
BRIGHT_GREEN("\033[38;5;46m"),
SKY_BLUE("\033[38;5;39m");
String code;
Color(String code) {
this.code = code;
}
}
private final String value;
private final static String RESET = "\u001B[0m";
private Log(Color color, PrintStream out) {
this.value = (color.code + "%s" + RESET);
this.out = out;
}
public void out(String message) {
this.out.println(this.value.formatted(message));
}
public static void error(String message) {
Log.ERROR.out(message);
}
public static void info(String message) {
Log.INFO.out(message);
}
public static void stop(String message) {
error(message);
System.exit(1);
}
}
void main(String[] args) {
var pattern = args.length > 0 && "-y".equals(args[0]) ? "yyyy.MM.dd" : "dd.MM.yyyy";
var now = LocalDate.now();
var dayOfWeek = now.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
var date = now.format(DateTimeFormatter.ofPattern(pattern));
copyToClipboard(date);
Log.info("zdate v%s - %s %s (copied to clipboard)"
.formatted(version,dayOfWeek,date));
}
void copyToClipboard(String text) {
try {
var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(text), null);
} catch (Exception e) {
Log.stop("Failed to copy to clipboard: " + e.getMessage());
}
}