-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat(auth): add press 'c' to copy URL during login flow #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * Clipboard utilities | ||
| * | ||
| * Cross-platform utilities for copying text to the system clipboard. | ||
| * Includes both low-level copy function and interactive keyboard-triggered copy. | ||
| */ | ||
|
|
||
| import type { Writer } from "../types/index.js"; | ||
| import { success } from "./formatters/colors.js"; | ||
|
|
||
| const CTRL_C = "\x03"; | ||
| const CLEAR_LINE = "\r\x1b[K"; | ||
|
|
||
| /** | ||
| * Copy text to the system clipboard. | ||
| * | ||
| * Uses platform-specific commands: | ||
| * - macOS: pbcopy | ||
| * - Linux: xclip or xsel | ||
| * - Windows: clip | ||
| * | ||
| * @param text - The text to copy to clipboard | ||
| * @returns true if copy succeeded, false otherwise | ||
| */ | ||
| export async function copyToClipboard(text: string): Promise<boolean> { | ||
| const { platform } = process; | ||
|
|
||
| let command: string | null = null; | ||
| let args: string[] = []; | ||
|
|
||
| if (platform === "darwin") { | ||
| command = Bun.which("pbcopy"); | ||
| args = []; | ||
| } else if (platform === "win32") { | ||
| command = Bun.which("clip"); | ||
| args = []; | ||
| } else { | ||
| // Linux - try xclip first, then xsel | ||
| command = Bun.which("xclip"); | ||
| if (command) { | ||
| args = ["-selection", "clipboard"]; | ||
| } else { | ||
| command = Bun.which("xsel"); | ||
| if (command) { | ||
| args = ["--clipboard", "--input"]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!command) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| const proc = Bun.spawn([command, ...args], { | ||
| stdin: "pipe", | ||
| stdout: "ignore", | ||
| stderr: "ignore", | ||
| }); | ||
|
|
||
| proc.stdin.write(text); | ||
| proc.stdin.end(); | ||
|
|
||
| const exitCode = await proc.exited; | ||
| return exitCode === 0; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets up a keyboard listener that copies text to clipboard when 'c' is pressed. | ||
| * Only activates in TTY environments. Returns a cleanup function to restore stdin state. | ||
| * | ||
| * @param stdin - The stdin stream to listen on | ||
| * @param getText - Function that returns the text to copy | ||
| * @param stdout - Output stream for feedback messages | ||
| * @returns Cleanup function to restore stdin state | ||
| */ | ||
| export function setupCopyKeyListener( | ||
| stdin: NodeJS.ReadStream, | ||
| getText: () => string, | ||
| stdout: Writer | ||
| ): () => void { | ||
| if (!stdin.isTTY) { | ||
| return () => { | ||
| /* no-op for non-TTY */ | ||
| }; | ||
| } | ||
|
|
||
| stdin.setRawMode(true); | ||
| stdin.resume(); | ||
|
|
||
| let active = true; | ||
|
|
||
| const onData = async (data: Buffer) => { | ||
| const key = data.toString(); | ||
|
|
||
| if (key === "c" || key === "C") { | ||
| const text = getText(); | ||
| const copied = await copyToClipboard(text); | ||
| if (copied && active) { | ||
| stdout.write(CLEAR_LINE); | ||
| stdout.write(success("Copied!")); | ||
| } | ||
| } | ||
betegon marked this conversation as resolved.
Show resolved
Hide resolved
betegon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (key === CTRL_C) { | ||
| stdin.setRawMode(false); | ||
| stdin.pause(); | ||
| process.exit(130); | ||
| } | ||
| }; | ||
|
|
||
| stdin.on("data", onData); | ||
|
|
||
| return () => { | ||
| active = false; | ||
| stdin.off("data", onData); | ||
| if (stdin.isTTY) { | ||
| stdin.setRawMode(false); | ||
| } | ||
| stdin.pause(); | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.