Repository navigation
Expand file tree
/
Copy pathPrintPaste.java
More file actions
98 lines (90 loc) · 4.01 KB
/
Copy pathPrintPaste.java
File metadata and controls
98 lines (90 loc) · 4.01 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
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.codejive.miniterm:miniterm${miniterm.ffm:}:${miniterm.version:0.2.0}
//DEPS org.codejive.miniterm:ansiparser:${miniterm.version:0.2.0}
package examples;
import java.io.IOException;
import org.codejive.miniterm.Terminal;
import org.codejive.miniterm.ansiparser.AnsiParser;
public class PrintPaste {
private static final String ENABLE_BRACKETED_PASTE = "\u001b[?2004h";
private static final String DISABLE_BRACKETED_PASTE = "\u001b[?2004l";
private static final String PASTE_START = "\u001b[200~";
private static final String PASTE_END = "\u001b[201~";
public static void main(String[] args) {
try (Terminal terminal = Terminal.create()) {
terminal.enableRawMode();
terminal.write(ENABLE_BRACKETED_PASTE);
System.out.println("Bracketed paste mode enabled.");
System.out.println("Try pasting text (Ctrl+V or right-click paste).");
System.out.println("Press Ctrl+C to exit.\n");
boolean inPaste = false;
StringBuilder pasteBuffer = new StringBuilder();
while (true) {
String token = AnsiParser.parse(() -> terminal.read(1000));
if (token == null) {
break; // EOF
} else if (token.isEmpty()) {
continue; // timeout
} else if (token.equals("\u0003")) { // Ctrl+C
break;
} else if (token.equals(PASTE_START)) {
inPaste = true;
pasteBuffer.setLength(0);
System.out.println("[PASTE START]");
} else if (token.equals(PASTE_END)) {
inPaste = false;
System.out.print("\r\n[PASTE END]\r\n");
System.out.print("Pasted content (" + pasteBuffer.length() + " chars):\r\n");
System.out.print("---\r\n");
// Print with proper line endings for raw mode
String content = pasteBuffer.toString();
for (int i = 0; i < content.length(); i++) {
char ch = content.charAt(i);
if (ch == '\n') {
System.out.print("\r\n");
} else if (ch == '\r') {
// Skip standalone CR, will be handled with LF
if (i + 1 < content.length() && content.charAt(i + 1) != '\n') {
System.out.print("\r\n");
}
} else {
System.out.print(ch);
}
}
System.out.print("\r\n---\r\n\r\n");
} else if (inPaste) {
pasteBuffer.append(token);
} else {
// Regular typed input
if (token.length() == 1) {
char ch = token.charAt(0);
if (ch >= 32 && ch < 127) {
System.out.println("Typed: '" + ch + "' (code: " + (int) ch + ")");
} else {
System.out.println("Typed: code " + (int) ch);
}
} else {
System.out.println("Sequence: " + escapeString(token));
}
}
}
terminal.write(DISABLE_BRACKETED_PASTE);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String escapeString(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == '\u001b') {
sb.append("\\e");
} else if (ch >= 32 && ch < 127) {
sb.append(ch);
} else {
sb.append(String.format("\\x%02x", (int) ch));
}
}
return sb.toString();
}
}