mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
iOS: DModemKit, the app's modem apart from its views; terminal rows as runs
ios/DModemKit (Swift package): the engine (dmodem_engine.h) from Swift
(Engine: its config, its callbacks to a listener), its DTE (read on a
thread of its own, written), the settings (ModemSettings, as the Android
app's: the SIP account or the Wi-Fi peer line, user:password@host, kept in
UserDefaults, read leniently), ConnectInfo ("V.cvm · LAPM"), and
ModemSession (started from the settings: status, log, console, the DTE's
data, the speaker) for the views to watch. On Apple's systems the engine
is the XCFramework; on Linux the host build's libraries
(DMODEM_HOST_BUILD), so its tests run there: a session on a peer line
answering AT commands, a dial handed to the app and turned down busy.
shared: TerminalEmulator.runs(), a screen row as styled runs with their
colours resolved as TerminalView has them (the remote's palette, bold as
bright, iCE colours, inverse, faint), for a view that draws text (the iOS
app's); tested on the JVM and natively.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
10 files changed
+861
No files matched your search
@@ -0,0 +1,97 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.terminal
|
||||
|
||||
/**
|
||||
* A row of the screen as runs of text with one look each, its colours
|
||||
* resolved (ARGB): what a view that draws text, not cells, needs (the iOS
|
||||
* app's, in SwiftUI). As Android's TerminalView resolves them: the
|
||||
* remote's palette changes, bold as bright, iCE colours (blink as a bright
|
||||
* background), inverse (the cell's and the screen's), faint.
|
||||
*/
|
||||
class StyledRun(
|
||||
/** its first column, and the columns it covers (a wide character two) */
|
||||
val col: Int,
|
||||
val cells: Int,
|
||||
/** its text: never-written cells as spaces, a wide character once, combining marks after their base */
|
||||
val text: String,
|
||||
val fg: Int,
|
||||
val bg: Int,
|
||||
val bold: Boolean,
|
||||
val italic: Boolean,
|
||||
val underline: Boolean,
|
||||
val strike: Boolean,
|
||||
)
|
||||
|
||||
/** how the runs' colours are taken (the terminal's settings) */
|
||||
class RunLook(
|
||||
val theme: TermTheme = TermTheme.VGA,
|
||||
/** bold in colours 0-7 as their bright ones (8-15), the BBS way */
|
||||
val boldBright: Boolean = true,
|
||||
/** blink as a bright background, not blinking (iCE colours) */
|
||||
val iceColors: Boolean = false,
|
||||
)
|
||||
|
||||
/** [row] of the screen (0: its top; negative: the scrollback's, -1 the newest) as runs */
|
||||
fun TerminalEmulator.runs(row: Int, look: RunLook = RunLook()): List<StyledRun> {
|
||||
val line = line(row)
|
||||
val out = ArrayList<StyledRun>()
|
||||
val text = StringBuilder()
|
||||
|
||||
fun color(c: Int, isFg: Boolean): Int {
|
||||
val i = when {
|
||||
c == Style.DEFAULT_FG -> 256
|
||||
c == Style.DEFAULT_BG -> 257
|
||||
c and Style.RGB == 0 && c in 0..255 -> c
|
||||
else -> -1
|
||||
}
|
||||
if (i >= 0) {
|
||||
val o = colorOverride(i)
|
||||
if (o != -1) return o
|
||||
}
|
||||
return look.theme.argb(c, isFg)
|
||||
}
|
||||
|
||||
var x = 0
|
||||
while (x < line.cols) {
|
||||
val st = line.styles[x]
|
||||
var end = x + 1
|
||||
while (end < line.cols && line.styles[end] == st) end++
|
||||
val a = Style.attrs(st)
|
||||
var fg = Style.fg(st)
|
||||
var bg = Style.bg(st)
|
||||
if (a and Style.BOLD != 0 && look.boldBright && fg in 0..7) fg += 8
|
||||
if (look.iceColors && a and Style.BLINK != 0 && bg in 0..7) bg += 8
|
||||
var f = color(fg, true)
|
||||
var b = color(bg, false)
|
||||
if ((a and Style.INVERSE != 0) != reverseVideo) {
|
||||
val t = f; f = b; b = t
|
||||
}
|
||||
if (a and Style.FAINT != 0) f = (f and 0x00ffffff) or (0x99 shl 24)
|
||||
val hidden = a and Style.INVISIBLE != 0
|
||||
text.setLength(0)
|
||||
for (i in x until end) {
|
||||
val cp = line.chars[i]
|
||||
when {
|
||||
cp == Line.WIDE_TAIL -> {}
|
||||
hidden || cp == 0 -> text.append(' ')
|
||||
else -> {
|
||||
text.appendCp(cp)
|
||||
line.extra(i)?.combining?.forEach { text.appendCp(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
out.add(
|
||||
StyledRun(
|
||||
col = x, cells = end - x, text = text.toString(), fg = f, bg = b,
|
||||
// (heavy glyphs only when bold isn't shown as a bright colour instead)
|
||||
bold = a and Style.BOLD != 0 && !(look.boldBright && Style.fg(st) in 0..7),
|
||||
italic = a and Style.ITALIC != 0,
|
||||
underline = a and Style.ANY_UNDERLINE != 0,
|
||||
strike = a and Style.STRIKE != 0,
|
||||
),
|
||||
)
|
||||
x = end
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.terminal
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/** A screen's rows as styled runs (what the iOS app draws): the text, its cells, its colours resolved. */
|
||||
class TerminalRunsTest {
|
||||
private val vga = TermTheme.VGA
|
||||
private fun argb(rgb: Int) = 0xff000000.toInt() or rgb
|
||||
|
||||
@Test
|
||||
fun plainColouredBoldInverse() {
|
||||
val t = TerminalEmulator(20, 3)
|
||||
// red, then bold red (bright, as BBSes have it), inverse, default
|
||||
t.appendText("\u001b[31mab\u001b[1mcd\u001b[0;7mef\u001b[0mgh")
|
||||
val r = t.runs(0)
|
||||
assertEquals(listOf("ab", "cd", "ef", "gh" + " ".repeat(12)), r.map { it.text })
|
||||
assertEquals(listOf(0, 2, 4, 6), r.map { it.col })
|
||||
assertEquals(argb(0xaa0000), r[0].fg)
|
||||
assertEquals(argb(0xff5555), r[1].fg)
|
||||
assertFalse(r[1].bold)
|
||||
// inverse: the default colours swapped
|
||||
assertEquals(vga.bgArgb, r[2].fg)
|
||||
assertEquals(vga.fgArgb, r[2].bg)
|
||||
assertEquals(20, r.sumOf { it.cells })
|
||||
// bold shown heavy when it isn't bright instead
|
||||
val heavy = t.runs(0, RunLook(boldBright = false))
|
||||
assertTrue(heavy[1].bold)
|
||||
assertEquals(argb(0xaa0000), heavy[1].fg)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wideCharactersAndTrueColour() {
|
||||
val t = TerminalEmulator(10, 2)
|
||||
t.appendText("\u001b[38;2;1;2;3m漢x")
|
||||
val r = t.runs(0)
|
||||
// the wide character once, over two cells
|
||||
assertEquals("漢x", r[0].text)
|
||||
assertEquals(3, r[0].cells)
|
||||
assertEquals(argb(0x010203), r[0].fg)
|
||||
// a row never written: spaces, the theme's colours
|
||||
val blank = t.runs(1).single()
|
||||
assertEquals(" ".repeat(10), blank.text)
|
||||
assertEquals(vga.bgArgb, blank.bg)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theRemotesPalette() {
|
||||
val t = TerminalEmulator(10, 1)
|
||||
// OSC 4: colour 1 is now green
|
||||
t.appendText("\u001b]4;1;rgb:00/ff/00\u001b\\\u001b[31mx")
|
||||
assertEquals(argb(0x00ff00), t.runs(0)[0].fg)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user