Files
claude-code-mirror/claude-code源码-中文注释/src/ink/supports-hyperlinks.ts
2026-04-03 13:01:19 +08:00

58 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import supportsHyperlinksLib from 'supports-hyperlinks'
// 支持 OSC 8 超链接但不在 supports-hyperlinks 检测范围内的额外终端。
// 针对 TERM_PROGRAM 和 LC_TERMINAL后者在 tmux 内保留)进行检查。
export const ADDITIONAL_HYPERLINK_TERMINALS = [
'ghostty',
'Hyper',
'kitty',
'alacritty',
'iTerm.app',
'iTerm2',
]
type EnvLike = Record<string, string | undefined>
type SupportsHyperlinksOptions = {
env?: EnvLike
stdoutSupported?: boolean
}
/**
* 返回 stdout 是否支持 OSC 8 超链接。
* 使用额外的终端检测扩展 supports-hyperlinks 库。
* @param options 可选的测试覆盖env, stdoutSupported
*/
export function supportsHyperlinks(
options?: SupportsHyperlinksOptions,
): boolean {
const stdoutSupported =
options?.stdoutSupported ?? supportsHyperlinksLib.stdout
if (stdoutSupported) {
return true
}
const env = options?.env ?? process.env
// 检查 supports-hyperlinks 未检测到的额外终端
const termProgram = env['TERM_PROGRAM']
if (termProgram && ADDITIONAL_HYPERLINK_TERMINALS.includes(termProgram)) {
return true
}
// LC_TERMINAL 由某些终端设置(例如 iTerm2并在 tmux 内保留,
// TERM_PROGRAM 被覆盖为 'tmux'。
const lcTerminal = env['LC_TERMINAL']
if (lcTerminal && ADDITIONAL_HYPERLINK_TERMINALS.includes(lcTerminal)) {
return true
}
// Kitty 设置 TERM=xterm-kitty
const term = env['TERM']
if (term?.includes('kitty')) {
return true
}
return false
}