|
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; |
8 | 10 |
|
9 | 11 | /** |
10 | 12 | * Platform-independent terminal operations interface. |
11 | 13 | * |
12 | 14 | * <p>This interface abstracts the low-level terminal operations that differ between Unix |
13 | 15 | * (Linux/macOS) and Windows platforms. |
14 | 16 | */ |
15 | | -public interface Terminal extends TerminalBase { |
| 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 size() 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 charset(); |
| 115 | + |
| 116 | + /** |
| 117 | + * Checks if raw mode is currently enabled. |
| 118 | + * |
| 119 | + * @return true if raw mode is enabled |
| 120 | + */ |
| 121 | + boolean rawModeEnabled(); |
| 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; |
| 137 | + |
| 138 | + public static class Size { |
| 139 | + |
| 140 | + public final int height; |
| 141 | + public final int width; |
| 142 | + |
| 143 | + public Size(int width, int height) { |
| 144 | + this.width = width; |
| 145 | + this.height = height; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean equals(Object o) { |
| 150 | + if (this == o) return true; |
| 151 | + if (o == null || getClass() != o.getClass()) return false; |
| 152 | + Size size = (Size) o; |
| 153 | + if (height != size.height) return false; |
| 154 | + return width == size.width; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public int hashCode() { |
| 159 | + return 31 * height + width; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String toString() { |
| 164 | + return width + "x" + height; |
| 165 | + } |
| 166 | + } |
16 | 167 |
|
17 | 168 | /** |
18 | 169 | * Factory method to create a Terminal for the current platform using FFM (Java 22+). |
|
0 commit comments