|
5 | 5 | package org.codejive.miniterm; |
6 | 6 |
|
7 | 7 | import java.io.IOException; |
8 | | -import java.nio.charset.Charset; |
9 | | -import java.util.function.Consumer; |
10 | 8 |
|
11 | 9 | /** |
12 | 10 | * Platform-independent terminal operations interface. |
13 | 11 | * |
14 | 12 | * <p>This interface abstracts the low-level terminal operations that differ between Unix |
15 | 13 | * (Linux/macOS) and Windows platforms. |
16 | 14 | */ |
17 | | -public interface Terminal extends Appendable, AutoCloseable { |
18 | | - |
19 | | - /** |
20 | | - * Enables raw mode on the terminal. |
21 | | - * |
22 | | - * <p>Raw mode disables line buffering, echo, and special character processing, allowing direct |
23 | | - * character-by-character input. |
24 | | - * |
25 | | - * @throws IOException if raw mode cannot be enabled |
26 | | - */ |
27 | | - void enableRawMode() throws IOException; |
28 | | - |
29 | | - /** |
30 | | - * Disables raw mode and restores original terminal attributes. |
31 | | - * |
32 | | - * @throws IOException if raw mode cannot be disabled |
33 | | - */ |
34 | | - void disableRawMode() throws IOException; |
35 | | - |
36 | | - /** |
37 | | - * Gets the current terminal size. |
38 | | - * |
39 | | - * @return the terminal size |
40 | | - * @throws IOException if the size cannot be determined |
41 | | - */ |
42 | | - Size getSize() throws IOException; |
43 | | - |
44 | | - /** |
45 | | - * Reads a single character from the terminal with timeout. |
46 | | - * |
47 | | - * @param timeoutMs timeout in milliseconds (-1 for infinite, 0 for non-blocking) |
48 | | - * @return the character read, -1 for EOF, or -2 for timeout |
49 | | - * @throws IOException if reading fails |
50 | | - */ |
51 | | - int read(int timeoutMs) throws IOException; |
52 | | - |
53 | | - /** |
54 | | - * Peeks at the next character without consuming it. |
55 | | - * |
56 | | - * @param timeoutMs timeout in milliseconds |
57 | | - * @return the character peeked, -1 for EOF, or -2 for timeout |
58 | | - * @throws IOException if reading fails |
59 | | - */ |
60 | | - int peek(int timeoutMs) throws IOException; |
61 | | - |
62 | | - /** |
63 | | - * Writes data to the terminal. |
64 | | - * |
65 | | - * @param data the data to write |
66 | | - * @throws IOException if writing fails |
67 | | - */ |
68 | | - void write(byte[] data) throws IOException; |
69 | | - |
70 | | - /** |
71 | | - * Writes a portion of a byte array to the terminal. |
72 | | - * |
73 | | - * <p>This method allows writing from a reusable buffer without creating intermediate byte array |
74 | | - * copies. |
75 | | - * |
76 | | - * @param buffer the byte array containing data |
77 | | - * @param offset the start offset in the buffer |
78 | | - * @param length the number of bytes to write |
79 | | - * @throws IOException if writing fails |
80 | | - */ |
81 | | - void write(byte[] buffer, int offset, int length) throws IOException; |
82 | | - |
83 | | - /** |
84 | | - * Writes a string to the terminal. |
85 | | - * |
86 | | - * @param s the string to write |
87 | | - * @throws IOException if writing fails |
88 | | - */ |
89 | | - void write(String s) throws IOException; |
90 | | - |
91 | | - @Override |
92 | | - default Appendable append(char c) throws IOException { |
93 | | - write(new byte[] {(byte) c}); |
94 | | - return this; |
95 | | - } |
96 | | - |
97 | | - @Override |
98 | | - default Appendable append(CharSequence csq) throws IOException { |
99 | | - write(csq.toString()); |
100 | | - return this; |
101 | | - } |
102 | | - |
103 | | - @Override |
104 | | - default Appendable append(CharSequence csq, int start, int end) throws IOException { |
105 | | - write(csq.subSequence(start, end).toString()); |
106 | | - return this; |
107 | | - } |
108 | | - |
109 | | - /** |
110 | | - * Returns the charset used for terminal I/O. |
111 | | - * |
112 | | - * @return the terminal charset |
113 | | - */ |
114 | | - Charset getCharset(); |
115 | | - |
116 | | - /** |
117 | | - * Checks if raw mode is currently enabled. |
118 | | - * |
119 | | - * @return true if raw mode is enabled |
120 | | - */ |
121 | | - boolean isRawModeEnabled(); |
122 | | - |
123 | | - /** |
124 | | - * Registers a handler to be called when the terminal is resized. |
125 | | - * |
126 | | - * @param handler the handler to call on resize, or null to remove |
127 | | - */ |
128 | | - void onResize(Consumer<Size> handler); |
129 | | - |
130 | | - /** |
131 | | - * Closes the terminal and releases resources. |
132 | | - * |
133 | | - * @throws IOException if closing fails |
134 | | - */ |
135 | | - @Override |
136 | | - void close() throws IOException; |
| 15 | +public interface Terminal extends TerminalBase { |
137 | 16 |
|
138 | 17 | /** |
139 | 18 | * Factory method to create a Terminal for the current platform using FFM (Java 22+). |
@@ -175,34 +54,4 @@ public static Terminal create() throws IOException { |
175 | 54 | throw new IOException( |
176 | 55 | "Failed to create terminal for platform: " + System.getProperty("os.name"), last); |
177 | 56 | } |
178 | | - |
179 | | - public static class Size { |
180 | | - |
181 | | - public final int height; |
182 | | - public final int width; |
183 | | - |
184 | | - public Size(int width, int height) { |
185 | | - this.width = width; |
186 | | - this.height = height; |
187 | | - } |
188 | | - |
189 | | - @Override |
190 | | - public boolean equals(Object o) { |
191 | | - if (this == o) return true; |
192 | | - if (o == null || getClass() != o.getClass()) return false; |
193 | | - Size size = (Size) o; |
194 | | - if (height != size.height) return false; |
195 | | - return width == size.width; |
196 | | - } |
197 | | - |
198 | | - @Override |
199 | | - public int hashCode() { |
200 | | - return 31 * height + width; |
201 | | - } |
202 | | - |
203 | | - @Override |
204 | | - public String toString() { |
205 | | - return width + "x" + height; |
206 | | - } |
207 | | - } |
208 | 57 | } |
0 commit comments