Skip to content

feat(auth): copy the device-flow user code to the clipboard - #44

Merged
ngjunsiang merged 1 commit into
mainfrom
feat/43-copy-user-code-to-clipboard
Oct 9, 2026
Merged

ngjunsiang merged 1 commit into
mainfrom
feat/43-copy-user-code-to-clipboard

Conversation

@ngjunsiang

Copy link
Copy Markdown
Contributor

Closes #43

Summary

campus auth login now attempts to copy the user code to the clipboard right after printing it, with a status line that reflects what actually happened:

  • Code copied to clipboard. — pyperclip succeeded (pbcopy / clip.exe / ctypes / xclip / wl-copy; WSL-aware). Definitive.
  • Sent the code to your terminal's clipboard (OSC 52); if it doesn't paste, copy it manually. — no native mechanism (e.g. headless SSH host), so the OSC 52 sequence was emitted instead. Delivery is unacknowledged by terminals, hence the hedge.
  • (silent) — both mechanisms unavailable; the printed code remains, i.e. exactly the old behavior.

Design notes

  • Why pyperclip rather than hand-rolled: it owns the platform matrix (macOS/Windows/WSL/X11/Wayland) in a pure-stdlib package with zero transitive dependencies. The one gap it can't cover — clipboard over plain SSH — is a ~15-line OSC 52 emitter this PR adds.
  • SSH: native tools run on the remote (usually headless) host and fail fast; OSC 52 travels back through the SSH channel to the local terminal, which is the only mechanism needing no remote tooling. Terminal support varies (tmux needs set-clipboard external — we also send the DCS passthrough wrapper; iTerm2 requires opt-in; VTE/GNOME Terminal ignores it). With X forwarding, pyperclip correctly targets the user's own local clipboard.
  • Safety: write-only path (the CLI never reads the clipboard); the helper never raises and never blocks login; piped stdout never receives escape bytes.

Test plan

  • New tests/unit/test_clipboard.py: native success, native-failure → OSC 52, missing-pyperclip → OSC 52, non-TTY suppression, tmux DCS wrapping.
  • test_auth_login_binds_tokens_to_target_auth_url patches the helper (CI never touches a real clipboard) and asserts the status line.
  • ruff check clean; full suite: 167 passed.

Transcribing the hyphenated user code is the clunkiest step of login,
and a typo costs a full browser round-trip. login now attempts a
best-effort clipboard copy with an honest status line:

- pyperclip (native pbcopy/clip.exe/xclip/wl-copy, WSL-aware) first:
  definitive result on any desktop session.
- OSC 52 escape sequence as fallback: travels back through the SSH
  channel, so it works over plain SSH in supporting terminals (tmux
  gets the DCS passthrough wrapper; no escape bytes on piped stdout).
  Delivery is unacknowledged, so the message hedges.

If neither mechanism is available (headless host, unsupported
terminal), nothing is printed and login proceeds exactly as before.
The helper never raises or blocks login.
@ngjunsiang
ngjunsiang merged commit e4c17c9 into main Oct 9, 2026
4 checks passed
@ngjunsiang
ngjunsiang deleted the feat/43-copy-user-code-to-clipboard branch October 20, 2026 13:14
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.

login: copy the device-flow user code to the clipboard

1 participant