58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
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
|
||
}
|