Skip to content

terminal: add drag-and-drop support - #101

Merged
vaayne merged 3 commits into
vaayne:mainfrom
josikinzz:feat/terminal-drag-drop-upstream
Jul 29, 2026
Merged

vaayne merged 3 commits into
vaayne:mainfrom
josikinzz:feat/terminal-drag-drop-upstream

Conversation

@josikinzz

Copy link
Copy Markdown
Contributor

Dragging files (or URLs/text) onto a Mori terminal pane currently does nothing — GhosttySurfaceView never registers for dragged types, so AppKit discards the drop before any bytes reach the terminal.

This PR adds drag-and-drop to the Ghostty surface, mirroring upstream Ghostty's macOS SurfaceView implementation:

  • registers .URL, .fileURL, and .string pasteboard types and answers .copy for supported drags
  • URL content first (escaped as-is), then file URLs (absolute paths, shell-escaped with Ghostty's shell-sensitive character set, space-joined in item order), then plain strings unescaped — matching Ghostty exactly
  • inserts via insertText on the main queue after the drag session ends, so content flows through ghostty_surface_text, which libghostty treats with paste semantics (bracketed paste when the pty enables it)

That last point means drops behave like pastes end to end: TUIs inside Mori's tmux sessions (pi, Claude Code, …) receive a real bracketed paste and can attach a dropped image instead of seeing raw keystrokes. Complements the remote-image request in #51 for the local case.

Testing

  • New MoriTerminalTests executable target (following the repo's non-XCTest convention), wired as mise run test:terminal and into the aggregate suite: drag-type registration, URL/file/string precedence, percent-decoding, shell escaping (spaces, parentheses), multi-item ordering.
  • swift build --product Mori and swift run MoriTerminalTests pass.
  • Verified live in a local build: dragging a PNG from Finder onto a pi pane inside a Mori tmux session attaches the image, spaces in the path included.

CHANGELOG and README entries added in both languages per repo conventions.


i hope this change is acceptable, it works on my end and solved a big problem for me, i used claude fable to implement the fix, apologies if this is an annoying slop pr

@vaayne vaayne left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this — the overall approach and test coverage look solid. I found two edge cases worth addressing before merge.

return TerminalShell.escape(url)
}

let strings = (pasteboardItems ?? []).compactMap { item in

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This per-item fallback differs from Ghostty’s actual drag-and-drop precedence. In vendored Ghostty, SurfaceView_AppKit.performDragOperation checks the whole pasteboard in order: .URL, then all NSURL objects, then .string. If any file URL exists, plain-string items are not appended. Here, dragging a file plus a separate string produces /path/to/file describe this, and the new mixed-item test locks in that divergence. Could we mirror performDragOperation’s global branching here and update the test? (Ghostty’s getOpinionatedStringContents() does use per-item fallback, but that helper is not used by its drag path.)


private enum TerminalShell {
// Match Ghostty's shell-sensitive character set for terminal insertion.
private static let escapeCharacters = "\\ ()[]{}<>\"'`!#$&;|*?\t"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reject file paths containing CR or LF before inserting them. macOS filenames can contain newlines, and this escape set leaves them untouched. The path then reaches ghostty_surface_text; libghostty treats that as an already-confirmed paste, and when bracketed paste is disabled it converts LF to CR. A maliciously named dropped file can therefore submit subsequent lines as terminal commands without another Enter. Ghostty currently shares this escape set, but this PR makes the path reachable in Mori. The smallest safe fix is to return nil for file paths containing \r or \n and add a regression test.

vaayne added 2 commits July 29, 2026 22:19
Both sides added test targets and changelog sections independently:
- mise.toml/AGENTS.md: keep test:terminal (ours) and test:git + test:ui (theirs)
- CHANGELOG*: keep the drag-and-drop Features entry, adopt the renamed
  Design section heading from main
Inspect the pasteboard as a whole (URL, else all file URLs, else string)
instead of falling back per item, so a file plus a string no longer inserts
both — this mirrors Ghostty's performDragOperation.

Reject file paths containing CR/LF: the shell escape set leaves them
untouched, and libghostty treats inserted text as a confirmed paste
(converting LF to CR when bracketed paste is off), so a maliciously named
file could submit the rest of the value as a command. One unsafe path
rejects the whole drop rather than silently omitting a file.
@vaayne
vaayne merged commit 10131f5 into vaayne:main Jul 29, 2026
5 checks passed
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.

2 participants