Skip to content

Commit 80cf687

Browse files
PixPMusicJulius Marmingejuliusmarmingecodex
authored
feat(mobile): Android terminal improvements — input, selection, sessions, Nerd Font (#3775)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
1 parent 57e6060 commit 80cf687

14 files changed

Lines changed: 1033 additions & 82 deletions

File tree

‎apps/mobile/modules/t3-terminal/THIRD_PARTY_NOTICES.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ The Android terminal renderer vendors upstream `libghostty-vt` shared libraries
2525

2626
Ghostty's MIT license applies to the vendored Android libraries. Keep this notice in sync when
2727
updating `Vendor/libghostty-vt`.
28+
29+
## MesloLGS NF (Android terminal font)
30+
31+
- Files: `android/src/main/assets/fonts/MesloLGS-NF-{Regular,Bold}.ttf`
32+
- Source: https://github.com/romkatv/powerlevel10k-media (Meslo LG patched with Nerd Fonts glyphs)
33+
- Upstream: Meslo LG by André Berg (customization of Apple's Menlo), Nerd Fonts patcher
34+
- License: Apache License 2.0
Binary file not shown.
Binary file not shown.

‎apps/mobile/modules/t3-terminal/android/src/main/cpp/t3_terminal_jni.cpp‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ std::vector<uint8_t> DrainResponses(Session* session) {
171171
return responses;
172172
}
173173

174+
bool ViewportGridRef(Session* session, jint x, jint y, GhosttyGridRef* out) {
175+
*out = GhosttyGridRef{};
176+
out->size = sizeof(*out);
177+
GhosttyPoint point{};
178+
point.tag = GHOSTTY_POINT_TAG_VIEWPORT;
179+
point.value.coordinate.x = static_cast<uint16_t>(std::max<jint>(x, 0));
180+
point.value.coordinate.y = static_cast<uint32_t>(std::max<jint>(y, 0));
181+
return ghostty_terminal_grid_ref(session->terminal, point, out) == GHOSTTY_SUCCESS;
182+
}
183+
174184
uint16_t StyleFlags(const GhosttyStyle& style, bool selected) {
175185
uint16_t flags = 0;
176186
if (style.bold) flags |= kBold;
@@ -272,6 +282,96 @@ Java_expo_modules_t3terminal_GhosttyBridge_nativeSetTheme(
272282
ApplyTheme(session, foreground, background, cursor, env, palette);
273283
}
274284

285+
extern "C" JNIEXPORT jboolean JNICALL
286+
Java_expo_modules_t3terminal_GhosttyBridge_nativeSelectWordAt(JNIEnv*, jclass,
287+
jlong handle, jint x,
288+
jint y) {
289+
auto* session = FromHandle(handle);
290+
if (session == nullptr) return JNI_FALSE;
291+
std::lock_guard<std::mutex> lock(session->mutex);
292+
GhosttyTerminalSelectWordOptions options{};
293+
options.size = sizeof(options);
294+
if (!ViewportGridRef(session, x, y, &options.ref)) return JNI_FALSE;
295+
GhosttySelection selection{};
296+
selection.size = sizeof(selection);
297+
if (ghostty_terminal_select_word(session->terminal, &options, &selection) !=
298+
GHOSTTY_SUCCESS) {
299+
return JNI_FALSE;
300+
}
301+
return ghostty_terminal_set(session->terminal, GHOSTTY_TERMINAL_OPT_SELECTION,
302+
&selection) == GHOSTTY_SUCCESS
303+
? JNI_TRUE
304+
: JNI_FALSE;
305+
}
306+
307+
extern "C" JNIEXPORT void JNICALL
308+
Java_expo_modules_t3terminal_GhosttyBridge_nativeExtendSelection(
309+
JNIEnv*, jclass, jlong handle, jint anchor_x, jint anchor_y, jint x, jint y) {
310+
auto* session = FromHandle(handle);
311+
if (session == nullptr) return;
312+
std::lock_guard<std::mutex> lock(session->mutex);
313+
GhosttySelection selection{};
314+
selection.size = sizeof(selection);
315+
if (!ViewportGridRef(session, anchor_x, anchor_y, &selection.start)) return;
316+
if (!ViewportGridRef(session, x, y, &selection.end)) return;
317+
ghostty_terminal_set(session->terminal, GHOSTTY_TERMINAL_OPT_SELECTION, &selection);
318+
}
319+
320+
extern "C" JNIEXPORT jboolean JNICALL
321+
Java_expo_modules_t3terminal_GhosttyBridge_nativeSelectAll(JNIEnv*, jclass,
322+
jlong handle) {
323+
auto* session = FromHandle(handle);
324+
if (session == nullptr) return JNI_FALSE;
325+
std::lock_guard<std::mutex> lock(session->mutex);
326+
GhosttySelection selection{};
327+
selection.size = sizeof(selection);
328+
if (ghostty_terminal_select_all(session->terminal, &selection) != GHOSTTY_SUCCESS) {
329+
return JNI_FALSE;
330+
}
331+
return ghostty_terminal_set(session->terminal, GHOSTTY_TERMINAL_OPT_SELECTION,
332+
&selection) == GHOSTTY_SUCCESS
333+
? JNI_TRUE
334+
: JNI_FALSE;
335+
}
336+
337+
extern "C" JNIEXPORT void JNICALL
338+
Java_expo_modules_t3terminal_GhosttyBridge_nativeClearSelection(JNIEnv*, jclass,
339+
jlong handle) {
340+
auto* session = FromHandle(handle);
341+
if (session == nullptr) return;
342+
std::lock_guard<std::mutex> lock(session->mutex);
343+
ghostty_terminal_set(session->terminal, GHOSTTY_TERMINAL_OPT_SELECTION, nullptr);
344+
}
345+
346+
// Returns the active selection as UTF-8 bytes (soft-wrapped lines unwrapped,
347+
// trailing whitespace trimmed), or null when there is no selection.
348+
extern "C" JNIEXPORT jbyteArray JNICALL
349+
Java_expo_modules_t3terminal_GhosttyBridge_nativeGetSelectionText(JNIEnv* env, jclass,
350+
jlong handle) {
351+
auto* session = FromHandle(handle);
352+
if (session == nullptr) return nullptr;
353+
std::lock_guard<std::mutex> lock(session->mutex);
354+
GhosttyTerminalSelectionFormatOptions options{};
355+
options.size = sizeof(options);
356+
options.emit = GHOSTTY_FORMATTER_FORMAT_PLAIN;
357+
options.unwrap = true;
358+
options.trim = true;
359+
uint8_t* bytes = nullptr;
360+
size_t len = 0;
361+
if (ghostty_terminal_selection_format_alloc(session->terminal, nullptr, options,
362+
&bytes, &len) != GHOSTTY_SUCCESS ||
363+
bytes == nullptr) {
364+
return nullptr;
365+
}
366+
auto result = env->NewByteArray(static_cast<jsize>(len));
367+
if (result != nullptr && len > 0) {
368+
env->SetByteArrayRegion(result, 0, static_cast<jsize>(len),
369+
reinterpret_cast<const jbyte*>(bytes));
370+
}
371+
ghostty_free(nullptr, bytes, len);
372+
return result;
373+
}
374+
275375
extern "C" JNIEXPORT jbyteArray JNICALL
276376
Java_expo_modules_t3terminal_GhosttyBridge_nativeSnapshot(JNIEnv* env, jclass,
277377
jlong handle) {

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/GhosttyBridge.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ internal object GhosttyBridge {
4444
)
4545

4646
@JvmStatic external fun nativeSnapshot(handle: Long): ByteArray
47+
48+
@JvmStatic external fun nativeSelectWordAt(handle: Long, col: Int, row: Int): Boolean
49+
50+
@JvmStatic
51+
external fun nativeExtendSelection(
52+
handle: Long,
53+
anchorCol: Int,
54+
anchorRow: Int,
55+
col: Int,
56+
row: Int
57+
)
58+
59+
@JvmStatic external fun nativeSelectAll(handle: Long): Boolean
60+
61+
@JvmStatic external fun nativeClearSelection(handle: Long)
62+
63+
@JvmStatic external fun nativeGetSelectionText(handle: Long): ByteArray?
4764
}

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
106106
}
107107
}
108108
terminalCanvas.onCellMetricsChanged = { emitResize() }
109+
terminalCanvas.selectionDelegate = object : TerminalSelectionDelegate {
110+
override fun selectWordAt(col: Int, row: Int): Boolean {
111+
if (terminalHandle == 0L) return false
112+
val selected = GhosttyBridge.nativeSelectWordAt(terminalHandle, col, row)
113+
if (selected) renderSnapshot()
114+
return selected
115+
}
116+
117+
override fun extendSelection(anchorCol: Int, anchorRow: Int, col: Int, row: Int) {
118+
if (terminalHandle == 0L) return
119+
GhosttyBridge.nativeExtendSelection(terminalHandle, anchorCol, anchorRow, col, row)
120+
renderSnapshot()
121+
}
122+
123+
override fun selectAll(): Boolean {
124+
if (terminalHandle == 0L) return false
125+
val selected = GhosttyBridge.nativeSelectAll(terminalHandle)
126+
if (selected) renderSnapshot()
127+
return selected
128+
}
129+
130+
override fun clearSelection() {
131+
if (terminalHandle == 0L) return
132+
GhosttyBridge.nativeClearSelection(terminalHandle)
133+
renderSnapshot()
134+
}
135+
136+
override fun selectionText(): String? =
137+
if (terminalHandle == 0L) {
138+
null
139+
} else {
140+
GhosttyBridge.nativeGetSelectionText(terminalHandle)?.let { String(it, Charsets.UTF_8) }
141+
}
142+
}
109143

110144
configureInputView()
111145
container.addView(
@@ -147,6 +181,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
147181
terminalCanvas.onScrollRows = null
148182
terminalCanvas.onRequestKeyboard = null
149183
terminalCanvas.onCellMetricsChanged = null
184+
terminalCanvas.selectionDelegate = null
150185
destroyTerminal()
151186
}
152187

@@ -174,7 +209,8 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
174209
event.action == KeyEvent.ACTION_DOWN
175210
val isEnter = isImeSend || isHardwareEnter
176211
if (isEnter) {
177-
onInput(mapOf("data" to "\n"))
212+
// Enter must send CR: raw-mode TUIs treat LF as Ctrl+J (insert newline).
213+
onInput(mapOf("data" to "\r"))
178214
true
179215
} else {
180216
false
@@ -288,6 +324,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
288324
GhosttyBridge.nativeDestroy(terminalHandle)
289325
terminalHandle = 0L
290326
fedBuffer = ""
327+
terminalCanvas.resetSelectionState()
291328
}
292329

293330
private fun feedPendingBuffer() {
@@ -299,6 +336,12 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
299336
val suffix = initialBuffer.substring(fedBuffer.length)
300337
if (suffix.isNotEmpty()) {
301338
emitResponse(GhosttyBridge.nativeFeed(terminalHandle, suffix.toByteArray(Charsets.UTF_8)))
339+
// New output invalidates an active selection (matches the web drawer);
340+
// otherwise the copy toolbar drifts out of sync with the grid.
341+
if (terminalCanvas.hasActiveSelection()) {
342+
GhosttyBridge.nativeClearSelection(terminalHandle)
343+
terminalCanvas.resetSelectionState()
344+
}
302345
}
303346
fedBuffer = initialBuffer
304347
renderSnapshot()

0 commit comments

Comments
 (0)