Skip to content

Charsets - #9

Merged
mitchellh merged 7 commits into
mainfrom
charsets
Aug 27, 2022
Merged

mitchellh merged 7 commits into
mainfrom
charsets

Conversation

@mitchellh

Copy link
Copy Markdown
Contributor

Initial charset support. This gets a lot of stuff working but still has some critical (but not very real world) limitations.

Implements:

  • All charset slots G0 to G3
  • All shifting commands to shift into GL and GR
  • Single shift commands ESC N and ESC O
  • UTF8, ASCII, British, and DEC Special Graphics character sets

Notably missing:

  • A number of rarely used charsets
  • NRC handling
  • Disabling UTF-8 mode

All the missing stuff is very edge case... but necessary for correctness.

@mitchellh
mitchellh merged commit cd74902 into main Aug 27, 2022
@mitchellh
mitchellh deleted the charsets branch August 27, 2022 17:33
mitchellh added a commit that referenced this pull request Feb 18, 2026
This PR introduces unit tests and a supporting Mock NSView for testing
the SplitTree implementation in Swift. It includes 51 tests which
achieve approximately 93.13% (949/1019) coverage of SplitTree.swift's
branches.

<details>
  <summary>Coverage</summary>
  <pre>
./ghostty/macos/Sources/Features/Splits/SplitTree.swift 93.13%
(949/1019)
SplitTree.Path.isEmpty.getter 100.00% (1/1)
SplitTree.isEmpty.getter 100.00% (3/3)
SplitTree.isSplit.getter 100.00% (3/3)
SplitTree.init() 100.00% (3/3)
SplitTree.init(view:) 100.00% (3/3)
SplitTree.contains(_:) 100.00% (4/4)
SplitTree.inserting(view:at:direction:) 100.00% (6/6)
SplitTree.find(id:) 100.00% (4/4)
SplitTree.removing(_:) 93.75% (15/16)
SplitTree.replacing(node:with:) 93.75% (15/16)
SplitTree.focusTarget(for:from:) 82.14% (46/56)
closure #1 in SplitTree.focusTarget(for:from:) 100.00% (1/1)
closure #2 in SplitTree.focusTarget(for:from:) 100.00% (1/1)
closure #3 in SplitTree.focusTarget(for:from:) 100.00% (3/3)
implicit closure #1 in SplitTree.focusTarget(for:from:) 0.00% (0/1)
SplitTree.equalized() 100.00% (5/5)
SplitTree.resizing(node:by:in:with:) 92.00% (69/75)
closure #1 in SplitTree.resizing(node:by:in:with:) 100.00% (1/1)
SplitTree.viewBounds() 100.00% (4/4)
SplitTree.init(from:) 76.00% (19/25)
SplitTree.encode(to:) 100.00% (15/15)
SplitTree.Node.find(id:) 100.00% (13/13)
SplitTree.Node.node(view:) 88.89% (16/18)
SplitTree.Node.path(to:) 100.00% (32/32)
search #1 <A>(_:) in SplitTree.Node.path(to:) 100.00% (27/27)
SplitTree.Node.node(at:) 89.47% (17/19)
SplitTree.Node.inserting(view:at:direction:) 86.84% (33/38)
SplitTree.Node.replacingNode(at:with:) 100.00% (43/43)
replaceInner #1 <A>(current:pathOffset:) in
SplitTree.Node.replacingNode(at:with:) 96.67% (29/30)
SplitTree.Node.remove(_:) 70.27% (26/37)
implicit closure #1 in SplitTree.Node.remove(_:) 100.00% (1/1)
SplitTree.Node.resizing(to:) 100.00% (16/16)
SplitTree.Node.leftmostLeaf() 87.50% (7/8)
SplitTree.Node.rightmostLeaf() 87.50% (7/8)
SplitTree.Node.equalize() 100.00% (4/4)
SplitTree.Node.equalizeWithWeight() 100.00% (30/30)
SplitTree.Node.weightForDirection(_:) 83.33% (10/12)
SplitTree.Node.calculateViewBounds(in:) 100.00% (50/50)
SplitTree.Node.viewBounds() 100.00% (26/26)
SplitTree.Node.spatial(within:) 100.00% (18/18)
SplitTree.Node.dimensions() 80.77% (21/26)
SplitTree.Node.spatialSlots(in:) 100.00% (53/53)
SplitTree.Spatial.slots(in:from:) 100.00% (47/47)
closure #1 in SplitTree.Spatial.slots(in:from:) 100.00% (1/1)
distance #1 <A>(from:to:) in SplitTree.Spatial.slots(in:from:) 100.00%
(6/6)
closure #2 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
implicit closure #1 in closure #2 in SplitTree.Spatial.slots(in:from:)
100.00% (1/1)
closure #3 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
closure #4 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
implicit closure #1 in closure #4 in SplitTree.Spatial.slots(in:from:)
100.00% (1/1)
closure #5 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
closure #6 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
implicit closure #1 in closure #6 in SplitTree.Spatial.slots(in:from:)
100.00% (1/1)
closure #7 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
closure #8 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
implicit closure #1 in closure #8 in SplitTree.Spatial.slots(in:from:)
100.00% (1/1)
closure #9 in SplitTree.Spatial.slots(in:from:) 100.00% (3/3)
SplitTree.Spatial.doesBorder(side:from:) 100.00% (20/20)
closure #1 in SplitTree.Spatial.doesBorder(side:from:) 100.00% (1/1)
closure #2 in SplitTree.Spatial.doesBorder(side:from:) 100.00% (3/3)
static SplitTree.Node.== infix(_:_:) 100.00% (13/13)
SplitTree.Node.init(from:) 66.67% (12/18)
SplitTree.Node.encode(to:) 100.00% (11/11)
SplitTree.Node.leaves() 100.00% (9/9)
SplitTree.makeIterator() 100.00% (3/3)
implicit closure #1 in SplitTree.makeIterator() 100.00% (1/1)
SplitTree.Node.makeIterator() 0.00% (0/3)
SplitTree.startIndex.getter 100.00% (3/3)
SplitTree.endIndex.getter 100.00% (3/3)
implicit closure #1 in SplitTree.endIndex.getter 100.00% (1/1)
SplitTree.subscript.getter 100.00% (5/5)
implicit closure #1 in SplitTree.subscript.getter 100.00% (1/1)
implicit closure #2 in implicit closure #1 in SplitTree.subscript.getter
100.00% (1/1)
implicit closure #3 in SplitTree.subscript.getter 0.00% (0/1)
implicit closure #4 in SplitTree.subscript.getter 0.00% (0/1)
SplitTree.index(after:) 100.00% (4/4)
implicit closure #1 in SplitTree.index(after:) 100.00% (1/1)
implicit closure #2 in SplitTree.index(after:) 0.00% (0/1)
SplitTree.Node.structuralIdentity.getter 100.00% (3/3)
SplitTree.Node.StructuralIdentity.init(_:) 100.00% (3/3)
static SplitTree.Node.StructuralIdentity.== infix(_:_:) 100.00% (3/3)
SplitTree.Node.StructuralIdentity.hash(into:) 100.00% (3/3)
SplitTree.Node.isStructurallyEqual(to:) 100.00% (18/18)
implicit closure #1 in SplitTree.Node.isStructurallyEqual(to:) 100.00%
(1/1)
implicit closure #2 in SplitTree.Node.isStructurallyEqual(to:) 100.00%
(1/1)
SplitTree.Node.hashStructure(into:) 100.00% (14/14)
SplitTree.structuralIdentity.getter 100.00% (3/3)
SplitTree.StructuralIdentity.init(_:) 100.00% (4/4)
static SplitTree.StructuralIdentity.== infix(_:_:) 100.00% (4/4)
implicit closure #1 in static SplitTree.StructuralIdentity.==
infix(_:_:) 100.00% (1/1)
SplitTree.StructuralIdentity.hash(into:) 80.00% (8/10)
static SplitTree.StructuralIdentity.areNodesStructurallyEqual(_:_:)
90.00% (9/10)
  </pre>
</details>

I chose this as a good place to start contributing to Ghostty because I
was curious about the macOS implementation, and there was a specific
request for help with testing (#7879).

My process for writing the tests was basically reading
[SplitTree.swift](./macos/Sources/Features/Splits/SplitTree.swift) to
understand it, then writing tests for each high-level method and
checking against code coverage to capture all the code paths:

## Running
```bash
rm -rf /tmp/ghostty-test.xcresult
xcodebuild -project macos/Ghostty.xcodeproj \
    -scheme GhosttyTest \
    -configuration Debug \
    test \
    -destination 'platform=macOS' \
    -enableCodeCoverage YES \
    -resultBundlePath /tmp/ghostty-test.xcresult \
    -only-testing:GhosttyTests/SplitTreeTests \
    2>&1 | xcbeautify
```

## Coverage
```bash
xcrun xccov view --report /tmp/ghostty-test.xcresult | grep 'SplitTree\.'
```

This was originally implemented in [~38
commits](https://github.com/pouwerkerk/ghostty/pull/1/commits), but I
squashed them down to 1 commit for easier review.

## AI Disclosure
The tests were written by me, but I used Opus 4.6 to explain some parts
of the code, and then finally to provide feedback on the tests. It
suggested tests for `nodeStructuralIdentityInSet` and
`nodeStructuralIdentityDistinguishesLeaves` as well as [the
Parameterized
test](pouwerkerk@6a0bca4),
`resizingAdjustsRatio`, which seemed like a clever way to collapse 12
individual tests into 3 parameterized ones that still run 12 cases
total. I didn't know this feature existed, and it seems like a great way
to write tests that are more maintainable. I read this relatively new
feature in the [Swift
Docs](https://developer.apple.com/documentation/testing/parameterizedtesting).
I find this to be a particularly useful feature of Claude/related
agents, where it can suggest better ways of writing something in a more
idiomatic way, and it taught me something new, which is always fun.

I'm more than happy to continue work on tests for #7879 and always
welcome to any feedback you have.
ren18161849172-byte added a commit to ren18161849172-byte/ghostty that referenced this pull request May 7, 2026
IMM32 IME support:
- WM_IME_STARTCOMPOSITION/COMPOSITION/ENDCOMPOSITION handlers
- IME composition window positioned at terminal cursor via setImePosition()
- Preedit text callback for in-progress composition
- Result text committed as key events
- Candidate window positioned near cursor
DAmesberger added a commit to DAmesberger/ghostty that referenced this pull request May 22, 2026
Reviewer caught during 6B.2 audit (task ghostty-org#9): every encodeParams() in
Service.swift used a fabricated wire format that didn't match the
canonical decoder in src/session/services/*.zig. Today that's masked
by ssh_capi.zig synthesizing on_close(SERVICE_ERROR) unconditionally,
but the moment the client-mux integration lands every channel open()
would fail with the daemon decoding garbage.

Four divergences, all fixed:

  * TCPConnectService — was [host UTF-8][NUL][port u16 BE]; now
    [u16 LE host_len][host][u16 LE port] matching tcp_connect.zig:16-18.
  * PortListenerService — same fix as TCPConnectService. port_listener.zig
    is still in flight (Phase 6A.5), so this mirrors the project house
    style of length-prefix + LE; comment marks it for re-verification
    when 6A.5 lands.
  * FileTransferService — was [op u8][path][NUL]; now
    [u8 direction][u32 LE mode][u16 LE path_len][path][upload-only
    trailer: 32-byte expected_sha256 + u64 LE total_size] matching
    file_transfer.zig:18-26. Operation.upload now takes mode, optional
    expectedSHA256 Data, and totalSize. Operation.download surface
    unchanged but the wire encoding now emits the mandatory mode +
    path_len prefix.
  * BrowserProxyService — was [method u8][target NUL-term][u16 BE
    header_count][[name NUL][value NUL]...]; now
    [u8 upstream_kind][u16 LE host_len][host][u16 LE port][metadata]
    matching browser_proxy.zig:17-24. The Method/headers public surface
    was completely fabricated and is gone — replaced with an UpstreamKind
    enum (direct / httpConnectTarget / socks5Target) + split host/port +
    opaque metadata Data.

Also added max-length constants matching each decoder's caps
(max_host_len=255, max_path_len=4096, max_metadata_len=8K) and three
private LE encoding helpers (u16/u32/u64) so the encoders read top-to-
bottom against the wire-format comments.

Tests in SSHTests.swift rewritten to assert exact byte sequences
against each format. The wire formats are spec-locked, so test diffs
will catch future drift before the daemon does.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
real0x0a1 added a commit to real0x0a1/ghostty that referenced this pull request Jul 1, 2026
Bidirectional (UAX ghostty-org#9) text shaping for Arabic/Hebrew/Persian via the pure-Zig
itijah resolver: the run iterator resolves per-row visual runs and shapes each
with the correct direction and joining forms. Adapted from upstream PR ghostty-org#11079
by DiaaEddin, with the shaper tests fixed for the HarfBuzz/Linux backend
(vtStream.nextSlice returns void; Chakma cluster x on HarfBuzz).
real0x0a1 added a commit to real0x0a1/ghostty that referenced this pull request Jul 1, 2026
Two changes to resolveRowLayout, building on the RTL shaping foundation:

- Base direction is now auto-detected per row from the first strong
  character (itijah's auto_ltr / UAX ghostty-org#9 P2-P3), falling back to LTR for
  rows with no strong character, instead of being hard-anchored to LTR.
  A line of pure Persian/Arabic/Hebrew now reads and aligns right-to-left,
  while ordinary shell output stays LTR.

- Add an identity fast path: rows whose codepoints are all below U+0590
  (no RTL script, Arabic number, or bidi formatting control) cannot
  reorder, so we skip the UAX ghostty-org#9 pass and its per-row allocation entirely
  and emit a single identity visual run. This keeps "bidi always on"
  free for the common ASCII/Latin case -- one comparison per cell.

Adds a test pinning the fast-path identity contract. All shaper tests
pass on the HarfBuzz backend (93 passed, 7 skipped).
real0x0a1 added a commit to real0x0a1/ghostty that referenced this pull request Jul 1, 2026
Resolve the per-row UAX ghostty-org#9 codepoint buffer through a reusable font.bidi.Scratch owned by the shaper, so a bidi row no longer allocates every frame; the LTR fast path still short-circuits before any allocation.

Drop the self-referential identity_run slice: the fast path now sets a flag and synthesizes the identity run in visualRunAt(), so a copied RunIterator can never dangle into its own storage.

Remove the now-orphan itijah imports from the shaper backends and add tests: mixed LTR/RTL and digits-under-RTL mapping, scratch/allocating agreement, run.zig helper coverage, and stronger backend-independent CoreText ZWNJ assertions.
real0x0a1 added a commit to real0x0a1/ghostty that referenced this pull request Jul 2, 2026
Rows containing CJK or emoji (>= U+0590 but never reorderable) took
the full UAX ghostty-org#9 pass per dirty row. needsBidi now quick-rejects below
U+0590 and consults the bidi class above it, so only rows that can
actually produce an odd embedding level under the LTR base leave the
identity fast path.
real0x0a1 added a commit to real0x0a1/ghostty that referenced this pull request Jul 2, 2026
The original construction alternated single 'a'/alef cells, which is an
identity trap: every RTL run has length 1, so UAX ghostty-org#9 L2's reversal is a
no-op and the resolved map is the identity permutation — the round-trip
and helper-agreement assertions had zero failure power (a mutation
making translateScratch return col unconditionally stayed green).

Alternate in pairs instead so every RTL run is 2 cells and genuinely
reverses (~256 visual runs, still the many-runs adversarial shape), and
pin the first RTL pair's exact reversal in both map directions so an
identity fallback fails immediately. Mutation re-verified: the
unconditional-identity mutant now fails the helper-agreement assertion.
real0x0a1 added a commit to real0x0a1/ghostty that referenced this pull request Jul 2, 2026
…notes

- font/bidi.zig: replace the wide-pair adjacency assert with a graceful
  skip; a violated equal-level invariant would be a dependency bug and
  now yields wrong order for that pair instead of UB in ReleaseFast.
- font/bidi.zig: drop pub from the allocating map variants (production
  callers all use the Scratch variants) and update the module doc to
  match: event-driven callers hold a persistent Scratch too.
- font/bidi.zig: needsBidi doc: UAX ghostty-org#9 I1 raises strong RTL to the next
  odd level, not always level 1.
- build.zig.zon: reword the itijah source note as a factual maintainer
  note; the content hash pins integrity and mirroring to
  deps.files.ghostty.org is requested as part of upstreaming.
- build/uucode_config.zig: the unchanged wasm size is because the
  unreferenced table decls are never analyzed or emitted, not
  "dead-stripped".
dzearing referenced this pull request in dzearing/ghoztty Jul 4, 2026
… watcher

Standing item #9 (CONTINUATION-3): the relay daemon read its device token
exactly once at startup, so a re-enroll silently required a manual restart,
and enroll's relay.env write could race a live daemon mid-read.

- enroll: saveRelayEnv is now ATOMIC — content goes to a .tmp sibling
  (fsync'd), then rename publishes it over the target. Plain rename(2) on
  POSIX; on Windows zig's Dir.rename issues FILE_RENAME_INFORMATION with
  ReplaceIfExists (MoveFileEx-replace semantics), so replacing an existing
  relay.env works on both OSes. A polling daemon can never observe a
  half-written credential.

- new relay_creds.zig: `Creds` holds the live token behind a mutex (every
  control dial snapshots it; superseded tokens are retired, never freed,
  because session workers borrow snapshots); `Watcher` polls relay.env on a
  5s stat(mtime+size) fast path, parses only on change, and applies the pure
  `decide` rule: relay.env-sourced token changed -> adopt + bounce the
  control link (redial with fresh creds within one backoff);
  GHOSTTY_DEVICE_TOKEN-sourced -> log and ignore (env keeps precedence);
  missing/empty file -> keep working creds. RELAY_BASE changes can't be
  hot-swapped and log a restart hint.

- link_control: new `bounce()` — close the live connection WITHOUT touching
  the desired state, so a tray Disconnect stays parked while an online link
  just drops and redials. Cross-platform by construction (no SIGHUP).

- main: the dial path builds the Authorization header per dial from the
  Creds snapshot; RelayConn carries its snapshot so per-session data dials
  keep using the token their control connection authenticated with.

Tests: atomic-replace + no-.tmp-left assertions (enroll), decide/baseMatches/
Creds/Watcher units incl. bounce-on-change + env-wins + missing-file
(relay_creds), bounce online/parked loop tests (link_control) — 220/220 via
`zig build test-agent`. Host + x86_64-windows-gnu builds pass. Verified live:
rotating relay.env under a running --relay daemon logs "reconnecting with
the new credential" within one poll tick.

Co-Authored-By: Claude Fable 5 <[email protected]>
RachidAlassir added a commit to RachidAlassir/ghostty that referenced this pull request Sep 4, 2026
The RTL shaper pulls in itijah (UAX ghostty-org#9) plus its lazy ucd dependency.
The flatpak build runs zig with --system vendor/p, which disables
fetching, so both packages have to be listed here or dependency
resolution fails before anything is compiled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant