Files
orcs-code/src/utils/hyperlink.ts
did:key:z6MkqDnb7Siv3Cwj7pGJq4T5EsUisECqR8KpnDLwcaZq5TPr d2542c9a62 asdf
Squash the current repository state back into one baseline commit while
preserving the README reframing and repository contents.

Constraint: User explicitly requested a single squashed commit with subject "asdf"
Confidence: high
Scope-risk: broad
Reversibility: clean
Directive: This commit intentionally rewrites published history; coordinate before future force-pushes
Tested: git status clean; local history rewritten to one commit; force-pushed main to origin and instructkr
Not-tested: Fresh clone verification after push
2026-03-31 03:34:03 -07:00

40 lines
1.4 KiB
TypeScript

import chalk from 'chalk'
import { supportsHyperlinks } from '../ink/supports-hyperlinks.js'
// OSC 8 hyperlink escape sequences
// Format: \e]8;;URL\e\\TEXT\e]8;;\e\\
// Using \x07 (BEL) as terminator which is more widely supported
export const OSC8_START = '\x1b]8;;'
export const OSC8_END = '\x07'
type HyperlinkOptions = {
supportsHyperlinks?: boolean
}
/**
* Create a clickable hyperlink using OSC 8 escape sequences.
* Falls back to plain text if the terminal doesn't support hyperlinks.
*
* @param url - The URL to link to
* @param content - Optional content to display as the link text (only when hyperlinks are supported).
* If provided and hyperlinks are supported, this text is shown as a clickable link.
* If hyperlinks are not supported, content is ignored and only the URL is shown.
* @param options - Optional overrides for testing (supportsHyperlinks)
*/
export function createHyperlink(
url: string,
content?: string,
options?: HyperlinkOptions,
): string {
const hasSupport = options?.supportsHyperlinks ?? supportsHyperlinks()
if (!hasSupport) {
return url
}
// Apply basic ANSI blue color - wrap-ansi preserves this across line breaks
// RGB colors (like theme colors) are NOT preserved by wrap-ansi with OSC 8
const displayText = content ?? url
const coloredText = chalk.blue(displayText)
return `${OSC8_START}${url}${OSC8_END}${coloredText}${OSC8_START}${OSC8_END}`
}